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

学习反射札记

时间:2015-01-15 17:30:31      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

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

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

/// <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() + "\n");
            }
            Console.WriteLine(sb.ToString());
        }

 

学习反射札记

标签:

原文地址:http://www.cnblogs.com/victor-wxy/p/4226530.html

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