4.0 高级编程 Reflection

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 =new StringBuilder(1000);

        static DateTimebackDateTo =new DateTime(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 = ias LastModifiedAttribute;

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

               }

            }

            Console.Read();

        }

 

    }

}

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值