解析如何使用反射調(diào)用類型成員 方法,字段,屬性
更新時(shí)間:2013年06月09日 08:50:54 作者:
本篇文章是對(duì)使用反射調(diào)用類型成員 方法,字段,屬性進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
如下所示:
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 should 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;
a = b;
b = x;
}
}
復(fù)制代碼 代碼如下:
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 should 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;
a = b;
b = x;
}
}
您可能感興趣的文章:
相關(guān)文章
C# TextBox多行文本框的字?jǐn)?shù)限制問題
最近在使用C# TextBox多行文本框的時(shí)候,發(fā)現(xiàn)了其對(duì)字?jǐn)?shù)限制的一點(diǎn)問題,所以總結(jié)下在使用C# TextBox多行文本框要注意的的字?jǐn)?shù)限制問題,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
C#中ExecuteNonQuery()返回值注意點(diǎn)分析
這篇文章主要介紹了C#中ExecuteNonQuery()返回值注意點(diǎn)分析,對(duì)于C#數(shù)據(jù)庫(kù)程序設(shè)計(jì)有很大的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
C#連接Oracle數(shù)據(jù)庫(kù)使用Oracle.ManagedDataAccess.dll
這篇文章主要介紹了C#使用Oracle.ManagedDataAccess.dll的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

