• 学习反射札记


    个人觉得,反射就是在不知道对象内部构造的情况下,对其成员进行访问,即获得属性的值以及调用其中的方法等。

    为了更好的说明,下面用控制台程序来举例说明:

    /// <summary>

    /// Person对象
    /// </summary>

    class Person
        {
            public string FirstName { get; set; }
       public string LastName { get; set; }
            public void GetFullName()
            {
                Console.WriteLine(FirstName + LastName);
            }
            public void GetFullName(string firstname, string lastname)
            {
                Console.WriteLine(firstname + lastname);
            }
        }

    /// <summary>
    /// 利用Type类来反射Person中的方法——Program类内部函数
    /// </summary>

    public static void TypeClassTest()
            {

         Person person1 = new Person();
                person1.FirstName = "Li";
                person1.LastName = "Lei";
                Type t = person1.GetType();//获得类型
                MethodInfo[] mi = t.GetMethods();//获得方法,如果想获得属性可用 GetProperties (),想获得字段可用GetFields();
                for (int i = 0; i < mi.Length; i++)
                {
                    if (mi[i].Name.Equals("GetFullName"))
                    {
                        ParameterInfo[] pi = mi[i].GetParameters();//获得方法需要传入的参数信息
                        if (pi.Length == 0) mi[i].Invoke(person1, null);//调用无参的函数,如果函数为静态person1处改为null,如果调用构造函数person1处不填
                        else
                        {
                            if (pi[0].ParameterType.ToString().Equals(typeof(string).ToString()))
                            {
                                mi[i].Invoke(person1, new object[] { "Han", "Meimei" });//调用有参的函数,如果函数为静态person1处改为null,如果调用构造函数person1处不填
                            }
                        }
                    }
                }
            }

    /// <summary>
    /// 利用Assembly类来反射Person中的方法——Program类内部函数
    /// </summary>

    public static void AssemblyClassTest()
            {
                Assembly assembly1 = Assembly.Load("ClassLibrary1");//名称为ClassLibrary1程序集在本地目录或者全局程序集缓存,还有其他绝对路径加载方法
                Type[] tps = assembly1.GetTypes();//获得程序集中所有类型
                foreach (var tp in tps)
                {
                    if (tp.Name.Equals("MyClassLibrary"))
                    {
                        MethodInfo[] mis = tp.GetMethods();
                        foreach (var mi in mis)
                        {
                            if (!mi.IsStatic)
                            {
                                if (mi.Name.Equals("MyMethod"))
                                {

                //object myObject=assembly1.CreateInstance(tp.FullName);//使用assembly类对某一类型进行实例化,参数必须是类型全名

                object myObject=tp.GetConstructor(Type.EmptyTypes).Invoke(null);//使用Type类对某一类型进 行实例化GetConstructor中的参数表示参构造函数的参数类型组,Invoke中的参数为对应类型组的值
                                    mi.Invoke(tp.GetConstructor(Type.EmptyTypes).Invoke(null), new object[] { "This is MyClassLibrary.MyMethod" });
                                }
                            }
                            else
                            {
                                if (mi.GetParameters().Length == 0)
                                    mi.Invoke(null, null);//调用静态函数
                            }
                        }
                    }
                }
            }

    //程序集

    namespace ClassLibrary1
    {
        public class MyClassLibrary
        {
            public void MyMethod(string msg)
            {
                Console.WriteLine(msg);
            }
            public static void MyStaticMethod()
            {
                Console.WriteLine("This is MyClassLibrary.MyStaticMethod");
            }
        }

    }

    另外,获得当前程序集且获得其中方法信息的函数:

     public static void CurrentAssemblyTest()
            {
                StringBuilder sb = new StringBuilder();
                Assembly currentAssembly = Assembly.GetExecutingAssembly();
                Type currentType = currentAssembly.GetType("ConsoleApplication1.Program");
                MethodInfo[] mis = currentType.GetMethods();
                foreach (var mi in mis)
                {
                    sb.Append("方法名称:" + mi.Name + "  参数类型:无");
                    ParameterInfo[] pis = mi.GetParameters();
                    if (pis.Length > 0)
                    {
                        sb.Remove(sb.Length - 1, 1);
                        foreach (var pi in pis)
                        {
                            sb.Append(pi.ParameterType.ToString() + "|");
                        }
                        sb.Remove(sb.Length - 1, 1);
                    }
                    sb.Append("  返回类型:" + mi.ReturnType.ToString() + " ");
                }
                Console.WriteLine(sb.ToString());
            }

  • 相关阅读:
    getBoundingClientRect()方法
    Sublime Text3 安装less
    less知识点总结(一)
    跨域知识(二)——JSONP
    面向过程和面向对象的区别(转)
    暴力+DP:买卖股票的最佳时机
    车的可用捕获量(3.26leetcode每日打卡)
    三维形体的表面积(3.25leetcode每日打卡)
    基础练习:FJ的字符串
    DP:打家劫舍
  • 原文地址:https://www.cnblogs.com/victor-wxy/p/4226530.html
Copyright © 2020-2023  润新知