public class Person { private string address; private string email; public string Name { set; get; } public int Age { set; get; } public void SayHello() { Console.WriteLine("你好"); } public static string MystaticPro { set; get; } public static void MyStatic() { Console.WriteLine("我是static方法"); } }
通過反射dll得到Person類
static void Main(string[] args) { //反射dll var strDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dll\\ReflectorDLL.dll"); var oAssembly = Assembly.LoadFile(strDllPath); var lstTypes = oAssembly.GetTypes(); foreach (var oType in lstTypes) { if (oType.Name == "Person") { //默認得到類下面的所有public成員 var lstMembers = oType.GetMembers(); foreach (var oMem in lstMembers) { Console.WriteLine("GetMembers()方法得到的成員名稱:"+oMem.Name); } Console.WriteLine(""); //默認得到類下面的所有public屬性 var lstProperty = oType.GetProperties(); foreach (var oProp in lstProperty) { Console.WriteLine("GetProperties()方法得到的成員名稱:" + oProp.Name); } Console.WriteLine(""); //默認得到類下面的所有public字段 var lstField = oType.GetFields(); foreach (var oField in lstField) { Console.WriteLine("GetFields()方法得到的成員名稱:" + oField.Name); } } } Console.ReadKey(); }
得到結果 (2)反射對象的私有成員: 一般私有屬性的用法比較少,我們就以私有字段為例來說明,還是上面的例子:
static void Main(string[] args) { //反射dll var strDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dll\\ReflectorDLL.dll"); var oAssembly = Assembly.LoadFile(strDllPath); var lstTypes = oAssembly.GetTypes(); foreach (var oType in lstTypes) { if (oType.Name == "Person") { //默認得到類下面的所有public字段 var lstField = oType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (var oField in lstField) { Console.WriteLine("GetFields()方法得到的成員名稱:" + oField.Name); } } } Console.ReadKey(); }
(3)反射對象的靜態成員:
static void Main(string[] args) { //反射dll var strDllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dll\\ReflectorDLL.dll"); var oAssembly = Assembly.LoadFile(strDllPath); var lstTypes = oAssembly.GetTypes(); foreach (var oType in lstTypes) { if (oType.Name == "Person") { //默認得到類下面的所有public成員 var lstMembers = oType.GetMembers(BindingFlags.Public|BindingFlags.Static); foreach (var oMem in lstMembers) { Console.WriteLine("GetMembers()方法得到的成員名稱:" + oMem.Name); } Console.WriteLine(""); //默認得到類下面的所有public字段 var lstField = oType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance); foreach (var oField in lstField) { Console.WriteLine("GetFields()方法得到的成員名稱:" + oField.Name); } } } Console.ReadKey(); }
還有枚舉類型等等就不一一介紹了,基本上都是在BindingFlags這個上面做處理。 (4)反射得到對象以及對象的操作: 反射得到對象的方法主要有兩種
public static T GetModel<T>(T oModel) { var model = default(T) ; //得到對象的方法一: model = (T)typeof(T).GetConstructor(new System.Type[] { }).Invoke(new object[] { });//反射得到泛型類的實體 //得到對象的方法二: model = (T)Activator.CreateInstance(typeof(T)); //邏輯處理...... return model; }
對象屬性的取值和賦值:
//List集合轉換為DataTable public static DataTable ListFillTable(object obj) { if (!(obj is IList)) { return null; } var objlist = obj as IList; if (objlist == null || objlist.Count <= 0) { return null; } var tType = objlist[0]; DataTable dt = new DataTable(tType.GetType().Name); DataColumn column; DataRow row; System.Reflection.PropertyInfo[] myPropertyInfo = tType.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var t in objlist) { if (t == null) { continue; } row = dt.NewRow(); for (int i = 0, j = myPropertyInfo.Length; i < j; i++) { System.Reflection.PropertyInfo pi = myPropertyInfo[i]; string name = pi.Name; if (dt.Columns[name] == null) { var coltype = pi.PropertyType; if (coltype.Name == "Nullable`1") { //coltype = typeof(System.DBNull); column = new DataColumn(name); } else { column = new DataColumn(name, coltype); } dt.Columns.Add(column); } row[name] = pi.GetValue(t, null); } dt.Rows.Add(row); } return dt; }