获取当前程序集assembly中类的自定义属性

 1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Text;
 4 using  System.Reflection;
 5 using  System.Collections;
 6 using  System.Xml;
 7
 8 namespace  ConsoleApplication1
 9 {
10
11
12    自定义URL 属性#region 自定义URL 属性
13    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
14    public class BindUrlAttribute : Attribute
15    {
16        private String bindUrl;
17        private string descUrl;
18
19        public String BindUrl
20        {
21            get return bindUrl; }
22            set { bindUrl = value; }
23        }

24
25        public string DescUrl
26        {
27            get return descUrl; }
28            set { descUrl = value; }
29        }

30    }

31    #endregion

32
33    要读取其自定义属性的类#region 要读取其自定义属性的类
34    [BindUrl(BindUrl = "aa.aspx", DescUrl = "test page")]
35    class TestAtt
36    {
37    }

38
39    [BindUrl(BindUrl = "index.aspx", DescUrl = "首页")]
40    class Class2
41    {
42    }

43
44    class TestClss
45    {
46
47    }

48    #endregion

49
50    class Demo
51    {
52        static void Main(string[] args)
53        {
54            List<BindUrlAttribute> urlList = GetUrlFromAssembly();
55            foreach (BindUrlAttribute var in urlList)
56            {
57                Console.WriteLine(var.BindUrl.ToString());
58                Console.WriteLine(var.DescUrl.ToString());
59            }
                    
60            Console.Read();
61
62        }

63
64        private static List<BindUrlAttribute> GetUrlFromAssembly()
65        {
66            Type[] types = Assembly.GetExecutingAssembly().GetTypes();
67
68            List<BindUrlAttribute> addInfo = new List<BindUrlAttribute>();
69
70            foreach (Type type in types)
71            {
72                object[] atts = type.GetCustomAttributes(true);
73                for (int i = 0; i < atts.Length; i++)
74                {
75                    if (atts[i] is BindUrlAttribute)
76                    {
77                        addInfo.Add(atts[i] as BindUrlAttribute);
78                        break;
79                    }

80                }

81            }

82
83            return addInfo;
84        }

85    }

86}

转载于:https://www.cnblogs.com/lyss/archive/2008/01/10/1034026.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值