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)
{
//通过类名称得到类型
//var assembly = Assembly.Load("Ewin.Client.Web");//参数为程序集的名称
//var oType = assembly.GetType("Ewin.Client.Web.Controllers." + strType);
//反射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();
}
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;
}