Reflection learning 2-Use of reflection

Source: Internet
Author: User

 

C # How to Use reflection. I don't know if it's the most comprehensive collection.
I found a lot of information on the Internet. It can be said that it is similar. I searched a bunch of conceptual things on the Internet. Today I sorted out the reflection items for your use, I promise that I am the most comprehensive thing here, and of course it is also the basic thing. After learning all this, you can learn the specific reflection plug-ins and other applications.

You don't have to read it. First, create a class library and generate it as helloworld. dll,

 

Using system;

Namespace webtest
{

Public interface interface1
{
Int add ();

}
Public class reflecttest: interface1
{

Public String write;
Private string writec;

Public String writea
{
Get
{
Return write;
}
Set
{
Write = value;
}

}

Private string writeb
{
Get
{
Return writec;
}
Set
{
Writec = value;
}

}

Public reflecttest ()
{
This. Write = "write ";
This. writec = "writec ";
}

Public reflecttest (string str1, string str2)
{
This. Write = str1;
This. writec = str2;

}

Public String writestring (string S, int B)
{
Return "Welcome," + S + "---" + B ;;
}

Public static string writename (string S)
{
Return "Welcome," + S;
}

Public String writenopara ()
{
Return "you are using a non-parameter method ";
}

Private string writeprivate ()
{
Return "Private method ";
}

 

Public int add ()
{
Return 5;
}
}
}

Then, create a project and introduce helloworld. dll.

Using system;

Using system. Threading;
Using system. reflection;

Class Test
{
Delegate string testdelegate (string value, int value1 );

Static void main ()
{
// Assembly T = assembly. loadfrom ("helloworld. dll"); same effect as below
Assembly T = assembly. Load ("helloworld ");

Foreach (type AAA in T. gettypes ())
{
// Console. Write (AAA. Name); // display all classes under the DLL
}

Module [] modules = T. getmodules ();

Foreach (module in modules)
{
// Display the module name. In this example, "helloworld. dll" is used"
// Console. writeline ("module name:" + module. Name );
}


// Obtain the type of the specific class and the following effect

Type A = typeof (webtest. reflecttest );

// Type A = T. GetType ("webtest. reflecttest ");//
// Console. Write (A. Name );

String [] BB =... {"aaaa", "bbbbb "};

// Create an instance of this class, And the BB following it is a parameter with a constructor
Object OBJ = activator. createinstance (A, BB );
// Object OBJ = T. createinstance ("webtest. reflecttest ");

 

Methodinfo [] miarr = A. getmethods ();
Foreach (methodinfo mi0 in miarr)
{
// Console. Write (mi0.name); // display all common methods
}

Methodinfo MI = A. getmethod ("writestring"); // display the specific method
Object [] AA =... {"using a non-static method with Parameters", 2 };
String S = (string) MI. Invoke (OBJ, AA); // call a method with Parameters

Methodinfo Mi1 = A. getmethod ("writename ");
String [] aa1 =... {"static method used "};
String S1 = (string) mi1.invoke (null, aa1); // call a static method

Methodinfo mi2 = A. getmethod ("writenopara ");
String S2 = (string) mi2.invoke (OBJ, null); // call a method without Parameters

Methodinfo MI3 = A. getmethod ("writeprivate", bindingflags. instance |

Bindingflags. nonpublic );
String S3 = (string) mi3.invoke (OBJ, null); // call a private Method

 

// Console. Write (S3 );

Propertyinfo [] piarr = A. getproperties (bindingflags. Instance

| Bindingflags. nonpublic | bindingflags. Public );
Foreach (propertyinfo PI in piarr)
{
// Console. Write (PI. Name); // display all methods
}

Propertyinfo PI1 = A. getproperty ("writea ");
// Pi1.setvalue (OBJ, "writea", null );
// Console. Write (pi1.getvalue (OBJ, null ));

Propertyinfo Pi2 = A. getproperty ("writeb ",

Bindingflags. instance | bindingflags. nonpublic | bindingflags. Public );
Pi2.setvalue (OBJ, "writeb", null );
// Console. Write (pi2.getvalue (OBJ, null ));

Fieldinfo fi1 = A. getfield ("write ");
// Console. Write (fi1.getvalue (OBJ ));

Constructorinfo [] CI1 = A. getconstructors ();
Foreach (constructorinfo Ci In CI1)
{
// Console. Write (CI. tostring (); // obtain the constructor form
}

Constructorinfo ASCI = A. getconstructor (New Type []... {

Typeof (string), typeof (string )});
// Console. Write (ASCI. tostring ());

Webtest. interface1 obj1 =

(Webtest. interface1) T. createinstance ("webtest. reflecttest ");
Webtest. reflecttest obj2 =

(Webtest. reflecttest) T. createinstance ("webtest. reflecttest ");
// Console. Write (obj1.add (); typical factory Mode

foreach (type TT in T. gettypes ()
{< br> If (TT. getinterface ("interface1 ")! = NULL)
{< br> webtest. interface1 obj3 = (webtest. interface1) activator. createinstance (a);
// console. write (obj3.add ();
}< BR >}

Testdelegate method = (testdelegate) Delegate. createdelegate (typeof (testdelegate), OBJ,

"Writestring ");
// A simple example of dynamically creating a delegate
Console. Write (method ("str1", 2 ));
Console. Read ();
}
}

 

Here, I have sorted out all the commonly used methods, attributes, and so on. Don't let everyone go about it. Let's just calm down and separate parts by myself, make sure that your reflection learning will get twice the result with half the effort. of course, I will continue to add the method. If you think about it, write it down first.

From: http://blog.csdn.net/lee576/archive/2009/01/08/3737103.aspx

 

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.