反射实例

实例一:数组

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

} } }}

相关文章
相关标签/搜索