码迷,mamicode.com
首页 > Windows程序 > 详细

C# 反射

时间:2020-05-26 13:52:30      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:调用dll   eth   script   sem   people   short   foreach   加载   vat   

一.反射

1>反射的命名空间是System.Reflection

2>是.Net框架提供的帮助类库,读取并使用matedata

 

二.反射基本用法

举例如下

 1>Assembly assembly Assembly.Load("DB.MySql");//1.反射加载DLL (DLL名称是DB.MySql),用Assembly动态加载程序集

 2>Type type = assembly.GetType("DB.MySql.MySqlHelper");//2.获取类型信息(命名空间+类名 :DB.MySql+MySqlHelper)

 3>object oDBHelper = Activator.CreateInstance(type);//3. 用Activator创建对象

 4>IDBHelper iDBHelper = (IDBHelper)oDBHelper;//4 类型转换,把object对象oDBHelper,通过强制转换为接口IDBHelper

 5>iDBHelper.Query();//5 方法调用,传建好了接口对象iDBHelper,调用方法Query()

 

三.反射用在哪里

主流框架MVC,IOC,ORM和DOT模型都在用反射,反射最主要的特点就是“动态”

1>IOC(IOC=简单工厂+配置文件+反射)

-之前调用dll,都是先引用dll,添加命名空间,然后创建对象。现在可以动态加载dll,动态获取类型,动态创建对象。

IDBHelper iDBHeler = Factory.CreateHelper();//Factory工厂主要作用是创建对象
iDBHeler.Query();//可以调用对象的方法了,反射是动态的 依赖的是字符串

 1     /// <summary>
 2     /// 创建对象
 3     /// </summary>
 4     public  class Factory
 5     {
 6         private static string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelperConfig"];
 7         private static string DllName = IDBHelperConfig.Split(,)[1];
 8         private static string TypeName = IDBHelperConfig.Split(,)[0];
 9 
10 
11         public static IDBHelper CreateHelper()//1 2
12         {
13             Assembly assembly = Assembly.Load(DllName);//1 加载dll
14             Type type = assembly.GetType(TypeName);//2 获取类型信息
15             object oDBHelper = Activator.CreateInstance(type);//3 创建对象
16             IDBHelper iDBHelper = (IDBHelper)oDBHelper;//4 类型转换
17             return iDBHelper;
18         }
19     }

 

2>MVC(主要是通过反射,调用方法)

 MVC是通过反射,创建对象,然后通过方法名称,达到调用方法的目的

 1                     //MVC 类名称+方法名称 3                     Assembly assembly = Assembly.Load("DB.SqlServer");
 4                     Type type = assembly.GetType("DB.SqlServer.ReflectionTest");
 5                     object oReflectionTest = Activator.CreateInstance(type);
 6                     {
 7                         MethodInfo method = type.GetMethod("Show4", BindingFlags.Instance | BindingFlags.NonPublic);//Show4的访问修饰符是Private
 8                         method.Invoke(oReflectionTest, new object[] { "4" });
 9                     }
10                     {
11                         MethodInfo method = type.GetMethod("Show1");//调用无参数方法
12                         method.Invoke(oReflectionTest, null);
13                     }
14                     {
15                         MethodInfo method = type.GetMethod("Show2");//调用有参数方法(拿int举例)
16                         method.Invoke(oReflectionTest, new object[] { 123 });
17                     }
18                     {
19                         MethodInfo method = type.GetMethod("Show5");
20                         method.Invoke(oReflectionTest, new object[] { "麦田的稻草人" });//调用静态方法,Show5是静态方法,第一个object对象传入不传入都可以
21                         method.Invoke(null, new object[] { "果然" });
22                     }
23                     {
24                         MethodInfo method = type.GetMethod("Show3", new Type[] { });//调用重载方法,Show3是重载方法
25                         method.Invoke(oReflectionTest, new object[] { });
26                     }
27                     {
28                         MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(int) });
29                         method.Invoke(oReflectionTest, new object[] { 123 });
30                     }
31                     {
32                         MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string) });
33                         method.Invoke(oReflectionTest, new object[] { "Ant" });
34                     }
35                     {
36                         MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });
37                         method.Invoke(oReflectionTest, new object[] { 234, "W" });
38                     }
39                     {
40                         MethodInfo method = type.GetMethod("Show3", new Type[] { typeof(string), typeof(int) });
41                         method.Invoke(oReflectionTest, new object[] { "W", 234 });
42                     }

