.net Reflection(反射)- 二
生活随笔
收集整理的這篇文章主要介紹了
.net Reflection(反射)- 二
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
反射 Reflection 中訪問方法?
新建一個ClassLibrary類庫:
public class Student{public string Name{ get; set; }public string School{ get; set; }public int Sum(int a, int b){return a + b;}public string GetName(){return "this is book" ;} }?
/// <summary>/// 反射/// </summary>class Program{static void Main(string[] args){Assembly assembly = Assembly.Load("ClassLibrary"); //調用 程序集 類的方法//創建該對象的實例,object類型,參數(名稱空間+類) object instances = assembly.CreateInstance("ClassLibrary.Student");//無參數 返回object value = type.GetMethod("GetName").Invoke(instances, null);//返回this is book//有參object[] params_obj = new object[2];params_obj[0] = 12; params_obj[1] = 23;object value1 = type.GetMethod("Sum").Invoke(instances, params_obj);//返回a b和}
還可以通過?FindInterfaces 方法返回 接口信息?
//獲取程序集接口 Stu 繼承的所有接口Type t = typeof(Stu);Type[] interfaces = t.FindInterfaces(TypeFilter, assembly);//顯示每個接口信息foreach (Type i in interfaces){//獲取映射接口方法的類型的方法InterfaceMapping mapping = t.GetInterfaceMap(i);for (int j = 0; j < mapping.InterfaceMethods.Length; j++){Console.WriteLine(" {0} 實現的 {1}", mapping.InterfaceMethods[j], mapping.TargetMethods[j]);}}
?
?
轉載于:https://www.cnblogs.com/dragon-L/p/3716302.html
總結
以上是生活随笔為你收集整理的.net Reflection(反射)- 二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 接口测试面试题
- 下一篇: 『Tableau』 地图显示不正确的一种