首页 > 代码库 > C#中反射的使用(How to use reflect in CSharp)(1)

C#中反射的使用(How to use reflect in CSharp)(1)

最近想做一个插件式的软件给公司的监控用,初步的想法是使用C#去反射Dll,Invoke其中的方法。此文仅供开发参考,不涉及原理,98%的代码以及2%的废话。

测试Solution是这么建的(.NET FRAMEWORK 4.5.1):

技术分享

Person类代码:

技术分享
 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4  5 namespace PersonMoudle 6 { 7     public class Person 8     { 9         public Person()10         {11             Name = "Sirius";12             Age = 25;13             Height = 172;14             Sex = "Middle";15         }16         public Person(string name, int age, float height, string sex)17         {18             Name = name;19             Age = age;20             Height = height;21             Sex = sex;22         }23 24         public string Name { get; set; }25         public int Age { get; set; }26         public float Height { get; set; }27         public string Sex { get; set; }28 29         public void Speak(string words)30         {31             Console.WriteLine(words);32         }33         private string GetMyName()34         {35             return Name.Trim();36         }37 38         public string GetMySex()39         {40             return Sex;41         }42 43         public List<string> BeenCity()44         {45             return new List<string>46             {47                 "Beijing",48                 "Jinan",49                 "NewYork"50             };51         }52 53         public List<string> BennCity(int count)54         {55             return new List<string>56             {57                 "Beijing",58                 "Jinan",59                 "NewYork"60             }.Take(count).ToList();61         }62     }63 }
View Code

 

控台中反射获取信息代码(控台引用类库项目,也可以load dll,后面的篇幅会写):

 1 using System; 2 using System.Linq; 3 using PersonMoudle; 4  5 namespace ReflectionTest 6 { 7     class Program 8     { 9         static void Main(string[] args)10         {11             var count = 0;//public count set;12 13             var person = new Person();14             var personType = person.GetType();15             Console.WriteLine("类型名: " + personType.Name);16             Console.WriteLine("类全名: " + personType.FullName);17             Console.WriteLine("命名空间名: " + personType.Namespace);18             Console.WriteLine("程序集名: " + personType.Assembly);19             Console.WriteLine("模块名: " + personType.Module.Name);20             Console.WriteLine("是否支持泛型: "+personType.IsConstructedGenericType);21 22             //获取构造函数信息23             var conInfo = personType.GetConstructors();24             Console.WriteLine(personType.FullName + "共有" + conInfo.Count() + "个构造函数");25             foreach (var constructorInfo in conInfo)26             {27                 count += 1;28                 Console.WriteLine("" + count + "个构造函数:");29                 var paramsInfo = constructorInfo.GetParameters();30                 Console.WriteLine(paramsInfo.Any() ? "参数列表:" : "无参数。");31                 foreach (var parameterInfo in paramsInfo)32                 {33                     Console.WriteLine("" + (parameterInfo.Position + 1) + "参数名:" + parameterInfo.Name + ", 参数类型:" +34                                       parameterInfo.ParameterType.FullName);35                 }36             }37             count = 0;38 39             //获取事件信息40             var eventInfo = personType.GetEvents();41             Console.WriteLine(personType.FullName+"共有"+eventInfo.Count()+"个事件");42 43             //获取字段信息44             var fields = personType.GetFields();45             Console.WriteLine(personType.FullName + " has " + fields.Count() + "fields.");46             foreach (var fieldInfo in fields)47             {48                 count += 1;49                 Console.WriteLine("The "+count+"st field is"+fieldInfo.Name+", type is "+fieldInfo.FieldType);   50             }51             count = 0;52 53             //获取接口信息54             var ifaces = personType.GetInterfaces();55             Console.WriteLine(personType.FullName + " has " + ifaces.Count() + " interfaces.");56             57 58             //获取成员信息59             var members = personType.GetMembers();60             Console.WriteLine(personType.FullName+" has "+members.Count()+" members.");61             foreach (var memberInfo in members)62             {63                 count += 1;64                 Console.WriteLine("The " + count + "st member name is " + memberInfo.Name + ", type is " +65                                   memberInfo.ReflectedType);66             }67             count = 0;68 69             //获取方法信息70             var methods = personType.GetMethods();71             Console.WriteLine(personType.FullName+" has "+methods.Count()+" methods.");72             foreach (var methodInfo in methods)73             {74                 count += 1;75                 var paramInfos = methodInfo.GetParameters();76                 Console.WriteLine("The " + count + "st method is " + methodInfo.Name + ", return type is " +77                                   methodInfo.ReturnType + ", param count is " + paramInfos.Count());78 79                 Console.WriteLine(paramInfos.Any() ? "参数列表:" : "无参数。");80                 foreach (var parameterInfo in paramInfos)81                 {82                     Console.WriteLine("param name:" + parameterInfo.Name + ", param type: " +83                                       parameterInfo.ParameterType);84                 }85             }86             count = 0;87 88             //propertites89             var pros = personType.GetProperties();90             Console.WriteLine(personType.FullName+" has "+pros.Count()+ "public properties.");91 92             Console.ReadKey();93         }94     }95 }

 

下一篇讲如何调用方法。

C#中反射的使用(How to use reflect in CSharp)(1)