Reference: C # reflection instance (I) Use reflection to use a class library

Source: Internet
Author: User

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, of course, it is also the basic thing. After learning all this, you can learn the specific reflection plug-ins and other applications, so you don't have to read it.

// First, create a class library and generate it as HelloWorld. dll,

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace Webtest
{
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 "\ n this is the string printed by the class library function: Welcome," + s + "---" + B ;;
}

Public static string WriteName (string s)
{
Return "\ n this is the string printed by the class library function: Welcome," + s;
}

Public string WriteNoPara ()
{
Return "\ n this is the string printed by the class library function: you are using a non-parameter method ";
}

Private string WritePrivate ()
{
Return "\ n this is the string printed by the class library function: Private type method ";
}

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

}

// Then, create a project and introduce HelloWorld. dll,

Using System;
Using System. Threading;
Using System. Reflection;
Using Webtest. Webtest;

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

Static void Main ()
{
// Assembly t = Assembly. LoadFrom ("Webtest. dll"); // same effect as the following
Assembly t = Assembly. Load ("Webtest ");

Console. Write ("Webtest Class Library has the following classes: \ n ");
Foreach (Type aaa in t. GetTypes ())
{
Console. Write (aaa. Name + "\ n"); // display all classes under this dll
}

Module [] modules = t. GetModules ();
Console. WriteLine ();
Foreach (Module module in modules)
{
Console. WriteLine ("module Name: \ n" + module. Name); // display the module Name. This example is "HelloWorld. dll"
}

Type a = typeof (Webtest. Webtest. ReflectTest); // obtain the Type of the specific class and the following effect:
// Type a = t. GetType ("Webtest. ReflectTest ");
// Console. Write ("classes in the module include:" + a. Name + "\ n ");

String [] bb = {"aaaaa", "bbbbb "};
Object obj = Activator. CreateInstance (a, bb); // create an instance of this class. The bb following is a parameter with a constructor.
// Object obj = t. CreateInstance ("Webtest. ReflectTest"); // same as the preceding Method

// Obtain the set of common instance methods
MethodInfo [] miArr = a. GetMethods ();
Console. Write ("\ n common methods: \ n ");
Foreach (MethodInfo mi0 in miArr)
{
Console. Write (mi0.Name + "\ n"); // display all common methods
}

MethodInfo mi = a. GetMethod ("WriteString"); // display the specific method
Object [] aa = {"using non-static methods 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 );

// Obtain the set of public attributes of the Instance
Propertyinfo [] piarr = A. getproperties (bindingflags. instance | bindingflags. nonpublic | bindingflags. Public );
Console. writeline ("\ n show all attributes :");
Foreach (propertyinfo PI in piarr)
{
Console. writeline (PI. Name); // display all attributes
}

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 ));

// Obtain the set of instance public Constructor
ConstructorInfo [] ci1 = a. GetConstructors ();
Console. WriteLine ("\ n shows all constructors :");
Foreach (ConstructorInfo ci in ci1)
{
Console. Write (ci. ToString () + "\ n"); // obtain the constructor form
}

// Constructorinfo ASCI = A. getconstructor (New Type [] {typeof (string), typeof (string )});
// Console. Write (ASCI. tostring ());

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

Foreach (type TT in T. gettypes ())
{
If (TT. getinterface ("interface1 ")! = NULL)
{
Webtest. Webtest. interface1 obj3 = (Webtest. Webtest. interface1) Activator. CreateInstance ();
// Console. Write (obj3.add ());
}
}

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.

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.