This post contains attachments v20020317185530.zip 
Summary
This article explains how to create a class and how to dynamically invoke its methods using System.Reflection Namespace.
Reflection provides objects that encapsulate assemblies, modules, and types. Reflection can be used to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object. You can invoke the type’s methods or access its fields and properties.
Reflection can be used to create applications called type browsers, which enable users to select types and then view the information about those types.
It can at times even be used to emit Intermediate Language Code on the fly so that the generated code can be executed directly.
The Reflection API uses the System.Reflection namespace, with the Type class to identify the Type of the class being reflected, fields of a struct or enum represented by the FieldInfo class, members of the reflected class represented by the MemberInfo class, methods of a reflected class represented by the MethodInfo class, and parameters to methods represented by the ParameterInfo class.
The Activator class's CreateInstance() method is used to dynamically invoke instances on the fly. Dynamic Invocation is very useful in providing a very late-binding architecture, where one component's runtime can be integrated with other component runtimes.
To explain the concept of dynamic method invocation, let me explain first by creating a class Client as follows:
Using C#:
using System;
class Client
{
private static String sFirstName = Pandu;
private static String sMiddleName = ;
private static String sLastName = Rao;
private String sAddress1;
private String sAddress2;
private String sCity;
private String sState;
private String sZip5;
private String sZip4;
public static String GetName()
{
return(sFirstName + sMiddleName + sLastName);
}
public String GetAddress()
{
sAddress1 = One New York Plaza;
sAddress2 = Water Street;
sCity = New York City;
sState = New York;
sZip5 = 10004;
sZip4 = 1234;
string sRetAddress = sAddress1 + , + sAddress2 + , + sCity + , + sState + , + sZip5 + , + sZip4;
return sRetAddress;
}
public String Greetings(String sName)
{
return(Hello + sName + . Welcome to the world to .NET);
}
}
Save the above code with filename Client.cs
Compile it as follows:
Csc /t:library Client.cs
This command will build a library called Client.dll
Refer Client.vb for VB.NET Code.
Compile it as follows:
C:\pandu\asp\reflection>vbc /t:library Client.vb
This command will build a library called Client.dll
Here I am giving the example with both C# .NET and VB.NET. While running this class, you need to select one language to run the application.
Now let us create a console-based application, which invokes the methods of the Client class dynamically by using System.Reflection namespace.
In the example given below,
Assembly oAssemby = Assembly.Load(path);
Loads an assembly given its AssemblyName “Clientâ€
Type oType = oAssemby.GetType(Client);
Gets the Type object that represents the specified type “Clientâ€
String sOutput = (String) oType.InvokeMember (GetName,BindingFlags.Default | BindingFlags.InvokeMethod,null,null,new Object [] {});
Invokes the specified member using specified binding constraints and matching the specified matching constraints. Here “GetName†is the method name of type string, Binding.Flags.Default | BindingFlags.InvokeMethod is of type BindingFlags, Binder is null, object to instantiate is null and new Object[] {} for argument list.
Refer Code in ClientInvoke.cs
Compile this code using the following command:
C:\pandu\asp\reflection>Csc ClientInvoke.cs
Refer Code of ClientInvoke.vb
Compile this code using the following command:
C:\pandu\asp\reflection>vbc ClientInvoke.vb
From the above code, you will know the differences in writing VB.NET and C#.NET code.
When you compile the above code, ClientInvoke.exe is created.
Then call the application by using the following command at command prompt:
C:\pandu\asp\reflection>ClientInvoke
System Requirements: .NET SDK
|