using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A14_Reflection
{
class Person
{
public string Name;
public int Age;
private int _ID;
public int ID
{
get{return _ID;}
set{_ID = value;}
}
public void DispalyID()
{
Console.WriteLine("ID = "+_ID);
}
public void DispalyName()
{
Console.WriteLine("Name = " + Name);
}
public void DispalyAge()
{
Console.WriteLine("Age = " + Age);
}
//调用方法测试
public void Test1()
{
Console.WriteLine("Person类Test1无参方法");
}
public void Test2(string str)
{
Console.WriteLine("Person类Test2无参方法,参数str = "+ str);
}
public void Test3()
{
Console.WriteLine("Person类Test3无参方法");
}
public void Test3(int num)
{
Console.WriteLine("Person类Test2无参方法,参数str = " + num);
}
private void DisplayPrivateMethod()
{
Console.WriteLine("Person类私有都方法调用");
}
}
}
/*
* 1.反射是通过程序,了解及调用“程序集”的相关属性,方法,字段
* 通过反射可读取程序集(*.exe *.dll)中代码的内容
* 可以根据(字符串)类名,来动态创建类的对象
* 可以动态获取对象中的方法、属性、字段
* 可以根据(字符串)方法名称来调用执行。
*
* 使用反射需要引入命名空间 System.Reflaction
* Type类实例可以获取类的名称、所属命名空间、是否为公共类以及字段、属性、方法等信息
*
*
* **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace A14_Reflection
{
class Demo1
{
public void Test1()
{
Person per = new Person();
Type tyPersonObj = per.GetType();
//显示类的名称
Console.WriteLine(tyPersonObj.Name);
//显示类所属的命名空间
Console.WriteLine(tyPersonObj.Namespace);
//显示类所属的程序集
Console.WriteLine(tyPersonObj.Assembly);
//获取类是否为公共,密封,私有
Console.WriteLine(tyPersonObj.IsPublic);
Console.WriteLine(tyPersonObj.IsSealed);
Console.WriteLine(tyPersonObj.IsPrimitive);
}
public void Test2()
{
Person per = new Person();
Type tyPersonObj = per.GetType();
Console.WriteLine("-----------------------------------------------");
//得到字段信息
FieldInfo[] fieArray = tyPersonObj.GetFields();
foreach (FieldInfo item in fieArray)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("-----------------------------------------------");
//得到方法信息
MethodInfo[] metArray = tyPersonObj.GetMethods();
foreach (MethodInfo item in metArray)
{
Console.WriteLine(item.Name);
}
Console.WriteLine("-----------------------------------------------");
//得到属性信息
PropertyInfo[] proArray = tyPersonObj.GetProperties();
foreach (PropertyInfo item in proArray)
{
Console.WriteLine(item.Name);
}
}
static void Main1(string[] args)
{
Demo1 obj = new Demo1();
obj.Test1();
obj.Test2();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace A14_Reflection
{
class Dome2
{
//通过程序集中的一个类的实例化,从而得到这个“程序集”(Assembly)的对象
public void Test1()
{
Person per = new Person();
//得到程序集
Assembly assObj = per.GetType().Assembly;
Console.WriteLine(assObj.FullName);
Console.WriteLine("---------------------------------------------");
Type[] typArray = assObj.GetTypes();
foreach (Type item in typArray)
{
Console.WriteLine(item.Name);
}
}
//使用路径的方式直接得到程序集
public void Test2()
{
Console.WriteLine("直接访问程序集的测试");
//通过路径的方式,直接的道程序集的对象
Assembly assObj = Assembly.LoadFrom(@"G:\System.dll");
Type[] typArray = assObj.GetTypes();
foreach (Type item in typArray)
{
Console.WriteLine(item.Name);
}
}
static void Main1(string[] args)
{
Dome2 obj = new Dome2();
obj.Test1();
obj.Test2();
}
}
}
/*
* 使用反射技术动态调用
* 缺点:运行速度慢
* 优点:可以使得类对象之间的调用关系,在运行其进行低偶合调用,
* 可以随着配置文件的改变,进行改变。
*
* ***/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace A14_Reflection
{
class Demo3
{
//通过反射调用方法和属性
public void Test1()
{
string strClassName = "Person";
string strMethodName = "Test1";
//的道程序集对象
Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
//得到type
Type typObj = assObj.GetType("A14_Reflection." + strClassName);
//创建对象
object obj = Activator.CreateInstance(typObj);
//得到方法
MethodInfo methInfo = typObj.GetMethod(strMethodName);
//调用无参方法
methInfo.Invoke(obj,null);
}
//调用有参方法
public void Test2()
{
string strClassName = "Person";
string strMethodName = "Test2";
//的道程序集对象
Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
//得到type
Type typObj = assObj.GetType("A14_Reflection." + strClassName);
//创建对象
object obj = Activator.CreateInstance(typObj);
//得到方法
MethodInfo methInfo = typObj.GetMethod(strMethodName);
//调用有参方法
object[] objArray = new object[1];
objArray[0] = "张三";
methInfo.Invoke(obj,objArray);
}
//调用重载方法
public void Test3()
{
string strClassName = "Person";
string strMethodName = "Test3";
//的道程序集对象
Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
//得到type
Type typObj = assObj.GetType("A14_Reflection." + strClassName);
//创建对象
object obj = Activator.CreateInstance(typObj);
//得到无参重载方法
MethodInfo methInfo = typObj.GetMethod(strMethodName,new Type[0]);
//调用无参重载方法
methInfo.Invoke(obj, null);
//得到有参重载方法
Type[] typArrayWithPara = { typeof(int) }; //确定方法重载中有参数
MethodInfo methInfo2 = typObj.GetMethod(strMethodName, typArrayWithPara);
//创建参数列表
object[] objArray = new object[1];
objArray[0] = 15;
//调用无参重载方法
methInfo2.Invoke(obj, objArray);
}
//演示私有方法
public void Test4()
{
string strClassName = "Person";
string strMethodName = "DisplayPrivateMethod";
//的道程序集对象
Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
//得到type
Type typObj = assObj.GetType("A14_Reflection." + strClassName);
//创建对象
object obj = Activator.CreateInstance(typObj);
//得到方法
MethodInfo methInfo = typObj.GetMethod(strMethodName, BindingFlags.NonPublic | BindingFlags.Instance);
//调用无参方法
methInfo.Invoke(obj,null);
}
//演示属性的调用
public void Test5()
{
string strClassName = "Person";
string stdrPropertyName = "ID"; //属性的名称
//的道程序集对象
Assembly assObj = Assembly.LoadFrom(@"G:\A14_Reflection.exe");
//得到type
Type typObj = assObj.GetType("A14_Reflection." + strClassName);
//创建对象
object obj = Activator.CreateInstance(typObj);
//得到属性
PropertyInfo propInfo = typObj.GetProperty(stdrPropertyName);
//设置属性
propInfo.SetValue(obj, 1000);
//读取属性的值
string strResult = propInfo.GetValue(obj, null).ToString();
Console.WriteLine(strResult);
}
static void Main1(string[] args)
{
Demo3 obj = new Demo3();
obj.Test1();
obj.Test2();
obj.Test3();
obj.Test4();
obj.Test5();
}
}
}
/*测试父类**/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A14_Reflection
{
public class Man
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A14_Reflection
{
public class ZhangSan:Man,ICanSpeak
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A14_Reflection
{
public class Dog
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A14_Reflection
{
interface ICanSpeak
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace A14_Reflection
{
class Demo4
{
//判断参数类是否为一个指定类的父类或接口
public void Test1()
{
Type typeMen = typeof(Man);
Type typeZahngSan = typeof(ZhangSan);
Type typeDog = typeof(Dog);
Type typeICanSpeak = typeof(ICanSpeak);
//判断父类
Console.WriteLine("类型Man是否为类型ZhangSan的父类:"+typeMen.IsAssignableFrom(typeZahngSan));
Console.WriteLine("类型Dog是否为类型ZhangSan的父类:" + typeDog.IsAssignableFrom(typeZahngSan));
//判断接口
Console.WriteLine("为类型ZhangSan是否实现了接口ICanSpeak:" + typeICanSpeak.IsAssignableFrom(typeZahngSan));
Console.WriteLine("为类型Dog是否实现了接口ICanSpeak:" + typeICanSpeak.IsAssignableFrom(typeDog));
}
public void Test2()
{
Type typeMen = typeof(Man);
Type typeZahngSan = typeof(ZhangSan);
ZhangSan zhangsanObj = new ZhangSan();
Type typeICanSpeak = typeof(ICanSpeak);
Dog dogObj = new Dog();
Man manObj = new Man();
//判断是否为实例
Console.WriteLine("menObj是否是类型Man的实例:" + typeMen.IsInstanceOfType(manObj));
Console.WriteLine("zhangsanObj是否是类型Man的实例:" + typeMen.IsInstanceOfType(zhangsanObj));
Console.WriteLine("dogObj是否是类型Man的实例:" + typeMen.IsInstanceOfType(dogObj));
//判断实例是否实现了接口
Console.WriteLine("实例zhangsanObj是否实现了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(zhangsanObj));
Console.WriteLine("实例dogObj是否实现了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(dogObj));
Console.WriteLine("实例manObj是否实现了接口ICanSpeak:" + typeICanSpeak.IsInstanceOfType(manObj));
}
//判断一个(参数)类是否为一个类的子类
public void Test3()
{
Type typeMen = typeof(Man);
Type typeZahngSan = typeof(ZhangSan);
Type typeDog = typeof(Dog);
//判断是否为实例
Console.WriteLine("类型ZhangSan是否为类型Man的子类:" + typeZahngSan.IsSubclassOf(typeMen));
Console.WriteLine("类型ZhangSan是否为类型Man的子类:" + typeDog.IsSubclassOf(typeMen));
}
public void Test4()
{
}
static void Main(string[] args)
{
Demo4 obj = new Demo4();
//obj.Test1();
//obj.Test2();
obj.Test3();
}
}
}