反射(操作MetaData)

1.泛型类反射(类型泛型数和方法泛型数一致)

 public static void ShowGeneric()
        {
            Assembly a1 = Assembly.Load("ClassDemo1");//DLL
            Type type = a1.GetType("ClassDemo1.GenericTest`4");   //类型
            //创建一个 newType,这个新Type,充当Type 来使用
            Type newType = 
                type.MakeGenericType(new Type[] { typeof(Int32), typeof(DateTime), typeof(String), typeof(Int32) });
            object o1 = Activator.CreateInstance(newType);//创建实例
            MethodInfo method = newType.GetMethod("Show");
            method.Invoke(o1,new object[] {12,DateTime.Now, "12", 12 });
        }
先加载DLL → 获取类型Type → 再创建一个指定泛型的新Type → 再创建实例 → 用新Type创建方法 →  再调用方法
2.泛型类反射(类型泛型数少于方法泛型数)
 public static void _ShowGeneric()
        {
            Assembly assembly1 = Assembly.Load("ClassDemo1");
            Type type = assembly1.GetType("ClassDemo1.GenericTest`4");
            Type newType = type.MakeGenericType(new Type[] { typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32) });
            object o = Activator.CreateInstance(newType);
            MethodInfo method = newType.GetMethod("_Show");
            MethodInfo newMethod = method.MakeGenericMethod(new Type[] { typeof(string), typeof(string) });
            newMethod.Invoke(o, new object[] { 12,12,12,12, "12", "12" });
        }
先加载DLL → 获取类型Type → 再创建一个指定泛型的新Type → 再创建实例 → 用新Type创建方法 → 再创建一个指定泛型的新Method →  用新的method调用方法

3.获取属性,操作字段,获取字段
public static void ActionProperties()
        {
            Assembly assembly1 = Assembly.Load("ClassDemo1");
            Type type = assembly1.GetType("ClassDemo1.UserInfo");
            object o = Activator.CreateInstance(type);
            foreach (var item in type.GetProperties())
            {
                if (String.Equals(item.Name, "Id"))
                {
                    item.SetValue(o, 123);
                }
                if (String.Equals(item.Name, "Name"))
                {
                    item.SetValue(o, "");
                }
                if (String.Equals(item.Name, "Age"))
                {
                    item.SetValue(o, 27);
                }
                if (String.Equals(item.Name, "Date"))
                {
                    item.SetValue(o, DateTime.Now);
                }
                Console.WriteLine(item.GetValue(o));
            }
        }

4.获取字段,操作字段,获取字段

 public static void ActionFields()
        {
            Assembly assembly1 = Assembly.Load("ClassDemo1");
            Type type = assembly1.GetType("ClassDemo1.UserInfo");
            object o = Activator.CreateInstance(type);
            foreach (var item in type.GetFields())
            {
                if (String.Equals(item.Name, "InputDate"))
                {
                    item.SetValue(o, DateTime.Now);
                }
                Console.WriteLine(item.GetValue(o));
            }
        }

 

4.1 获取私有的字段

var item1 = type.GetField("JobName", BindingFlags.Instance | BindingFlags.NonPublic);

10.反射字段或属性的实践,两个对象字段的转移

public static void UseActionFields()
        {
            UserInfo user = new UserInfo()
            { 
                Id = 12,
                Name = "",
                Age = 27,
                Date = DateTime.Now,
                InputDate=DateTime.Now,
            };

            Assembly assembly1 = Assembly.Load("ClassDemo1");
            Type type = assembly1.GetType("ClassDemo1.UserInfo");
            object o = Activator.CreateInstance(type);
            foreach (var item in type.GetProperties())//属性
            {
                item.SetValue(o, typeof(UserInfo).GetProperty(item.Name).GetValue(user));//根据实体的Type获取具体属性的值
                Console.WriteLine(item.GetValue(o));//获取属性值
            }
            foreach (var item in type.GetFields())//字段
            {
                item.SetValue(o, typeof(UserInfo).GetField(item.Name).GetValue(user));//根据实体的Type获取具体字段的值
                Console.WriteLine(item.GetValue(o));//获取字段值
            }
        }

11.优点:「动态」

     确定:1,写起来复杂

      2,避开编译器检查

      3,性能问题;100W 性能差别可能有400 - 500 倍,但绝对值很小,绝大情况下不会影响性能,

            但可以空间换时间,缓存

            MVC,EF,都是第一次很慢,吧所有的公共方法加载到缓存,后面就很快






原文地址:https://www.cnblogs.com/Jacob-Wu/p/9291611.html