C # uses the reflection mechanism to get class information [go]

Source: Internet
Author: User

Http://www.cnblogs.com/zhoufoxcn/archive/2006/10/31/2515873.html1. Creates a class instance dynamically with reflection and invokes its public member function. Create a new Class library project and add a Getsum method. Using System; namespace classlibrary1{   public class class1   {       Public Class1 ()       {     }       public int getsum (int x, int y)       {return x + y;      }   }} //Another project that references the generated classlibrary1.dll system.reflection.assembly a in the project System.Reflection.Assembly.LoadFrom ("ClassLibrary1.DLL");  system.type t = A.gettype ("Classlibrary1.class1");  //dynamically generates an instance of the Classlibrary1.class class object theobj = System.Activator.CreateInstance (t);  //parameter information, Getsum requires two int parameters system.type[] Paramtypes = new system.type[2];p aramtypes[0] = System.Type.GetType ("System.Int32"); PARAMTYPES[1] = System.Type.GetType ("System.Int32");  system.reflection.methodinfo mi = T.getmethod ("Getsum", Paramtypes);  //parameter value object[] Parameters = new Object[2];p arameters[0] = 3;parameters[1] = 4; object returnvalue = mi. Invoke (theobj, parameters);  console.writeline ("ClassLibrary1.Class1.GetSum (3, 4) returns: {0}", Returnvalue.tostring ()); 2. Use reflection to access the private members of the class. If it is C + +, we can calculate the position of the member within the object and then offset the pointer to access all nonpublic members of the type. But. NET objects are fully managed by the GC, the address is not available at all, and the method cannot be called through the pointer.
Of course... This is a highly inappropriate technique, and access to non-public members is likely to undermine the state of the object and cause unpredictable consequences. But anyway, use. NET's reflection mechanism can do.
For example, a class like this:
Class MyClass
{
private string Privatefield = "Private Field";
protected string Protectedfield = "protected Field";
private string _protectedproperty = "Protected property";
Protected string Protectedproperty
{
Get{return _protectedproperty;}
Set{_protectedproperty = value;}
}
private string _privateproperty = "Private Property";
private String Privateproperty
{
Get{return _privateproperty;}
Set{_privateproperty = value;}
}
protected void Protectedmethod ()
{
Console.WriteLine ("Protected Method invoked");
}
private void Privatemethod ()
{
Console.WriteLine ("Private Method invoked");
}
}
In addition to the default constructor, no member is public, but I still want to get and set the value of field and property, and call those two methods. The method is:
MyClass mc = new MyClass ();
Type t = typeof (MyClass);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic;

Fields
FieldInfo fi_protected = T.getfield ("Protectedfield", BF);
FieldInfo fi_private = T.getfield ("Privatefield", BF);

Console.WriteLine (fi_protected. GetValue (MC));
Console.WriteLine (fi_private. GetValue (MC));
Fi_private. SetValue (MC, "New Private Field");
Console.WriteLine (fi_private. GetValue (MC));

Console.WriteLine ();

Properties
PropertyInfo pi_protected = T.getproperty ("Protectedproperty", BF);
PropertyInfo pi_private = T.getproperty ("Privateproperty", BF);

Console.WriteLine (pi_protected. GetValue (Mc,null));
Console.WriteLine (pi_private. GetValue (Mc,null));
Pi_private. SetValue (MC, "New Private property", null);
Console.WriteLine (pi_private. GetValue (Mc,null));

Console.WriteLine ();

Methods
MethodInfo mi_protected = T.getmethod ("Protectedmethod", BF);
MethodInfo mi_private = T.getmethod ("Privatemethod", BF);

Mi_protected. Invoke (Mc,null);
Mi_private. Invoke (Mc,null);

Console.ReadLine ();

Output:
Protected Field
Private Field
New Private Field

Protected Property
Private Property
New Private Property

Protected Method invoked
Private Method invoked
event, as can be manipulated, EventInfo:-)

C # uses the reflection mechanism to get class information [go]

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.