Introduction to. NET Reflection

Source: Internet
Author: User
Tags array new set reflection
Introduction
Reflection is ability to find information about types contained in a 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 of the APIs related to reflection are located under System.Reflection. . NET reflection is a powerful mechanism which-only allows-to-inspect type information but also allows to Invok E 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 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 editor S.

. NET Assemblies
Assemblies are the building blocks of any. NET application. All functionality The. 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
The the thing you should doing while using reflection classes are to include System.Reflection namespace.

Using System.Reflection;
Loading an assembly
Before obtaining any information about types contained at an assembly we must the the assembly.

Assembly myassembly = Assembly.LoadFrom ("Employee.dll");
This is statement loads an assembly called Employee.dll. Can substitute your own path here. Assembly class has a static method called LoadFrom this loads the specified Assembly in memory. The method returns an instance of assembly class itself.

Obtaining details about types from the assembly
The next 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 a assay of System.Type objects. The method GetType returns a type object has details of specified object. Note this in our example namespace. In case your assembly does not contain any namespace your would 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 of 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 the above 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 This can also get information about specific method, the 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. 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 the assembly. Reflection also allows us to create instances of this 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 obtained type of employee class.  We then created an instance the IT using Activator.CreateInstance () method. There are two forms of Invoke () method:

If your method isn't returning any value then your may use following form
Invoke (obj, obj[])
If your method was returing some value and you want to trap it use following form:
obj = Invoke (obj, Bindingflag, binder, parameters, Cultureflag)
For both the forms with instance of object on which the method would be called and array of objects that contains Metho D parameters.

The second method shown we are 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. The reflection APIs can be found in System.Reflection namespace. The APIs allow you to inspect types as the AS-as-create types on the fly and invoke methods on them.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.