C#反射实例(转)

     在网上查找了不少的资料,可以说大同小异,概念性的东西网上一搜一堆,今天把反射的东西整理了一下,供大家使用,我保证我这里是最全面的东西,当然也是基础的东西,在学好了这一切的基础上,大家可以学习反射的具体插件等应用,老鸟就不用看了。

//首先我们建立一个类库,将它生成为HelloWorld.dll,

using System;
using System.Collections.Generic;
using System.Text;

namespace Webtest
{
    using System;

    namespace Webtest
    {

        public interface interface1
        {
            int add();

        }
        public class ReflectTest : interface1
        {

            public String Write;
            private String Writec;

            public String Writea
            {
                get
                {
                    return Write;
                }
                set
                {
                    Write = value;
                }

            }

            private String Writeb
            {
                get
                {
                    return Writec;
                }
                set
                {
                    Writec = value;
                }

            }

            public ReflectTest()
            {
                this.Write = "Write";
                this.Writec = "Writec";
            }

            public ReflectTest(string str1, string str2)
            {
                this.Write = str1;
                this.Writec = str2;

            }

            public string WriteString(string s, int b)
            {
                return "\n这是类库函数打印的字符串:欢迎您," + s + "---" + b; ;
            }

            public static string WriteName(string s)
            {
                return "\n这是类库函数打印的字符串:欢迎您光临," + s;
            }

            public string WriteNoPara()
            {
                return "\n这是类库函数打印的字符串:您使用的是无参数方法";
            }

            private string WritePrivate()
            {
                return "\n这是类库函数打印的字符串:私有类型的方法";
            }


            public int add()
            {
                return 5;
            }
        }
    }


}

//然后,建立再建立一个项目引入该HelloWorld.dll,

using System;
using System.Threading;
using System.Reflection;
using Webtest.Webtest;


class Test
{
    delegate string TestDelegate(string value, int value1);

    static void Main()
    {
        //Assembly t = Assembly.LoadFrom("Webtest.dll"); //与下面相同的效果
        Assembly t = Assembly.Load("Webtest");

        Console.Write("Webtest类库里有以下类:\n");
        foreach (Type aaa in t.GetTypes())
        {
            Console.Write(aaa.Name + "\n");   //显示该dll下所有的类
        }

        Module[] modules = t.GetModules();
        Console.WriteLine();
        foreach (Module module in modules)
        {
            Console.WriteLine("模块名称:\n" + module.Name);//显示模块的名字本例为"HelloWorld.dll"
        }

        Type a = typeof(Webtest.Webtest.ReflectTest);//得到具体的类的类型,和下面一个效果
        //Type a = t.GetType("Webtest.ReflectTest");
        //Console.Write("模块里的类有:" + a.Name + "\n");


        string[] bb ={ "aaaaa", "bbbbb" };
        object obj = Activator.CreateInstance(a, bb); //创建该类的实例,后面的bb为有参构造函数的参数
        //object obj = t.CreateInstance("Webtest.ReflectTest");//与上面方法相同

        //获得实例公共方法的集合
        MethodInfo[] miArr = a.GetMethods();
        Console.Write("\n共有方法: \n");
        foreach (MethodInfo mi0 in miArr)
        {
            Console.Write(mi0.Name + "\n"); //显示所有的共有方法
        }


        MethodInfo mi = a.GetMethod("WriteString");//显示具体的方法
        object[] aa ={ "使用的是带有参数的非静态方法", 2 };
        string s = (string)mi.Invoke(obj, aa); //带参数方法的调用

        MethodInfo mi1 = a.GetMethod("WriteName");
        String[] aa1 ={ "使用的是静态方法" };
        string s1 = (string)mi1.Invoke(null, aa1); //静态方法的调用

        MethodInfo mi2 = a.GetMethod("WriteNoPara");
        string s2 = (string)mi2.Invoke(obj, null); //不带参数的方法调用

        MethodInfo mi3 = a.GetMethod("WritePrivate", BindingFlags.Instance | BindingFlags.NonPublic);
        string s3 = (string)mi3.Invoke(obj, null); //私有类型方法调用

        //Console.Write(s3);

        //获得实例公共属性的集合
        PropertyInfo[] piArr = a.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        Console.WriteLine("\n显示所有的属性:");
        foreach (PropertyInfo pi in piArr)
        {
            Console.WriteLine(pi.Name); //显示所有的属性
        }


        PropertyInfo pi1 = a.GetProperty("Writea");
        //pi1.SetValue(obj, "Writea", null);
        //Console.Write(pi1.GetValue(obj,null));

        PropertyInfo pi2 = a.GetProperty("Writeb", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        pi2.SetValue(obj, "Writeb", null);
        //Console.Write(pi2.GetValue(obj, null));

        FieldInfo fi1 = a.GetField("Write");
        //Console.Write(fi1.GetValue(obj));

        //获得实例公共构造函数的集合
        ConstructorInfo[] ci1 = a.GetConstructors();
        Console.WriteLine("\n显示所有的构造函数:");
        foreach (ConstructorInfo ci in ci1)
        {
            Console.Write(ci.ToString()+"\n"); //获得构造函数的形式
        }
       
        //ConstructorInfo asCI = a.GetConstructor(new Type[] { typeof(string), typeof(string) });
        //Console.Write(asCI.ToString());


        Webtest.Webtest.interface1 obj1 = (Webtest.Webtest.interface1)t.CreateInstance("Webtest.ReflectTest");
        Webtest.Webtest.ReflectTest obj2 = (Webtest.Webtest.ReflectTest)t.CreateInstance("Webtest.ReflectTest");
        //Console.Write(obj1.add());典型的工厂模式


        foreach (Type tt in t.GetTypes())
        {
            if (tt.GetInterface("interface1") != null)
            {
                Webtest.Webtest.interface1 obj3 = (Webtest.Webtest.interface1)Activator.CreateInstance(a);
                //Console.Write(obj3.add());
            }
        }


        TestDelegate method = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelegate), obj, "WriteString");
        //动态创建委托的简单例子
        Console.Write(method("str1", 2));
        Console.Read();
    }
}

    在这里我把我们常用的方法,属性,等全部整理了出来,大家不要嫌弃乱,静下心来,自己按照我的分隔一部分一部分的来,保证你对反射的学习,会事半功倍.当然有关于其方法我会继续补充,想了这么些就先写下来吧.

