码迷,mamicode.com
首页 > 其他好文 > 详细

反射基础

时间:2018-11-23 18:36:08      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:this   flags   tty   return   基础   arp   lag   odi   测试   

    /// <summary>
    /// 测试反射模型
    /// </summary>
    public class TestFunction
    {
        public TestFunction() { }
        public TestFunction(string param)
        {
            this.Param = param;
        }
        public string Param { get; set; }

        public string Function(string fParam)
        {
            return this.Param + "-" + fParam;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            TestReflection(new TestFunction());
            Console.Read();
        }

        public static void TestReflection(object obj)
        {
            //obj类的类型
            var t = obj.GetType();
//字段集GetProperties参数可指定字段修饰符 var field = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); //构造函数的参数 object[] constuctParms = new object[] { "构造函数的值" }; //根据类型创建对象 object dObj = Activator.CreateInstance(t, constuctParms); //获取方法的信息 MethodInfo method = t.GetMethod("Function"); //GetValue方法的参数 object[] parameters = new object[] { "Function函数中的值" }; //调用方法,用一个object接收返回值 object returnValue = method.Invoke(dObj, BindingFlags.Public, Type.DefaultBinder, parameters, null); Console.Write(returnValue); } }

  

反射基础

标签:this   flags   tty   return   基础   arp   lag   odi   测试   

原文地址:https://www.cnblogs.com/liyiboke/p/10008900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!