Main BLOGGER
Google
WWW THIS BLOG
Wednesday, March 16, 2005
 
.NET reflection introduction paper
About the author
Name :Bipin Joshi

.NET assemblies
Assemblies are the building blocks of any .NET application. All functionality of .NET application is exposed via assemblies. Assemblies form a unit of deployment and versioning. Assemblies contain modules which in turn contain various types (classes, structures, enumerations etc.).

Getting started
Before obtaining any information about types contained in an assembly we must first load the assembly.

Assembly myassembly = Assembly.LoadFrom("employee.dll");

This statement loads an assembly called employee.dll. You can substitute your own path here. Assembly class has a static method called LoadFrom that loads the specified assembly in memory. The method returns an instance of assembly class itself.

The next step is to obtain a list of various types contained in the assembly.

Types mytypes[] = myassembly.GetTypes();
Type mytype=myassembly.GetType("Company.Employee");

There are two methods to get type information . The method GetTypes returns an assay of System.Type objects. The method GetType returns a type object having details of specified object.

The Type class has following properties that gives details about the type under consideration :
Name : Gives name of the type
FullName : Give fully qualified name of the type
Namespace : Gives namespace name
IsClass
IsInterface
IsAbstract
IsCOMObject : Indicates if the type is a COM object
IsEnum
IsSealed
IsPublic

Each type may have fields (member variables), properties and methods. The details about each of these types are obtained by following methods of the Type object.

GetMembers() : Gives array of MemberInfo objects
GetFields() : Gives array of FieldInfo objects
GetProperties() : Gives array of PropertyInfo objects
GetMethods() : Gives array of MethodInfo objects

Note that you can also get information about specific method, property or field using GetMethod("mymethod"), GetProperty("myprop") or GetField("myfield") methods.

MethodInfo[] mymethods= mytype.GetMethods();
MethodInfo mymethod = mytype.GetMethod("GetSalary");

Following paragraphs list commonly used properties and methods of above objects. The property and method names are self explanatory. You can refer MSDN for more details.

Properties and methods of MethodInfo Object
Name
IsPrivate
IsPublic
IsStatic
IsConstructor
ReturnType
GetParameters()
Invoke()


Properties and methods of PropertyInfo Object

Name
CanRead
CanWrite
PropertyType
GetValue()
SetValue()


Properties and methods of FieldInfo Object
Name
FieldType
IsPublic
IsPrivate
IsStatic
GetValue()
SetValue()

Invoking a method on a type
We have seen how to get information about various types from an assembly. Reflection also allows us to create instances of these types and invoke methods on them. Following code fragment shows just that.Assembly

Assembly a=Assembly.LoadFrom("employee.dll");
Type t=a.GetType("Company.Employee");
MethodInfo getsalary=t.GetMethod("GetSalary");
object obj=Activator.CreateInstance(t);
object[] p=new object[1];
p[0]="bipin";
object retval=getsalary.Invoke(obj,BindingFlags.DefaultBinding, null,p,null); Console.WriteLine(retval);

Here, we first obtained type of employee class. We then created an instance of it using Activator.CreateInstance() method. There are two forms of Invoke() method :

If your method is not returning any value then you may use following form
Invoke ( obj , obj[])

If your method is returning some value and you want to trap it use following form :
obj = Invoke ( obj , bindingflag, binder , parameters, cultureflag )

For both the forms you pass instance of object on which the method will be called and array of objects that contains method parameters.

The second method shown here is with most commonly used values for BindingFlags, Binder and Culture. For more detailed information on the second syntax of Invoke method please refer MSDN.

Summary
Reflection allows a powerful mechanism to introspect your types. The reflection APIs can be found in System.Reflection namespace. The APIs allow you to inspect types as well as create types on the fly and invoke methods on them.



<< Home

Powered by Blogger

Google
WWW THIS BLOG