C# 反射
来源:互联网 发布:家常菜谱软件下载 编辑:程序博客网 时间:2024/06/14 09:36
今天上课加深了一下反射的学习
首先构建一个工程写几个对象来做测试,可以多写几个
//具体内容(可以推出其他) //接口 interface IAirLine { string PrintBoardingCheck(); } //实现接口 class SouthAirLine : IAirLine { public string PrintBoardingCheck() { return "打印南航登记牌"; } }
之后
将工程生成dll文件,引用到反射的工程中
class Program { static void Main(string[] args) { Console.WriteLine("please input num"); RefectType ftype = new RefectType(); int i = Convert.ToInt32(Console.ReadLine()); Program pr = new Program(); string _select = pr.convertToClassName(i); Console.WriteLine("please input your name"); string pasName = Console.ReadLine(); string print = ftype.Test_Reflection_ByType(_select); Console.WriteLine(print); } public string convertToClassName(int index) { string className = ""; switch (index) { case 0: { className = "ChinaAirLine"; break; } case 1: { className = "EastAirLine"; break; } case 2: { className = "NorthAirLine"; break; } } return className; } } class RefectType { //无参数反射 当要反射的函数为无参时 public string Test_Reflection_ByType(string name) { //获取dll //引用导入 Assembly _tempAss = Assembly.Load("AirLine"); //路径导入 //Assembly _tempAss = Assembly.LoadFile(@"E:\AirLine.dll"); object _airLine = _tempAss.CreateInstance(string.Format("AirLine.{0}", name)); //动态获取到对象后,获取当前实例类型 Type _Temp_Type = _airLine.GetType(); object _tempString; //反射对象,无参数 _tempString = _Temp_Type.InvokeMember("PrintBoardingCheck", BindingFlags.InvokeMethod, null, _airLine, null); return _tempString.ToString(); } }
无参完成,那么接下来试试有参的
//具体内容(可以推出其他) //接口 interface IAirLine { string PrintBoardingCheck(string name); } //实现接口 class SouthAirLine : IAirLine { private string _name; public string Name { get => _name; set => _name = value; } public SouthAirLine(){} public SouthAirLine(string name) { this._name = name; } public string PrintBoardingCheck(string name) { string printInfo = "welcome" + name + "\r\n" + "please output southAirLine card"; return printInfo; } }
main函数中改一下
//main中将第一个print注释掉 string print = ftype.Test_Reflection_ByType_Parameter(_select,pasName);
添加个新反射方法
//有参反射 public string Test_Reflection_ByType_Parameter(string _temp,string PassengerName) { Assembly tempAss = Assembly.Load("AirLine"); object[] _params = new object[1]; _params[0] = PassengerName; object _obj = tempAss.CreateInstance(string.Format("AirLine.{0}", _temp), true, BindingFlags.Default, null, _params, null, null); Type _temp_Type = _obj.GetType(); object[] _method_params = new object[1]; _method_params[0] = PassengerName; object _tempString = _temp_Type.InvokeMember("PrintBoardingCheck", BindingFlags.InvokeMethod, null, _obj, _method_params); return _tempString.ToString(); }
大功告成
阅读全文
0 0
- C# 反射
- C#反射
- C# 反射
- C#反射
- C#反射
- C# 反射
- C#反射
- c#反射
- c#反射
- C#反射
- c# 反射
- C# 反射
- c# 反射
- C#反射
- C#反射
- C#反射
- C# 反射
- C#反射
- POJ3662-Telephone Lines
- opengles(一) hello world
- mongoose模糊查询+限制条件
- [PAT-乙级]1046.划拳
- 扩展ThreadPoolExecutor
- C# 反射
- 虚拟机Ubuntu下使用串口短信猫
- 数位DP小结
- 【shell】判断文件中是否存在字符串
- System.getProperty()参数大全
- 环境搭建,与入门
- windows cmd javac乱码
- 公共技术点之 Java 注解 Annotation
- python 变量命名规范