这些资料是我学习的时候在网络上寻找到的,发布出来希望对大家有帮助,一起努力前进,嘿嘿......Microsoft C#规范 2.0 版 GFC用户提交

feedsky 抓虾 pageflakes google reader my yahoo bloglines 鲜果 有道 http://wap.feedsky.com/bliplink
显示标签为“Reflection高级编程”的博文。显示所有博文
显示标签为“Reflection高级编程”的博文。显示所有博文

Reflection高级编程1

今天上课老师讲了c#中的高级编程,讲的很透彻,我感觉c#中的反射原理,其实和我们平时用的类-实例是一样的道理,只不过现可以把其它的类作为Reflection类中的一个实例,有Reflection类对这些类进行操作,利用反射可以在运行时得到类型信息
1. 包括方法、属性、事件、字段及构造函数,还可以获得每个成员的名称、限定符和参数等信息。
2.利用反射可以在运行时根据得到的类型信息动态创建类型的实例,设置字段,调用方法,设置属性和激发事件
现在我把有关反射类所设计的一些知识给大家共享出来,如果你想获得更好的理解,建议你通过msil去查看一下这些类到底再内存中是如果工作的,
所有我们现在用的反射都是通过抽象类来引用自己派生类的东西,所有有关对类进行操作的反射类的方法,微软已经全部封装在.dll程序集中是internel类型的,因此我们是直接用不了的
现在我把我们老师的内容给大家发布出来,希望对你们有用
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//(1).NET Framework对RTTI提供了整套支持,最重要的一个支持就是System.Type类。
//  CLR开始在一个进程中运行时,它会立即为System.Type类型(在MSCorLib.dll中定义)创建一个特殊的类型对象System.RuntimeType
//  其它运行类型对象均为System.RuntimeType类型的实例。
//(2)在运行其间,可以通过以下方法返回System.Type类型的实例。
//  (a)Object.GetType()方法是非虚方法,这样可以防止一个类重写该方法,并隐藏其类型,从而破坏类型安全性。
//   Object.GetType() 返回当前实例的运行时类型,更详细的说:
//   Object.GetType()返回的是存储在指定对象中的“类型对象指针”成员中的地址,
//       即返回的是指定对象的类型对象指针。
//  (b)typeof 运算符用于获得指定类型的 System.Type对象,即返回的是指定类型的类型对象指针。
//   typeof 表达式采用的形式:typeof(type)
//   其中:type 要获得其 System.Type 对象的类型。
//  (c)通过System.Type类的静态方法创建
//   传入的自符串参数必须是完全限定名:
//   Type myType1 = Type.GetType("System.Int32");
//  (d)通过System.Reflection.Assembly类的实例方法
//    System.Reflection.Assembly.GetType(<args>)
//    System.Reflection.Assembly.GetTypes()
//     

using System;
using System.Reflection;
namespace _08_21
{
    public class Class_08_21
    {
        public static void Main()
        {
            //因为System.Type类型对象本身也是System.RuntimeType类型对象的“实例”。
            // 获取 System.Type 自身的 Type 实例
            Type t = typeof(System.Type);
            //System.RuntimeType类型对象本身也是一个对象,所以也包含一个类型对象指针成员,它指向它本身。
            Type t1 = t.GetType();
            Type t2 = t1.GetType();

            Console.WriteLine(t);
            Console.WriteLine(t1);
            Console.WriteLine(t2);

            // 获取一些该 Type 的性质
            Console.WriteLine("IsAbstract : {0}", t.IsAbstract);
            //Console.WriteLine("IsAbstract : {0}", t.GetType().IsAbstract);
            Console.WriteLine("IsClass : {0}", t.IsClass);
            Console.WriteLine("IsArray : {0}", t.IsArray);
            Console.WriteLine("IsValueType : {0}", t.IsValueType);
            Console.WriteLine("IsPublic : {0}", t.IsPublic);
            // 获取该 Type 的公共字段
            Console.WriteLine("Type 的 Fields :");
            foreach (FieldInfo fi in t.GetFields())
            {
                Console.WriteLine("\t{0}", fi);
            }
            Console.WriteLine("RuntimeType 的 Fields :");
            foreach (FieldInfo fi in t1.GetFields())
            {
                Console.WriteLine("\t{0}", fi);
            }
            // 用 GetMethod(string, Type[]) 方法获取该方法本身的信息
            Console.WriteLine("GetMethod : {0}", t.GetMethod("GetMethod", new Type[] { typeof(string), typeof(Type[]) }));
        }
    }
}

 

