目前工程需要用到发射,谁给讲讲,有形成的文档和DEMO最好

解决方案 »

  1.   

    MSDN 就是最好的文档啊
    很详细的
    我就看那个学习的反射
      

  2.   

    XPathNavigator navigator = nav.CreateNavigator();
                if (navigator.MoveToFirstAttribute())
                {
                    PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this);
                    do
                    {
                        PropertyDescriptor pd = pdc[navigator.Name];
                        if (pd != null && pd.IsLocalizable)
                        {
                            if (!pd.IsReadOnly)
                                pd.SetValue(this, CreateInstanceByType(pd.PropertyType, navigator.Value));
                            else    // Write ReadOnly Properties to XML
                            {
                                PropertyInfo pi = this.GetType().GetProperty(pd.Name);
                                if (pi != null && pi.CanWrite)
                                    pi.SetValue(this, CreateInstanceByType(pd.PropertyType, navigator.Value), null);
                            }
                        }
                        else
                            Properties.Add(navigator.Name.ToUpper(), navigator.Value);
                    } while (navigator.MoveToNextAttribute());
                }
      

  3.   

    程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途: 使用 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,您不必创建属性的实例就可以检查它们。System.Reflection.Emit 命名空间的类提供了一种特殊形式的反射,使您能够在运行时构造类型。反射也可用于创建称作类型浏览器的应用程序,它使用户能够选择类型,然后查看有关选定类型的信息。反射还有其他一些用途。JScript 等语言编译器使用反射来构造符号表。System.Runtime.Serialization 命名空间中的类使用反射来访问数据并确定要持久保存的字段。System.Runtime.Remoting 命名空间中的类通过序列化来间接地使用反射。
      

  4.   

    问的比较笼统,也只能给你说得笼统点...如果有MSDN的话,看看这个例子,讲得非常清楚了..ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref10/html/T_System_Reflection_BindingFlags.htm
      

  5.   

    using System;
    using System.Reflection;
    using System.IO;namespace BindingFlagsSnippet
    {
        class EntryPoint
        {
            static void Main(string[] args)
            {
                Invoke.Go();
            }
        }
        class Invoke
        {
            public static void Go()
            {
                // BindingFlags.InvokeMethod
                // Call a static method.
                Type t = typeof (TestClass);            Console.WriteLine();
                Console.WriteLine("Invoking a static method.");
                Console.WriteLine("-------------------------");
                t.InvokeMember ("SayHello", BindingFlags.InvokeMethod, null, null, new object [] {});            // BindingFlags.InvokeMethod
                // Call an instance method.
                TestClass c = new TestClass ();
                Console.WriteLine();
                Console.WriteLine("Invoking an instance method.");
                Console.WriteLine("----------------------------");
                c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});
                c.GetType().InvokeMember ("AddUp", BindingFlags.InvokeMethod, null, c, new object [] {});
                
                // BindingFlags.InvokeMethod
                // Call a method with parameters.
                object [] args = new object [] {100.09, 184.45};
                object result;
                Console.WriteLine();
                Console.WriteLine("Invoking a method with parameters.");
                Console.WriteLine("---------------------------------");
                result = t.InvokeMember ("ComputeSum", BindingFlags.InvokeMethod, null, null, args);
                Console.WriteLine ("{0} + {1} = {2}", args[0], args[1], result);            // BindingFlags.GetField, SetField
                Console.WriteLine();
                Console.WriteLine("Invoking a field (getting and setting.)");
                Console.WriteLine("--------------------------------------");
                // Get a field value.
                result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
                Console.WriteLine ("Name == {0}", result);
                // Set a field.
                t.InvokeMember ("Name", BindingFlags.SetField, null, c, new object [] {"NewName"});
                result = t.InvokeMember ("Name", BindingFlags.GetField, null, c, new object [] {});
                Console.WriteLine ("Name == {0}", result);
                
                Console.WriteLine();
                Console.WriteLine("Invoking an indexed property (getting and setting.)");
                Console.WriteLine("--------------------------------------------------");
                // BindingFlags.GetProperty 
                // Get an indexed property value.
                int  index = 3;
                result = t.InvokeMember ("Item", BindingFlags.GetProperty, null, c, new object [] {index});
                Console.WriteLine ("Item[{0}] == {1}", index, result);
                // BindingFlags.SetProperty
                // Set an indexed property value.
                index = 3;
                t.InvokeMember ("Item", BindingFlags.SetProperty, null, c, new object [] {index, "NewValue"});
                result = t.InvokeMember ("Item", BindingFlags.GetProperty , null, c, new object [] {index});
                Console.WriteLine ("Item[{0}] == {1}", index, result);
    ....太长,只贴了部分..
      

  6.   

    最简单明了的例子: Assembly a = Assembly.LoadFrom(@"c:\abc.dll");
    object o = a.CreateInstance("abc.Class1")