Getting started with reflection in [net] net (generate and call object member functions based on class name and function name)

Source: Internet
Author: User
Tags mscorlib

Http://blog.csdn.net/szwangdf/article/details/1504203

 

Outline:
1. What is reflection?
2. Relationships between namespaces and accessories
3. What is the purpose of obtaining type information during runtime?
4. How to Use reflection to obtain the type
5. How to dynamically create objects based on types
6. How to obtain methods and dynamic call Methods
7. dynamically create Delegation

1. What is reflection?
Reflection, which is translated into reflection in Chinese.
This is a method for obtaining runtime type information in. net. Net ApplicationsProgramIt consists of several parts: 'assembly ', 'module', and 'Type (class) '. Reflection provides a programming method, this allows programmers to obtain relevant information about these components during the running period, for example:

The Assembly class can obtain information about running accessories, dynamically load the accessories, find the type information in the accessories, and create instances of this type.
The type class can obtain the type information of an object. This information includes all elements of the object, such as methods, constructors, and attributes. The type class can obtain and call the information of these elements.
Methodinfo contains information about the method. You can use this class to obtain the name, parameter, and return value of the method and call it.
For example, fieldinfo and eventinfo are included in the system. Reflection namespace.

2. Relationships between namespaces and accessories
Many people may not be clear about this concept. It is necessary to clarify this concept for qualified. Net programmers.
The namespace is similar to a Java package, but it is not exactly the same, because the Java package must be placed according to the directory structure, and the namespace is not required.

The assemblies are the units where the .netapp executes. All the compiled .dllw..exe files are accessories.

the relationship between accessories and namespaces is not one-to-one, and they are not mutually exclusive. An Accessory can have multiple namespaces, a namespace can also exist in multiple accessories, which may be a bit vague. For example:
accessory:
namespace N1
{< br> public class AC1 {...}
public class ac2 {...}
}< br> namespace N2
{< br> public class AC3 {...}
public class ac4 {...}
}< br> accessory B:
namespace N1
{< br> public class BC1 {...}
public class BC2 {...}
}< br> namespace N2
{< br> public class bc3 {...}
public class bc4 {...}
}

Both of the accessories have two namespaces, N1 and N2, and each declares two classes. This is completely acceptable. Then we reference assembly a in an application, in this application, we can see that the classes below N1 are AC1 and ac2, And the classes below N2 are Ac3 and ac4.
Then we remove the reference to a and add the reference to B, then the classes under N1 that we can see in this application become BC1 and BC2, and the same is true under N2.
If we reference these two accessories at the same time, we can see the following four classes in N1: AC1, ac2, BC1, and BC2.

Here, we can clearly understand a concept. The namespace only indicates the type of the family, such as the Han nationality and Hui nationality, and the accessories indicate the type of the family, for example, if someone lives in Beijing or Shanghai, then there are Han people in Beijing, Hui people in Shanghai, Han people in Shanghai, and Hui people. This is not a conflict.

As we have mentioned above, accessories are a place where the types reside. To use a class in a program, you must tell the compiler where the class lives before the compiler can find it, that is to say, the accessory must be referenced.
So when you write a program, you may not be sure where the class is, but you only know its name, can you use it? The answer is yes. This is reflection, that is, to provide this type of Address while the program is running, and find it.
If you are interested, let's take a look.

3. What is the purpose of obtaining type information during runtime?
Some may doubt that, since it can be written during developmentCodeIt is not only tedious, but also inefficient.
This is a matter of opinion, just like early binding and late binding. Some people are opposed to late binding on the grounds of waste efficiency, but many people do not realize that they have used late binding when enjoying the benefits of virtual functions. This question is open. It's not clear in just a few words, so it's just a few minutes.
In my opinion, late binding can bring a lot of convenience in design. Proper use can greatly improve the reusability and flexibility of the program, but everything has two sides, it must be measured over and over again.

Next, what is the purpose of obtaining the type information during the runtime?
For example, many software developers prefer to leave some interfaces in their own software. Others can write some plug-ins to expand software functions. For example, I have a media player, I hope that the format of recognition can be easily extended in the future, so I declare an interface:
Public interface imediaformat
{
String extension {Get ;}
Decoder getdecoder ();
}
This interface contains an extension attribute, which returns a supported extension, and another method returns a decoder object (here I assume a decoder class, this class provides the function of decoding the file stream, which can be derived from the extension plug-in). I can interpret the file stream through the decoder object.
In this case, all decoding plug-ins must derive a decoder and implement this interface. In the getdecoder method, return the decoder object and configure its type name to my configuration file.
In this way, I do not need to know the type of the extended format when developing the player. I only need to obtain the type names of all decoder types from the configuration file, the dynamic creation of media format objects is used to convert them to the imediaformat interface.