2008年08月16日 星期六 下午 12:21
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途:

使用 Assembly 定义和加载程序集,加载在程序集清单中列出的模块,以及从此程序集中查找类型并创建该类型的实例。

使用 Module 了解如下的类似信息:包含模块的程序集以及模块中的类等。您还可以获取在模块上定义的所有全局方法或其他特定的非全局方法。

使用 ConstructorInfo 了解以下信息:构造函数的名称、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetConstructors 或 GetConstructor 方法来调用特定的构造函数。

使用 MethodInfo 了解以下信息:方法的名称、返回类型、参数、访问修饰符(如 public 或 private)和实现详细信息(如 abstract 或 virtual)等。使用 Type 的 GetMethods 或 GetMethod 方法来调用特定的方法。

使用 FieldInfo 了解以下信息:字段的名称、访问修饰符(如 public 或 private)和实现详细信息(如 static)等;并获取或设置字段值。

使用 EventInfo 来了解如下的类似信息:事件的名称、事件处理程序数据类型、自定义属性、声明类型和反射类型等;并添加或移除事件处理程序。

使用 PropertyInfo 来了解如下的类似信息:属性的名称、数据类型、声明类型、反射类型和只读或可写状态等;并获取或设置属性值。

使用 ParameterInfo 来了解如下的类似信息:参数的名称、数据类型、参数是输入参数还是输出参数,以及参数在方法签名中的位置等。

当您在一个应用程序域的仅反射上下文中工作时,请使用 CustomAttributeData 来了解有关自定义属性的信息。使用 CustomAttributeData,您不必创建属性的实例就可以检查它们。[FROM:MSDN]

通过一个示例来简要说明:

using System;
using System.Reflection;

