如何进行定义特性(Attribute),并且在程序中如何检索定义的特性,可以根据用户定义的不同特性进行不同的操作。

using System;

using System.Collections;

using System.Runtime.InteropServices;

using System.Diagnostics;

using System.Reflection;

 

namespace Test

{

        public class Myclass

        {

                public static void Main ()

                {

                        HelpAttribute HelpAttr;

 

                        string assemblyName;

                        Process p = Process.GetCurrentProcess();

                        assemblyName = p.ProcessName + ".exe";

                        Assembly a = Assembly.LoadFrom(assemblyName);

                       

                        Type b = a.GetType("Test.Class1");

                        foreach (Attribute attr in b.GetCustomAttributes(true))

                        {

                                HelpAttr = attr as HelpAttribute;

                                if (null != HelpAttr)

                                {

                                        Console.WriteLine("Description of {0}:/n{1},Version={2}",

                                                assemblyName,HelpAttr.Description,HelpAttr.Version);

                                }

                        }

 

                        Type c = a.GetType("Test.Class2");

                       

                        foreach (Attribute attr in c.GetCustomAttributes(true))

                        {

                                HelpAttr = attr as HelpAttribute;

                                if (null != HelpAttr)

                                {

                                        Console.WriteLine("Description of {0}:/n{1},Version={2}",

                                                assemblyName,HelpAttr.Description,HelpAttr.Version);

                                }

                        }

 

                        Type d = a.GetType("Test.Class3");

                       

                        foreach (Attribute attr in d.GetCustomAttributes(true))

                        {

                                HelpAttr = attr as HelpAttribute;

                                if (null != HelpAttr)

                                {

                                        Console.WriteLine("Description of {0}:/n{1},Version={2}",

                                                assemblyName,HelpAttr.Description,HelpAttr.Version);

                                }

                        }

                }

        }

 

        [Help("This is Class1")]

        public class Class1

        {

        }

 

        [Help("This is Class2", Version = "1.0")]

        public class Class2

        {

        }

 

 

        [Help("This is Class3", Version = "2.0",

                 Description = "This is do-nothing class")]

        public class Class3

        {

        }

 

        [AttributeUsage(AttributeTargets.Class, AllowMultiple = true,

                 Inherited = true)]

        public class HelpAttribute : Attribute

        {

                public HelpAttribute(String Description_in)

                {

                        this.description = Description_in;

                        this.version = "No Version is defined for this class";

                }

 

                protected string description;

                public string Description

                {

                        get

                        {

                                return this.description;

                        }

                        set

                        {

                                this.description = value;

                        }

                }

 

                protected string version;

                public string Version

                {

                        get

                        {

                                return this.version;

                        }

                        set

                        {

                                this.version = value;

                        }

                }

        }

}

 

结果为:

Description of ConsoleApplication7.exe:

This is Class1,Version=No Version is defined for this class

Description of ConsoleApplication7.exe:

This is Class2,Version=1.0

Description of ConsoleApplication7.exe:

This is do-nothing class,Version=2.0 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值