欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解析如何使用反射调用类型成员 方法,字段,属性

程序员文章站 2023-12-19 17:59:16
如下所示:复制代码 代码如下:    class program    {   &...
如下所示:
复制代码 代码如下:

    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;
        }
    }

上一篇:

下一篇: