.Net 反射学习
Why?为什么使用反射
MVC ORM EF 都是用的反射。反射可以让程序的扩展性,灵活性得到加强。一起即可动态创建
what 反射原理
动态加载类库 ,先添加引用类库,或者复制debug里面的文件dll到运行文件Debug
Assembly assembly = Assembly.Load("WeiAi.DB.MysqlHelper");//反射生成类库 使用Assembly ,Load方法 里面填写namespace
foreach (var item in assembly.GetModules()) //遍历类库的dll文件
{
Console.WriteLine(item.Name);
}
foreach (var item in assembly.GetTypes())//遍历类库的类型
{
Console.WriteLine(item.Name);
foreach (var act in item.GetMethods())//遍历类的方法
{
Console.WriteLine(act.Name);
}
foreach (var item in assembly.GetModules()) //遍历类库的dll文件
{
Console.WriteLine(item.Name);
}
foreach (var item in assembly.GetTypes())//遍历类库的类型
{
Console.WriteLine(item.Name);
foreach (var act in item.GetMethods())//遍历类的方法
{
Console.WriteLine(act.Name);
}
}
反射创建对象并调用方法
Assembly assembly = Assembly.Load("WeiAi.DB.MysqlHelper"); //反射类库
Type type = assembly .GetType("WeiAi.DB.MysqlHelper.MySQLHelper");//反射类型
object obj = Activator.CreateInstance(type,null);//动态创建类的对象
ISQLHelper mysql = oMysql as ISQLHelper;// ISQLHelper 是创建的一个接口,MysqlHelper实现了这个接口,所以它类的对象可以转换
mysql.Query();//调用方法
创建不同的对象:
1、无参对象:
object obj = Activator.CreateInstance(type,null);
2、有参对象 int参数为例:
object obj = Activator.CreateInstance(type,new object[]{11});
3、创建泛型对象
Type genericType = assembly.CreateInstance("WeiAi.DB.MysqlHelper.GenericClass`3");//GenericClass为泛型类 有3个泛型,就写数字3
Type ty1 =genericType.MakeGenericType(new Type[]{typeof{int},typeof{string},typeof{int}});
object obj =Activator.CreateInstance(ty1,new object[]{11,"hello",123});//三个有参构造函数
object obj =Activator.CreateInstance(ty1,null);//无参构造函数
调用不同的方法:
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show1");//无参方法
method.Invoke(ob1, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show2", new Type[] { typeof(int) });//有一个参方法
method.Invoke(ob1, new object[] { 12 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });//有两个参方法
method.Invoke(ob1, new object[] { 12, "haha" });
}
{
//Type myType1 = assembly.GetType("WeiAi.DB.MysqlHelper.GenericMethod");
//object ob1 = Activator.CreateInstance(myType1);
//MethodInfo method = myType1.GetMethod("Show");
//MethodInfo methodNew= method.MakeGenericMethod(new Type[] { typeof(int),typeof(string),typeof(double) });
//methodNew.Invoke(ob1,new object[] { 12,"Jianghao",123456});
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show4");
MethodInfo methodNew = method.MakeGenericMethod(new Type[] { typeof(int) });//泛型方法
methodNew.Invoke(ob1, new object[] { 123123123 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show5");//静态方法
method.Invoke(null, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show6");//带返回值的方法
object result = method.Invoke(ob1, null);
Console.WriteLine(result);
}
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show1");//无参方法
method.Invoke(ob1, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show2", new Type[] { typeof(int) });//有一个参方法
method.Invoke(ob1, new object[] { 12 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show3", new Type[] { typeof(int), typeof(string) });//有两个参方法
method.Invoke(ob1, new object[] { 12, "haha" });
}
{
//Type myType1 = assembly.GetType("WeiAi.DB.MysqlHelper.GenericMethod");
//object ob1 = Activator.CreateInstance(myType1);
//MethodInfo method = myType1.GetMethod("Show");
//MethodInfo methodNew= method.MakeGenericMethod(new Type[] { typeof(int),typeof(string),typeof(double) });
//methodNew.Invoke(ob1,new object[] { 12,"Jianghao",123456});
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show4");
MethodInfo methodNew = method.MakeGenericMethod(new Type[] { typeof(int) });//泛型方法
methodNew.Invoke(ob1, new object[] { 123123123 });
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show5");//静态方法
method.Invoke(null, null);
}
{
object ob1 = Activator.CreateInstance(myType);
MethodInfo method = myType.GetMethod("Show6");//带返回值的方法
object result = method.Invoke(ob1, null);
Console.WriteLine(result);
}
反射类的属性和字段,并且动态赋值或者获取值
Type type = typeof(Person);
Person oPerson = (Person)Activator.CreateInstance(type);
oPerson.Id = 123;
oPerson.Name = "hai";
oPerson.Age = 18;
oPerson.Description = "1111111111111111";
// Person person = new Person();
foreach (var item in type.GetProperties())//属性
{
Console.WriteLine(item.Name);
Console.WriteLine(item.GetValue(oPerson));
if (item.Name.Equals("Id"))
{
item.SetValue(oPerson, 12);
}
if (item.Name.Equals("Name"))
{
item.SetValue(oPerson, "jianghao");
}
if (item.Name.Equals("Age"))
{
item.SetValue(oPerson, 22);
}
if (item.Name.Equals("Description"))
{
item.SetValue(oPerson, "hahahahaha");
}
Console.WriteLine(item.GetValue(oPerson));
}
foreach (var item in type.GetFields())//字段
{
Console.WriteLine(item.Name);
}
Person oPerson = (Person)Activator.CreateInstance(type);
oPerson.Id = 123;
oPerson.Name = "hai";
oPerson.Age = 18;
oPerson.Description = "1111111111111111";
// Person person = new Person();
foreach (var item in type.GetProperties())//属性
{
Console.WriteLine(item.Name);
Console.WriteLine(item.GetValue(oPerson));
if (item.Name.Equals("Id"))
{
item.SetValue(oPerson, 12);
}
if (item.Name.Equals("Name"))
{
item.SetValue(oPerson, "jianghao");
}
if (item.Name.Equals("Age"))
{
item.SetValue(oPerson, 22);
}
if (item.Name.Equals("Description"))
{
item.SetValue(oPerson, "hahahahaha");
}
Console.WriteLine(item.GetValue(oPerson));
}
foreach (var item in type.GetFields())//字段
{
Console.WriteLine(item.Name);
}