Welcome to the amazing dot net programming

Author: Vijaya Kumar
Contact:

    

  

Get updates by e-mail

HP Computer Museum

 

 

 

 

free website submission search engine seo optimization

 

Powered by Blogger

November 17, 2006

Read Assembly Information Using Reflection

Reflection is ability to find information about types contained in an assembly at run time. Prior to .NET languages like C++ provided such ability in a limited sense. .NET provides a whole new set of APIs to introspect assemblies and objects. All the APIs related to reflection are located under System.Reflection namespace. .NET reflection is a powerful mechanism which not only allows you to inspect type information but also allows you to invoke methods on those types at runtime. Certain reflection APIs also allow creating of assembly in memory dynamically and use it in your code. In this article we will examine the basic and most commonly used features of reflection. Reflection APIs can be used to develop applications like class browsers, add-ons for development IDEs and inelegant editors.

Getting started

The first thing you should do while using reflection classes is to include System.Reflection namespace.

using System.Reflection;


Loading an assembly

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.


obtaining details about types from the assembly

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 array of System.Type objects. The method GetType returns a type object having details of specified object. Note that in our example Company is the namespace. In case your assembly do not contain any namespace you will simply write the type name.


Obtaining type details

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

All the property names are self-explanatory and need no separate explanation.


obtaining details about methods, properties and fields

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.


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 a=Assembly.LoadFrom("employee.dll");
Type t=a.GetType("Company.Employee");
MethodInfo getsalary=t.GetMethod("DisplayMsg");
object obj=Activator.CreateInstance(t);
object[] p=new object[1];
p[0]="Hello bipin";
getsalary.Invoke(obj,p);

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.

8 Comments:

At 2/14/2007 12:56:00 PM, Anonymous Anonymous said...

I will recommend you to all my friends!
penis enlargement pill
best penis enlargement pill
natural penis enlargement pill
penis enlargement pill review
do penis enlargement pill work
enlargement penis pills
buy penis pills
cheap penis enlargement pill
penis enlargement pill
penis enlargement pill product
herbal penis enlargement pill
cheapest penis enlargement pill
penis enlargement pills
buy penis enlargement pill
natural penis enlargement
penis enlargement pills online
natural penis enlargement pill
penis enlargement drug
penis enlargement natural
With best regards...

 
At 9/16/2007 11:48:00 PM, Anonymous viagra said...

The increase in the number of erectile dysfunction patients and the easy availability of the ED drug viagra in the online pharmacies have made it easy for many to order viagra online. There are certain online pharmacies that use a number of superlative degree adjectives and try and befool the customers. Buying Viagra online without proper inspection of the online pharmacy can have serious affects on your health and there are chances that your cheap Viagra never reaches your home. Whenever you buy viagra online it’s very mandatory to bear in mind that you go through a medical consultation and that the Viagra that you are given is FDA approved. Otherwise it would be wiser to buy viagra from a local known chemist.

 
At 12/18/2009 09:52:00 PM, Blogger sandra said...

Viagra is a prescription drug that takes care of the physical dilemma of erectile dysfunction in men. Sildenafil citrate is the major component of generic viagra pills that dilates the arteries in the penis and allows filling of blood in small spaces of penis that hardens and gets erect.

 
At 2/18/2010 10:17:00 PM, Blogger reggiewhite said...

Generic Cialis is a prescription drug, commonly used to help treat men having erectile dysfunction (ED) problems, also known as impotence.

 
At 4/09/2010 12:50:00 AM, Blogger johnnysmith said...

Generic Cialis is the buzzword prescribed for all patients suffering from impotence. Generic Levitra is an FDA-approved oral medication to treat erectile dysfunction (ED) in men.

 
At 4/22/2010 10:50:00 AM, Anonymous Anonymous said...

Generic Viagra is the world renowned medicine for erectile dysfunction and most men have forgotten they ever had a problem with erections after its use. Take it on empty stomach and it will work in 30 minutes. Erections can be achieved on stimulation.

 
At 5/15/2010 01:14:00 AM, Anonymous Anonymous said...

generic viagra
generic viagra
online pharmacy
buy viagra
buy viagra online
generic viagra
generic cialis
generic viagra
buy cialis

 
At 3/20/2011 10:48:00 PM, Blogger Vinny said...

Your web site is probably definitely one the very best . All round appearance of the page is probably eye-catching .
Wellington FL Locksmith

 

Post a Comment

<< Home

Google
 
Web dotnetlibrary.blogspot.com