反射實例

實例一:數組

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;spa

namespace ReflectionApplication
{
class Program
{
//Type t2 = Assembly.GetType("System.String");
static void Main(string[] args)
{
Type t = Type.GetType("ReflectionApplication.TestClass");
Object[] constructParms = new object[] { "hello" }; //構造器參數
TestClass obj = (TestClass)Activator.CreateInstance(t);
TestClass _obj = (TestClass)Activator.CreateInstance(t, constructParms);
obj.Value = "xiaoxiao";
Console.WriteLine(obj.Value);
Console.WriteLine(_obj.Value);
Console.ReadKey();
}
}orm

public class TestClass
{
private string _value;對象

public string Value
{
get { return _value; }
set { _value = value; }
}事件

public TestClass()
{
}
public TestClass(string value)
{
_value = value;
}
}
}ip

 實例二:get

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;string

namespace ReflectionApplication_1
{
class Program
{
static void Main(string[] args)
{
//獲取類型信息
Type t = Type.GetType("ReflectionApplication_1.TestClass");
//構造器的參數
object[] constuctParms = new object[] { "timmy" };
//根據類型建立對象
object dObj = Activator.CreateInstance(t, constuctParms);
//獲取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//調用方法的一些標誌位,這裏的含義是Public而且是實例方法,這也是默認的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的參數
object[] parameters = new object[] { "Hello" };
//調用方法,用一個object接收返回值
object returnValue = method.Invoke(dObj, flag, Type.DefaultBinder, parameters, null);
Console.WriteLine(returnValue);
Console.ReadKey();it

}
}io

public class TestClass
{
private string _value;
public TestClass()
{
}
public TestClass(string value)
{
_value = value;
}

public string GetValue(string prefix)
{
if (_value == null)
return "NULL";
else
return prefix + " : " + _value;
}

public string Value
{
set
{
_value = value;
}
get
{
if (_value == null)
return "NULL";
else
return _value;
}
}
}
}

 

實例三:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AssemblyApplication_1
{
public class Program
{
public static void Main(string[] args)
{
System.Type myType = System.Type.GetType("AssemblyApplication_1.Demo");//取得系統類型
object obj = Assembly.GetAssembly(myType).CreateInstance("AssemblyApplication_1.Demo");//建立實例
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
object[] params_1 = new object[] { "Rookie", 27 };
MethodInfo method = myType.GetMethod("PrintLine");//提取方法信息
//method.Invoke(obj, new object[] { "Rookie", 27 });//調用方法
method.Invoke(obj, flag, Type.DefaultBinder, params_1, null);

method = myType.GetMethod("PrintLine2");//提取另一個方法,實際應用中是根據不一樣版本取得同一個方法,而構造不一樣參數數組
method.Invoke(obj, new object[] { "Rookie", 27, "Rookie personal information." });//調用方法
Console.ReadKey();
}
}
public class Demo
{
public Demo()
{
}
//實際應用中老版本的方法
public void PrintLine(string name, int age)
{
System.Console.WriteLine("Name = " + name + "; Age = " + age.ToString());
}

//實際應用中升級版本的方法(名稱相同,只是參數個數不一樣)
public void PrintLine2(string name, int age, string description)
{
System.Console.WriteLine("Name = " + name + "; Age = " + age.ToString() + "; Description = " + description);
}
}
}

實例四:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace AssemblyApplication
{
class Program
{
static void Main(string[] args)
{
Program reflect = new Program();//定義一個新的自身類
//調用一個reflecting.exe程序集
Assembly myAssembly = Assembly.LoadFrom("AssemblyApplication.exe");
reflect.getreflectioninfo(myAssembly);//獲取反射信息
Console.ReadKey();
}
//定義一個獲取反射內容的方法
void getreflectioninfo(Assembly myassembly)
{
Type[] typearr = myassembly.GetTypes();//獲取類型
foreach (Type type in typearr)//針對每一個類型獲取詳細信息f
{
//獲取類型的結構信息
ConstructorInfo[] myconstructors = type.GetConstructors();
//獲取類型的字段信息
FieldInfo[] myfields = type.GetFields();
//獲取方法信息
MethodInfo[] myMethodInfo = type.GetMethods();
//獲取屬性信息
PropertyInfo[] myproperties = type.GetProperties();
//獲取事件信息
EventInfo[] Myevents = type.GetEvents();

} } }}

相關文章
相關標籤/搜索