Resolve how to use reflection to call the type member methods, fields, and attributes
Source: Internet
Author: User
As follows:
Copy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
Type t = typeof (Test );
Object result;
Test tc = new Test ();
Console. WriteLine ("Invoke a static method ");
T. InvokeMember ("Sayhello", BindingFlags. InvokeMethod | BindingFlags. Static | BindingFlags. Public, null, null, new object [] {});
Console. WriteLine ("------------------------");
Console. WriteLine ("Invoke a generic method ");
List <string> list = new List <string> ();
List. Add ("GuoHu ");
List. Add ("LeiHu ");
// We shoshould assign the parameter type to generic method By using MakeGenericMethod
MethodInfo mi = t. GetMethod ("Print"). MakeGenericMethod (typeof (string ));
Mi. Invoke (null, new object [] {list });
Console. WriteLine ("------------------------");
Console. WriteLine ("Invoke a instance method ");
MethodInfo m = t. GetMethod ("Swap ");
Object [] obj = new object [2];
Obj [0] = 123;
Obj [1] = 230;
M. Invoke (new Test (), obj );
Console. WriteLine ("{0}, {1}", obj [0], obj [1]);
Console. WriteLine ("------------------------");
Console. WriteLine ("output field name ");
FieldInfo [] fi = t. GetFields ();
Foreach (FieldInfo name in fi)
{
Console. WriteLine ("{0}", name );
}
Console. WriteLine ("------------------------");
Console. WriteLine ("Invoke a method with named parameters ");
Object [] argValues = new object [] {"Guo", "Hu "};
String [] argNames = new String [] {"lastName", "firstName "};
T. InvokeMember ("PrintName", BindingFlags. InvokeMethod, null, null, argValues, null, null, argNames );
Console. WriteLine ("------------------------");
Console. WriteLine ("Get a field value ");
Result = t. InvokeMember ("Name", BindingFlags. GetField | BindingFlags. GetProperty, null, tc, new object [] {});
Console. WriteLine ("Name = {0}", result );
Console. WriteLine ("------------------------");
Console. WriteLine ("Set a field value ");
T. InvokeMember ("Name", BindingFlags. SetField, null, tc, new object [] {"New value "});
Result = t. InvokeMember ("Name", BindingFlags. GetField | BindingFlags. GetProperty, null, tc, new object [] {});
Console. WriteLine ("Name = {0}", result );
}
}
Class Test
{
Public string Name;
Public Test ()
{
Name = "Initilize Name ";
}
Public static void Sayhello ()
{
Console. WriteLine ("Sayhello ");
}
Public static void Print <T> (IEnumerable <T> item)
{
Foreach (T t in item)
{
Console. WriteLine ("{0}", t );
}
}
Public static void PrintName (String firstName, String lastName)
{
Console. WriteLine ("{0}, {1}", lastName, firstName );
}
Public void Swap (ref int a, ref int B)
{
Int x =;
A = B;
B = x;
}
}
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.