4.0 高级编程 Reflection

来源:互联网 发布:fc2最新域名怎么设置 编辑:程序博客网 时间:2024/06/01 08:46

System.type类

1.属性

许多布尔属性表示这种类型是一个类还是一个枚举等等。这些特性包括IsAbstract,IsClass等等

Type vecType = typeof(Vector);

            Console.WriteLine(vecType.IsAbstract);//return false

            Console.WriteLine(vecType.IsClass);//return true

2.方法

获取对应数据类型的成员信息,构造函数,属性,方法和事件

 

            Type t = typeof(double);

            MethodInfo[] methods = t.GetMethods();

            foreach (MethodInfonextmethodin methods)

            {

               Console.WriteLine(nextmethod);

            }

返回方法的俄细节信息。

Assmeble类

把程序集加载到正在运行的进程中。可以使用静态成员Assmeble.Load和Assemble.LoadFrom。前者的参数是程序集名称,运行库会在各个位置上搜索该程序集。后者的参数是完整的路径

WhatsNewAttribute的例子自定义Attribute 类必须继承于System.Attribute 空间。

namespace Wrox.ProCSharp.WhatsNewAttributes

{

    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple =true, Inherited = false)]

    public classLastModifiedAttribute:Attribute

    {

        private DateTimedateModified;

        private stringchanges;

        private stringissues;

 

        public LastModifiedAttribute(stringdateModified,string changes)

        {

            this.dateModified = DateTime.Parse(dateModified);

            this.changes = changes;

        }

 

        public DateTimeDateModified

        {

            get

            {

               return dateModified;

            }

        }

 

        public string Changes

        {

            get

            {

               return changes;

            }

        }

 

        public string Issues

        {

            get

            {

               return issues;

            }

            set

            {

               issues = value;

            }

        }

 

    }

    [AttributeUsage(AttributeTargets.Assembly)]

    public classSupportsWhatsNewAttribute :Attribute

    {

    }

}

 

 

using Wrox.ProCSharp.WhatsNewAttributes;

using System.Collections;

 

[assembly: SupportsWhatsNew]

namespace Wrox.ProCSharp.VectorClass

{

    [LastModified("14Feb 2007","IEnumerable interfaceimplemented " +

      "So Vector can now be treated as a collection")]

    [LastModified("10Feb 2007","IFormattable interfaceimplemented " +

        "So Vector now responds to format specifiers N andVE")]

    class Vector :IFormattable

    {

        public double x, y,z;

 

        public Vector(doublex,double y,doublez)

        {

            this.x = x;

            this.y = y;

            this.z = z;

        }

 

        [LastModified("10Feb 2002",

                      "Methodadded in order to provide formatting support")]

        public stringToString(string format,IFormatProvider formatProvider)

        {

            if (format != null)

            {

               return ToString();

            }

            else

                returnnull;

        }

 

        [LastModified("14Feb 2007",

             "Class created as part of collection support forVector")]

        private classVectorEnumerator

        { }

    }

}

 

 

using Wrox.ProCSharp.WhatsNewAttributes;

namespace Reflection

{

    class Program

    {

        static StringBuilderoutputText =newStringBuilder(1000);

        static DateTimebackDateTo =newDateTime(2007,2, 1);

        static void Main(string[] args)

        {

            Assembly theAssembly = Assembly.Load("Wrox.ProCSharp.VectorClass");

            Type[] types = theAssembly.GetTypes();

            foreach (Type tin types)

            {

               Console.WriteLine(t.Name);

               Attribute[] attri = Attribute.GetCustomAttributes(t, false);

               foreach (Attributeiin attri)

               {

                   LastModifiedAttributelastModifiedAttrib = iasLastModifiedAttribute;

                   Console.WriteLine(lastModifiedAttrib.DateModified.ToString()+" ; " +lastModifiedAttrib.Changes);

               }

            }

            Console.Read();

        }

 

    }

}

 

 


0 0