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
}

2

3

4

5

6

7

8

9



10

11

12


13

14

15



16

17

18

19

20



21



22



23

24

25

26



27



28



29

30

31

32

33


34

35

36



37

38

39

40

41



42

43

44

45



46

47

48

49

50

51



52

53



54

55

56



57

58

59

60

61

62

63

64

65



66

67

68

69

70

71



72

73

74



75

76



77

78

79

80

81

82

83

84

85

86
