C#中的反射(System.Reflection)

这篇文章介绍了C#中的反射(System.Reflection),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、获取程序集Assembly

1、获取当前运行的程序集

System.Reflection.Assembly[] asm = AppDomain.CurrentDomain.GetAssemblies(); // Assembly b = Assembly.GetExecutingAssembly();

2、获取指定文件的程序集:Load,LoadFrom,LoadFile方法。

Assembly c = Assembly.Load("mscorlib.dll");//如果你引用了程序及,那么就直接Load()方法,参数里面程序集名称就可以加载了。Assembly c = Assembly.Load("mscorlib"); Assembly d = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "mscorlib.dll");//LoadFrom只能用于加载不同标识的程序集, 也就是唯一的程序集, 不能用于加载标识相同但路径不同的程序集。 Assembly e = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "mscorlib.dll");//LoadFile:只加载指定文件,但不会自动加载依赖程序集

二、获取类型Type (指Class类):

Assembly asm = Assembly.GetExecutingAssembly(); Type[] tArr = asm.GetExportedTypes();//获取程序集中定义的公共类型

1、从类字符串中获得Type对象:Assembly.GetType(“”),Module.GetType(“”), Type.GetType(“”)

Assembly ass = Assembly.LoadFrom(@"C:\bin\Debug\ConsoleApplication2.exe"); Console.WriteLine(ass.GetType("ConsoleApplication2.Person").ToString());    //根据程序集(dll或exe)获取里面的Class Module mod = ass.GetModules()[0]; Console.WriteLine(mod.GetType("ConsoleApplication2.Person").ToString()); Type type = Type.GetType("System.Int32");//静态方法,参数为完全限定名(首选) Type type = Type.GetType("MyAssembly.Example",false,true)   //注意0是类名,参数1表示若找不到对应类时是否抛出异常,参数2表示类名是否区分大小写

2、从具体类中获得Type对象:typeof运算符

Type t4 = typeof(TestSpace.TestClass);//使用typeof运算符

3、从实例中获得Type对象:Object.GetType()

Example example = new Example(); Type type = example.GetType(); Type t3 = 42.GetType();//根据对象实例获取类型

4、Type的属性

t.IsPublic; t.IsAbstract; t.IsClass; t.IsValueType;

三、获取成员MemberInfo

MemberInfo[] miArr = t.GetMembers(BindingFlags.Instance | BindingFlags.Public);//实例与公共成员。还有BindingFlags.Static|BindingFlags.NonPublic foreach (MemberInfo item in miArr) { bool a = item is FieldInfo; PropertyInfo; MethodBase; ConstructorInfo; MethodInfo; EventInfo; Type; } t.GetConstructor();//获取构造函数 t.GetFields();//获取字段 t.GetProperties(); //获取属性 t.GetMethods();//获取方法 t.GetEvents();//获取事件 t.GetInterfaces();//获取接口 t.GetCustomAttributes(true);//获取类型上标记的自定义属性

在System.Reflection命名空间内包含多个反射常用的类:

  • Assembly: 通过此类可以加载操纵一个程序集,并获取程序集内部信息
  • EventInfo: 该类保存给定的事件信息
  • FieldInfo :该类保存给定的字段信息
  • MethodInfo :该类保存给定的方法信息
  • MemberInfo :该类是一个基类,它定义了EventInfo、FieldInfo、MethodInfo、PropertyInfo的多个公用行为
  • Module :该类可以使你能访问多个程序集中的给定模块
  • ParameterInfo :该类保存给定的参数信息
  • PropertyInfo: 该类保存给定的属性信息

四、获取具体成员

Type t = Assembly.GetExecutingAssembly().GetType("TestSpace.TestClass"); MethodInfo m = t.GetMethod("TestMethod"); ParameterInfo[] p = m.GetParameters();//获取方法参数

五、创建实例

1、根据Assembly创建类型实例:asm.CreateInstance()

