a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

解析如何使用反射調(diào)用類型成員 方法,字段,屬性

2020-01-24 03:13:44
字體:
供稿:網(wǎng)友
如下所示:
復(fù)制代碼 代碼如下:

    class Program
    {
        static void Main(string[] args)
        {
            Type t = typeof(Test);
            object result;
            Test tc =new Test();
            Console.WriteLine("Invoke a static method");
            t.InvokeMember("Sayhello",BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, new object[] { });
            Console.WriteLine("------------------------");
            Console.WriteLine("Invoke a generic method");
            List<string> list = new List<string>();
            list.Add("GuoHu");
            list.Add("LeiHu");
            //We should assign the parameter type to generic method By using MakeGenericMethod
            MethodInfo mi = t.GetMethod("Print").MakeGenericMethod(typeof(string));
            mi.Invoke(null, new object[] { list });
            Console.WriteLine("------------------------");
            Console.WriteLine("Invoke a instance method");
            MethodInfo m = t.GetMethod("Swap");
            object[] obj = new object[2];
            obj[0] = 123;
            obj[1] = 230;
            m.Invoke(new Test(), obj);
            Console.WriteLine("{0},{1}", obj[0], obj[1]);
            Console.WriteLine("------------------------");
            Console.WriteLine("output field name");
            FieldInfo[] fi = t.GetFields();
            foreach (FieldInfo name in fi)
            {
                Console.WriteLine("{0}",name);
            }
            Console.WriteLine("------------------------");
            Console.WriteLine("Invoke a method with named parameters");
            object[] argValues = new object[] { "Guo", "Hu" };
            String[] argNames = new String[] { "lastName", "firstName" };
            t.InvokeMember("PrintName", BindingFlags.InvokeMethod, null, null, argValues, null, null, argNames);
            Console.WriteLine("------------------------");
            Console.WriteLine("Get a field value");
            result = t.InvokeMember("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, tc, new object[] { });
            Console.WriteLine("Name == {0}", result);
            Console.WriteLine("------------------------");
            Console.WriteLine("Set a field value");
            t.InvokeMember("Name", BindingFlags.SetField, null, tc, new object[] { "New value" });
            result = t.InvokeMember("Name", BindingFlags.GetField | BindingFlags.GetProperty, null, tc, new object[] { });
            Console.WriteLine("Name == {0}",result);
        }
    }
    class Test
    {
        public string Name;
        public Test()
        {
            Name = "Initilize Name";
        }
        public static void Sayhello()
        {
            Console.WriteLine("Sayhello");
        }
        public static void Print<T>(IEnumerable<T> item)
        {
            foreach (T t in item)
            {
                Console.WriteLine("{0}", t);
            }
        }
        public static void PrintName(String firstName, String lastName)
        {
            Console.WriteLine("{0},{1}", lastName, firstName);
        }
        public void Swap(ref int a, ref int b)
        {
            int x = a;
            a = b;
            b = x;
        }
    }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产亚洲一区在线 | 日日夜夜精品免费视频 | 中文二区| 午夜免费在线 | www四虎 | 日韩精品 电影一区 亚洲 | 欧美日韩综合精品 | av一区二区三区 | 91精品一区二区三区在线观看 | 九色在线 | 天堂视频在线 | 免看一级一片 | 精品一区二区三区三区 | 中文字幕一区二区三区四区五区 | 亚洲视频中文字幕 | 久久午夜激情 | 四虎成人在线视频 | 国产97久久 | 99久久综合国产精品二区 | 黄色毛片免费看 | 午夜日韩| 九九热免费精品视频 | 精品在线播放 | 欧美一区二区三区免费 | 国产精品久久久久久久久久久久久 | 亚洲午夜精品一区二区三区他趣 | 久久这 | 色欧美片视频在线观看 | 99精品99 | 日本视频一区二区三区 | av在线免费观看一区二区 | 青青草精品视频 | 成人av网站在线观看 | 嫩草影院网站入口 | 羞羞网站在线观看 | 欧美成人在线免费视频 | 黄色毛片在线 | 视频一区在线播放 | 麻豆免费短视频 | 91日日| 亚洲一区 |