下面这个类,是反射测试类

  1     /// <summary>
  2     /// 反射测试类
  3     /// </summary>
  4     public class ReflectionTest
  5     {
  6         #region Identity
  7         /// <summary>
  8         /// 无参构造函数
  9         /// </summary>
 10         public ReflectionTest()
 11         {
 12             Console.WriteLine("这里是{0}无参数构造函数", this.GetType());
 13         }
 14 
 15         /// <summary>
 16         /// 带参数构造函数
 17         /// </summary>
 18         /// <param name="name"></param>
 19         public ReflectionTest(string name)
 20         {
 21             Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
 22         }
 23 
 24         public ReflectionTest(int id)
 25         {
 26             Console.WriteLine("这里是{0} 有参数构造函数", this.GetType());
 27         }
 28         #endregion
 29 
 30         #region Method
 31         /// <summary>
 32         /// 无参方法
 33         /// </summary>
 34         public void Show1()
 35         {
 36             Console.WriteLine("这里是{0}的Show1", this.GetType());
 37         }
 38         /// <summary>
 39         /// 有参数方法
 40         /// </summary>
 41         /// <param name="id"></param>
 42         public void Show2(int id)
 43         {
 44 
 45             Console.WriteLine("这里是{0}的Show2", this.GetType());
 46         }
 47         /// <summary>
 48         /// 重载方法之一
 49         /// </summary>
 50         /// <param name="id"></param>
 51         /// <param name="name"></param>
 52         public void Show3(int id, string name)
 53         {
 54             Console.WriteLine("这里是{0}的Show3", this.GetType());
 55         }
 56         /// <summary>
 57         /// 重载方法之二
 58         /// </summary>
 59         /// <param name="name"></param>
 60         /// <param name="id"></param>
 61         public void Show3(string name, int id)
 62         {
 63             Console.WriteLine("这里是{0}的Show3_2", this.GetType());
 64         }
 65         /// <summary>
 66         /// 重载方法之三
 67         /// </summary>
 68         /// <param name="id"></param>
 69         public void Show3(int id)
 70         {
 71 
 72             Console.WriteLine("这里是{0}的Show3_3", this.GetType());
 73         }
 74         /// <summary>
 75         /// 重载方法之四
 76         /// </summary>
 77         /// <param name="name"></param>
 78         public void Show3(string name)
 79         {
 80 
 81             Console.WriteLine("这里是{0}的Show3_4", this.GetType());
 82         }
 83         /// <summary>
 84         /// 重载方法之五
 85         /// </summary>
 86         public void Show3()
 87         {
 88 
 89             Console.WriteLine("这里是{0}的Show3_1", this.GetType());
 90         }
 91         /// <summary>
 92         /// 私有方法
 93         /// </summary>
 94         /// <param name="name"></param>
 95         private void Show4(string name)
 96         {
 97             Console.WriteLine("这里是{0}的Show4", this.GetType());
 98         }
 99         /// <summary>
100         /// 静态方法
101         /// </summary>
102         /// <param name="name"></param>
103         public static void Show5(string name)
104         {
105             Console.WriteLine("这里是{0}的Show5", typeof(ReflectionTest));
106         }
107         #endregion
108     }

 

3>ORM(通过反射,调用属性和字段或者设置属性的值)

 

 1                     People people = new People();
 2                     people.Id = 123;
 3                     people.Name = "Look";
 4                     people.Description = "二年级学生";
 5                     {
 6                         Type typePeople = typeof(People);
 7                         Type typePeopleDTO = typeof(PeopleDTO);
 8                         object peopleDTO = Activator.CreateInstance(typePeopleDTO);
 9                         foreach (var prop in typePeopleDTO.GetProperties())
10                         {
11                             object value = typePeople.GetProperty(prop.Name).GetValue(people);
12                             prop.SetValue(peopleDTO, value);
13                         }
14                         foreach (var filed in typePeopleDTO.GetFields())
15                         {
16                             object value = typePeople.GetField(filed.Name).GetValue(people);
17                             filed.SetValue(peopleDTO, value);
18                         }
19                     }

 

下面是people实体类

 1    /// <summary>
 2     /// 实体
 3     /// </summary>
 4     public class People
 5     {
 6         public People()
 7         {
 8             Console.WriteLine("{0}被创建", this.GetType().FullName);
 9         }
10 
11         public int Id { get; set; }
12         public string Name { get; set; }
13         public string Description;
14     }
15 
16     public class PeopleDTO
17     {
18         public PeopleDTO()
19         {
20             Console.WriteLine("{0}被创建", this.GetType().FullName);
21         }
22         public int Id { get; set; }
23         public string Name { get; set; }//ShortName  特性
24 
25         public string Description;
26     }
27 }

 

C# 反射

标签:调用dll   eth   script   sem   people   short   foreach   加载   vat   

原文地址:https://www.cnblogs.com/dfcq/p/12965055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!