Assembly asm = Assembly.GetExecutingAssembly(); TestClass obj = asm.CreateInstance("TestSpace.TestClass");//根据Assembly创建类型实例

2、根据类型创建实例:Activator.CreateInstance()

Type t = Type.GetType("TestSpace.TestClass"); TestClass obj = (TestClass)Activator.CreateInstance(t);//1、根据类型创建实例 TestClass obj = (TestClass)Activator.CreateInstance(t, new object[] { "aa" });// 2、根据”有参数的构造函数”创建实例 // TestClass obj = (TestClass)t.InvokeMember("TestClass", BindingFlags.CreateInstance, null, null, null);

六、调用方法

1、调用实例方法:Invoke

MethodInfo m = t.GetMethod("WriteString"); object returnValue = m.Invoke(obj, new object[] { "test", 1 });//传两参数,若方法无参数,可以将Invoke的第二个参数设为null //或者 object returnValue = m.Invoke(obj, BindingFlags.Public, Type.DefaultBinder, new object[] { "test", 1 }, null);//最后一个参数表示Culture.

2、调用静态方法

MethodInfo m = t.GetMethod("StaticMethod"); object returnValue = m.Invoke(null, new object[] { "test", 1 });

七、反射属性

通过System.Reflection.Property能查找到类里面的属性。 常用的方法有GetValue(object,object[])获取属性值和SetValue(object,object,object[])设置属性值

PropertyInfo propertyName = type.GetProperty("Name");    //获取Name属性对象 propertyName.SetValue(obj, "张飞", null);                //设置Name属性的值 object objName = propertyName.GetValue(obj, null);  //获取属性值

根据属性的类型设置属性的值

Type type = typeof(Person);      //注意要输入全部路径,包括命名空间 object obj = Activator.CreateInstance(type); //假设这是存在于XML的数据 Dictionary dic = new Dictionary(); dic.Add("Id", "1"); dic.Add("Name", "神灵武士"); dic.Add("Birthday", "2001-01-01"); PropertyInfo[] ProArr = type.GetProperties(); foreach (PropertyInfo p in ProArr) { if (dic.Keys.Contains(p.Name)) { p.SetValue(obj, Convert.ChangeType(dic[p.Name], p.PropertyType), null);  //当需要给属性设置不同类型的值时 } } Person person = obj as Person; Console.WriteLine(person.Birthday);

八、反射特性

通过System.Reflection.MemberInfo的GetCustomAttributes(Type,bool)就可反射出一个类里面的特性。

Assembly assembly = Assembly.Load("fanshe"); Type type = assembly.GetType("fanshe.Person");      //注意要输入全部路径,包括命名空间 object obj = Activator.CreateInstance(type); object[] typeAttributes = type.GetCustomAttributes(false);       //获取Person类的特性 foreach (object attribute in typeAttributes) { Console.WriteLine(attribute.ToString());    //输出 System.SerializableAttribute   因为我在Person上里加了个[Serializable] }

九、创建委托实例

TestDelegate myDelegate = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelete), obj, "MyMethod"); string returnValue = myDelegate("Hello");//执行委托

十、应用举例

1、动态加载程序集

Assembly asm = Assembly.LoadFrom(@"E:\Test.dll"); Type type = asm.GetType("TestSpace.TestClass"); object obj = System.Activator.CreateInstance(type);//也可以使用强制转换,将obj转换为预定义的接口或者抽象类(如Form),直接执行基方法,不用反射GetMethod . MethodInfo m = type.GetMethod("WriteString"); m.Invoke(obj, new object[] { "test" });

2、获得List中的T类型:

List dogs = new List(); Type type = dogs.GetType(); if (type.IsGenericType) {   Type[] genericArgTypes = type.GetGenericArguments();   if (genericArgTypes[0] == typeof(Dog))   {   //你想要判断的是这个吗?   } }

到此这篇关于C#反射(Reflection)的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是C#中的反射(System.Reflection)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程