......

[阅读全文]

Reflection高级编程3

//类型的信息都是以“元数据”的形式保存在程序集中的。
//运行时类型信息(RuntimeTypeInfo--RTTI)可以在程序运行的过程中提供有关类型的信息。
//可以输出类的所有公开成员的信息。

using System;
using System.Reflection;

namespace _08_20
{
    public class MyClass
    {
        private string privateField;
        public string Field1;
        public int Property1
        {
            get { return 0; }
        }
        public void Method1(string param)
        {
        }
    }

    public class Class_08_20
    {
        public static void Main()
        {
            string s = "Hello";
            MyClass m = new MyClass();

            Console.WriteLine(s.GetType());
            Console.WriteLine(m.GetType());

            System.Type t = typeof(MyClass);
            Console.WriteLine(t);

            //public MemberInfo[] Type.GetMembers 方法 ()
            foreach (MemberInfo mem in t.GetMembers())
            {
                Console.WriteLine("\t{0}", mem);
                Console.WriteLine("\t{0}", mem.GetType().ToString());
            }

        }
    }
}


......

[阅读全文]

Reflection高级编程2

//(1)利用反射可以在运行时得到类型信息
//  包括方法、属性、事件、字段及构造函数,还可以获得每个成员的名称、限定符和参数等信息。
//(2)利用反射可以在运行时根据得到的类型信息动态创建类型的实例,设置字段,调用方法,设置属性和激发事件。

//------------------System.Reflection命名空间中的一些类------------------------------------------
//
// System.Object
//  System.Reflection.Assembly
//  System.Reflection.Module
//  System.Reflection.ParameterInfo
//  System.Reflection.MemberInfo
//   System.Reflection.EventInfo
//   System.Reflection.FieldInfo
//   System.Reflection.MethodBase
//    System.Reflection.ConstructorInfo
//    System.Reflection.MethodInfo
//   System.Reflection.PropertyInfo
//   System.Type
//
// MemberInfo 类是用于获取类的所有成员(构造函数、事件、字段、方法和属性)信息的类的抽象基类。
//----------------------------------------------------------------------------------------------

//Assembly类的CreateInstance()方法在创建具有无参数构造函数的对象时比较方便,创建具有有参数构造函数的对象时比较麻烦。
//Assembly类的CreateInstance()方法的内部是调用系统激活器类Activator的CreateInstance来创建对象的。

//创建具有有参数构造函数的对象时,可采用Assembly类的CreateInstance()方法。
----------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;
namespace _08_22
{
    public class MyClass
    {
        public string MyField;
        public void MyMethod()
        {
            Console.WriteLine("调用 MyMethod! MyField: {0}", MyField);
        }
        public void MyMethod(string param)
        {
            Console.WriteLine("调用 MyMethod! MyField: {0}. 参数: {1}", MyField, param);
        }
    }
}
------------------------------------------------------------------------------------------------------------------

using System;
using System.Reflection;

namespace _08_22
{
    public class Class_08_22
    {
        public static void Main()
        {
            Type t = typeof(object);
            // 从文件中加载程序集 MyAssembly
            Assembly a = Assembly.LoadFrom("MyAssembly.dll");

            // 获取程序集中名为"MyClass"的 Type, 这里必须用全名
            Type type = a.GetType("_08_22.MyClass");

            // 动态创建 MyClass 的一个实例
            Object obj = Activator.CreateInstance(type);

            // 动态设置 obj 的字段
            FieldInfo field = type.GetField("MyField");
            field.SetValue(obj, "动态设置的字段");

            // 动态调用 obj 的方法
            MethodInfo method = type.GetMethod("MyMethod", new Type[0]);
            method.Invoke(obj, null);
            method = type.GetMethod("MyMethod", new Type[] { typeof(string) });
            method.Invoke(obj, new Object[] { "一个参数" });
        }
    }
}


......

[阅读全文]

友情链接

郑州大学软件学院 SpringWidgets-Blogger 徵信社 翻译公司 链接帮手网 行驶证字体 酷站目录 Friend Connectified