comparison Source/TypeBuilder/GenerateAttributeAttribute.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 namespace BLToolkit.TypeBuilder
4 {
5 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]
6 public class GenerateAttributeAttribute: Builders.AbstractTypeBuilderAttribute
7 {
8 public GenerateAttributeAttribute(Type attributeType)
9 {
10 _attributeType = attributeType;
11 }
12
13 public GenerateAttributeAttribute(Type attributeType, params object[] arguments)
14 {
15 _attributeType = attributeType;
16 _arguments = arguments;
17 }
18
19 private readonly Type _attributeType;
20 public Type AttributeType
21 {
22 get { return _attributeType; }
23 }
24
25 private readonly object[] _arguments;
26 public object[] Arguments
27 {
28 get { return _arguments; }
29 }
30
31 private string[] _namedArgumentNames;
32 public string[] NamedArgumentNames
33 {
34 get { return _namedArgumentNames; }
35 set { _namedArgumentNames = value; }
36 }
37
38 private object[] _namedArgumentValues;
39 public object[] NamedArgumentValues
40 {
41 get { return _namedArgumentValues; }
42 set { _namedArgumentValues = value; }
43 }
44
45 public object this[string name]
46 {
47 get
48 {
49 if (_namedArgumentNames == null)
50 return null;
51
52 int idx = Array.IndexOf(_namedArgumentNames, name);
53
54 return idx < 0? null: _namedArgumentValues[idx];
55 }
56 set
57 {
58 if (_namedArgumentNames == null)
59 {
60 _namedArgumentNames = new string[]{ name };
61 _namedArgumentValues = new object[]{ value };
62 return;
63 }
64
65 int idx = Array.IndexOf(_namedArgumentNames, name);
66 if (idx < 0)
67 {
68 idx = _namedArgumentNames.Length;
69
70 Array.Resize(ref _namedArgumentNames, idx + 1);
71 Array.Resize(ref _namedArgumentValues, idx + 1);
72
73 _namedArgumentNames [idx] = name;
74 _namedArgumentValues[idx] = value;
75 }
76 else
77 {
78 _namedArgumentValues[idx] = value;
79 }
80 }
81 }
82
83 public T GetValue<T>(string name)
84 {
85 object value = this[name];
86 return value == null? default(T): (T)value;
87 }
88
89 public T GetValue<T>(string name, T defaultValue)
90 {
91 return _namedArgumentNames == null || Array.IndexOf(_namedArgumentNames, name) < 0?
92 defaultValue : GetValue<T>(name);
93 }
94
95 public void SetValue<T>(string name, T value)
96 {
97 this[name] = value;
98 }
99
100 public override Builders.IAbstractTypeBuilder TypeBuilder
101 {
102 get
103 {
104 return new Builders.GeneratedAttributeBuilder(
105 _attributeType, _arguments, _namedArgumentNames, _namedArgumentValues);
106 }
107 }
108 }
109 }