This is a typical application of reflection.

4. How to Use reflection to obtain the type
First, let's look at how to obtain the type information.
There are two methods to obtain the type information. One is to obtain the instance object.
At this time, I only get this instance object. The method may be an object reference or an interface reference, but I don't know its exact type. I need to know, then you can call system. the GetType method declared on the object to obtain the type object of the Instance Object. For example, in a method, I need to determine whether the passed parameter has implemented an interface. If yes, then, a method of this interface is called:
...
Public void process (Object processobj)
{
Type T = processsobj. GetType ();
If (T. getinterface ("itest ")! = NULL)
...
}
...
Another method to obtain the type is to use the type. GetType and assembly. GetType methods, such:
Type T = type. GetType ("system. String ");
Note that we have mentioned the relationship between namespaces and accessories. To find a class, you must specify its accessories, or call GetType on the obtained Assembly instance.
In this Assembly, only the type name can be written, and the other exception is mscorlib. DLL, the type declared in this Assembly can also omit the Assembly name (. by default, mscorlib is referenced during the compilation of net accessories. DLL, unless it is explicitly specified during compilation), for example:
System. String is declared in mscorlib. dll. The above type T = type. GetType ("system. String") is correct.
System. Data. datatable is declared in system. Data. dll, so:
Type. GetType ("system. Data. able") can only get null references.
Required:
Type T = type. GetType ("system. Data. able, system. Data, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089 ");
In this way, you can read the following post:
Http://expert.csdn.net/Expert/topic/2210/2210762.xml? Temp =. 1919977.
The answer from qqchen is wonderful.

5. How to dynamically create objects based on types
System. activator provides methods to dynamically create objects based on types, such as creating a datatable:

Type T = type. GetType ("system. Data. able, system. Data, version = 1.0.3300.0, culture = neutral, publickeytoken = b77a5c561934e089 ");

Datatable table = (datatable) activator. createinstance (t );

Example 2: Create an object based on the parameter constructor
namespace testspace {
public class testclass
{< br> private string _ value;
Public testclass (string value) {
_ value = value;
}< BR >}< br>...
type T = type. getType ("testspace. testclass ");
object [] constructparms = new object [] {" Hello "}; // constructor parameter
testclass OBJ = (testclass) activator. createinstance (T, constructparms);
...
put the parameters in an object array in sequence

6. How to obtain methods and dynamic call methods
namespace testspace
{< br> public class testclass {
private string _ value;
Public testclass () {
}< br> Public testclass (string value) {
_ value = value;
}

Public String getvalue (string prefix ){
If (_ value = NULL)
Return "null ";
Else
Return prefix + ":" + _ value;
}

Public String Value {
Set {
_ Value = value;
}
Get {
If (_ value = NULL)
Return "null ";
Else
Return _ value;
}
}
}
}

The above is a simple class that contains a constructor with parameters, a getvalue method, and a value attribute. We can obtain and call the method by using the method name, for example:

// Obtain the type information
Type T = type. GetType ("testspace. testclass ");
// Parameters of the constructor
Object [] constuctparms = new object [] {"tietong "};
// Create an object based on the Type
Object dobj = activator. createinstance (T, constuctparms );
// Obtain method information
Methodinfo method = T. getmethod ("getvalue ");
// Some flag spaces of the call method. This field indicates public and instance method, which is also the default value.
Bindingflags flag = bindingflags. Public | bindingflags. instance;
// Parameters of the getvalue Method
Object [] parameters = new object [] {"hello "};
// Call a method and use an object to receive the returned value
Object returnvalue = method. Invoke (dobj, flag, type. defaultbinder, parameters, null );

Properties are similar to method calls. For details, refer to the msdn

7. dynamically create Delegation
Delegate is the basis for implementing events in C #. Sometimes it is inevitable to dynamically create a delegate. In fact, delegate is also a type: system. Delegate. All delegates are derived from this class.
System. Delegate provides some static methods to dynamically create a delegate, such as a delegate:

namespace testspace {
delegate string testdelegate (string value);
public class testclass {
Public testclass () {
}< br> Public void getvalue (string value) {
return value;
}< BR >}

Example:
Testclass OBJ = new testclass ();

// Obtain the type. In fact, you can use typeof to obtain the type.
Type T = type. GetType ("testspace. testclass ");
// Create a proxy, input the type, object for creating the proxy, and method name
Testdelegate method = (testdelegate) Delegate. createdelegate (T, OBJ, "getvalue ");

String returnvalue = method ("hello ");

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.