namespace testReflection
{
    public delegate void TestDelegate(string info);
    class Program
    {
        static void Main(string[] args)
        {
             //从Dll中加载
             //Assembly ass = Assembly.LoadFile(@"TestReflect.dll");
             //Type myType = ass.GetType("testReflection.Person");
             //object aPerson = ass.CreateInstance("Person");

             //取得类型
            Type myType = Type.GetType ("testReflection.Person");
             //构造函数要用到的参数
            object [] constuctParms = new object[] { "Brad Pitt" };
            //创建实例
            //object TestName = Assembly.GetAssembly(myType).CreateInstance("Person");
            object aPerson = Activator.CreateInstance (myType, constuctParms);

             //使用MethodInfo 和Invoke 调用方法
            MethodInfo displayInfoMethod = myType.GetMethod ("DisplayInfo");
            displayInfoMethod .Invoke(aPerson, new object[] { "Using Invoke to call Method DisplayInfo()" });

            //使用InvokeMember 调用方法
            //调用方法的一些标志位
             BindingFlags flag = BindingFlags .InvokeMethod | BindingFlags.Public | BindingFlags.Instance;
            myType.InvokeMember("DisplayInfo", flag, null, aPerson, new object[] { "Using InvokeMethod to call DisplayInfo()" });
             //如果方法有返回值
             string name = (string)myType.InvokeMember("getName" , flag, null , aPerson, null );
            System.Console.WriteLine( "call getName(), return: " + name );

            //设置属性值
            myType.InvokeMember("Age" , BindingFlags.SetProperty, null, aPerson, new object[] { 30 });
            //得到属性值
             int age = (int)myType.InvokeMember( "Age", BindingFlags.GetProperty, null, aPerson, null);
            System.Console .WriteLine("Get the property of Age : " + Convert.ToString(age));

             //设置字段值
            myType.InvokeMember ("Name", BindingFlags.SetField , null, aPerson , new object[] { "Michal Jodn" });
             //获取字段值
            string fname = ( string)myType.InvokeMember("Name", BindingFlags.GetField, null , aPerson, null );
            System.Console.WriteLine("Get the Field Value of Name : " + fname);

            myType.InvokeMember("DisplayName", flag, null, aPerson, null);

            //获得方法集
             MethodInfo[] methods = myType.GetMethods();
            foreach (MethodInfo m in methods) {
                System.Console.WriteLine(m.Name);
            }
            //同样还有:GetFiedls()、GetProperties()、GetEvents()等方法

            //使用Delegate
            //此方法是静态的,所以必须提供委托类型。
            TestDelegate dg = (TestDelegate)Delegate.CreateDelegate( typeof(testReflection.TestDelegate), aPerson, "DisplayInfo");
            dg ("Test Delegate by call DisplayInfo()");
            
           
            //获得解决方案的所有Assembly
            Assembly[] AX = AppDomain.CurrentDomain.GetAssemblies();
            //遍历显示每个Assembly的名字
            foreach (object var in AX ) {
                Console.WriteLine ("Assembly的名字:"+var.ToString());               
            }
             //使用一个已知的Assembly名称,来创建一个Assembly
             //通过CodeBase属性显示最初指定的程序集的位置
            Console.WriteLine ("最初指定的程序集TestReflection的位置:" + Assembly.Load("TestReflection").CodeBase);

            System.Console.ReadLine();

         }
    }  

    public class Person
    {
        public string Name;
        private int _Age;
        public int Age
        {
            get
            {
                 return _Age;
             }
            set
            {
                _Age = value;
            }
        }

        public Person (string Name)
         {
            this.Name = Name;
        }
        public void DisplayInfo(string info)
        {
            System.Console.WriteLine(info );
            System.Console.WriteLine( "called sucessfully!");
        }
         public void DisplayName()
        {
            System .Console.WriteLine(Name);
        }
        public string getName()
        {
             return Name;
        }

    }
}
说明:
使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下:

public object InvokeMember(
   string name,
   BindingFlags invokeAttr,
   Binder binder,
   object target,
   object[] args
);

参数

name
String,它包含要调用的构造函数、方法、属性或字段成员的名称。
- 或 -
空字符串 (""),表示调用默认成员。

invokeAttr
一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。访问可以是 BindingFlags 之一,如 Public、NonPublic、Private、InvokeMethod 和 GetField 等。不需要指定查找类型。如果省略查找类型,则将应用 BindingFlags.Public | BindingFlags.Instance。

binder
一个 Binder 对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。
- 或 -
若为空引用(Visual Basic 中为 Nothing),则使用 DefaultBinder。

target
在其上调用指定成员的 Object。

args
包含传递给要调用的成员的参数的数组。

返回值
表示被调用成员的返回值的 Object。

下列 BindingFlags 筛选标志可用于定义包含在搜索中的成员:

为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。
指定 BindingFlags.Public 可在搜索中包含公共成员。
指定 BindingFlags.NonPublic 可在搜索中包含非公共成员(即私有成员和受保护的成员)。
指定 BindingFlags.FlattenHierarchy 可包含层次结构上的静态成员。

下列 BindingFlags 修饰符标志可用于更改搜索的执行方式:
BindingFlags.IgnoreCase,表示忽略 name 的大小写。
BindingFlags.DeclaredOnly,仅搜索 Type 上声明的成员,而不搜索被简单继承的成员。

可以使用下列 BindingFlags 调用标志表示要对成员采取的操作:
CreateInstance,表示调用构造函数。忽略 name。对其他调用标志无效。
InvokeMethod,表示调用方法,而不调用构造函数或类型初始值设定项。
对 SetField 或 SetProperty 无效。
GetField,表示获取字段值。对 SetField 无效。
SetField,表示设置字段值。对 GetField 无效。
GetProperty,表示获取属性。对 SetProperty 无效。
SetProperty 表示设置属性。对 GetProperty 无效。
 

 

原文地址:https://www.cnblogs.com/ejiyuan/p/1289623.html