0
|
1 using System;
|
|
2 using System.Reflection;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.TypeBuilder;
|
|
8
|
|
9 namespace TypeBuilder
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class GenerateAttributeTest
|
|
13 {
|
|
14 [AttributeUsage(AttributeTargets.All, Inherited = false)]
|
|
15 public class NonInheritedAttribute: Attribute
|
|
16 {
|
|
17 public NonInheritedAttribute()
|
|
18 {
|
|
19 }
|
|
20
|
|
21 public NonInheritedAttribute(Type type, string str)
|
|
22 {
|
|
23 _type = type;
|
|
24 }
|
|
25
|
|
26 public NonInheritedAttribute(Type type, string str, int i, AttributeTargets e):
|
|
27 this(type, str)
|
|
28 {
|
|
29 }
|
|
30
|
|
31 private string _namedArgument;
|
|
32 public string NamedArgument
|
|
33 {
|
|
34 get { return _namedArgument; }
|
|
35 set { _namedArgument = value; }
|
|
36 }
|
|
37
|
|
38 private Type _type;
|
|
39 public Type Type
|
|
40 {
|
|
41 get { return _type; }
|
|
42 set { _type = value; }
|
|
43 }
|
|
44 }
|
|
45
|
|
46 public abstract class TestObject
|
|
47 {
|
|
48 [NonInherited]
|
|
49 public abstract void Method1();
|
|
50
|
|
51 [GenerateAttribute(typeof (NonInheritedAttribute))]
|
|
52 public abstract void Method2();
|
|
53
|
|
54 [GenerateAttribute(typeof (NonInheritedAttribute), typeof(TestObject), "str", 123, AttributeTargets.Field)]
|
|
55 public abstract void Method3();
|
|
56
|
|
57 [GenerateAttribute(typeof (NonInheritedAttribute))]
|
|
58 public virtual void Method4(){}
|
|
59
|
|
60 [GenerateAttribute(typeof(NonInheritedAttribute),
|
|
61 NamedArgumentNames = new string[] { "NamedArgument"},
|
|
62 NamedArgumentValues = new object[] { "SomeValue"})]
|
|
63 public virtual void Method5() { }
|
|
64
|
|
65 public abstract int Prop1
|
|
66 {
|
|
67 [GenerateAttribute(typeof(NonInheritedAttribute))] get;
|
|
68 [GenerateAttribute(typeof(NonInheritedAttribute))] set;
|
|
69 }
|
|
70
|
|
71 private int _prop2;
|
|
72 public virtual int Prop2
|
|
73 {
|
|
74 [GenerateAttribute(typeof(NonInheritedAttribute))] get { return _prop2; }
|
|
75 [GenerateAttribute(typeof(NonInheritedAttribute))] set { _prop2 = value; }
|
|
76 }
|
|
77
|
|
78 // This affects the underlying field, not the property itself.
|
|
79 //
|
|
80 [GenerateAttribute(typeof(NonInheritedAttribute))]
|
|
81 public abstract int Prop3 { get; set; }
|
|
82 }
|
|
83
|
|
84 [Test]
|
|
85 public void MainTest()
|
|
86 {
|
|
87 TestObject o = (TestObject)TypeAccessor.CreateInstance(typeof(TestObject));
|
|
88 Type type = o.GetType();
|
|
89
|
|
90 Assert.IsNull (Attribute.GetCustomAttribute(type.GetMethod("Method1"), typeof(NonInheritedAttribute)));
|
|
91
|
|
92 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetMethod("Method2"), typeof(NonInheritedAttribute)));
|
|
93 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetMethod("Method3"), typeof(NonInheritedAttribute)));
|
|
94 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetMethod("Method4"), typeof(NonInheritedAttribute)));
|
|
95
|
|
96 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetProperty("Prop1").GetGetMethod(), typeof(NonInheritedAttribute)));
|
|
97 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetProperty("Prop1").GetSetMethod(), typeof(NonInheritedAttribute)));
|
|
98 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetProperty("Prop2").GetGetMethod(), typeof(NonInheritedAttribute)));
|
|
99 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetProperty("Prop2").GetSetMethod(), typeof(NonInheritedAttribute)));
|
|
100
|
|
101 Assert.IsNotNull(Attribute.GetCustomAttribute(type.GetField("_prop3", BindingFlags.Instance | BindingFlags.NonPublic), typeof(NonInheritedAttribute)));
|
|
102
|
|
103 NonInheritedAttribute attribute = (NonInheritedAttribute)
|
|
104 Attribute.GetCustomAttribute(type.GetMethod("Method5"), typeof(NonInheritedAttribute));
|
|
105
|
|
106 Assert.IsNotNull(attribute);
|
|
107 Assert.AreEqual("SomeValue", attribute.NamedArgument);
|
|
108 }
|
|
109
|
|
110 public abstract class BadObject
|
|
111 {
|
|
112 [GenerateAttribute(typeof (NonInheritedAttribute), "str")]
|
|
113 public abstract void Method();
|
|
114 }
|
|
115
|
|
116 [Test, ExpectedException(typeof(TypeBuilderException))]
|
|
117 public void MismatchedAgsTest()
|
|
118 {
|
|
119 TypeAccessor.CreateInstance(typeof(BadObject));
|
|
120 }
|
|
121
|
|
122 // In FW1.1 an attribute argument may not be null, as stated in 17.2:
|
|
123 // "An attribute argument must be a constant expression,
|
|
124 // typeof expression or one-dimensional array creation expression."
|
|
125
|
|
126 public abstract class NullArgObject
|
|
127 {
|
|
128 [GenerateAttribute(typeof(NonInheritedAttribute), null, null)]
|
|
129 public abstract void Method();
|
|
130 }
|
|
131
|
|
132 [Test]
|
|
133 public void NullArgTest()
|
|
134 {
|
|
135 TypeAccessor.CreateInstance(typeof(NullArgObject));
|
|
136 }
|
|
137
|
|
138 public class CustomGenerateAttribute: GenerateAttributeAttribute
|
|
139 {
|
|
140 public CustomGenerateAttribute(): base(typeof(NonInheritedAttribute))
|
|
141 {
|
|
142 this["NamedArgument"] = "NamedValue";
|
|
143 this["Type"] = Type.GetType("System.Int32");
|
|
144 }
|
|
145 }
|
|
146
|
|
147 [CustomGenerate]
|
|
148 public abstract class CustomObject
|
|
149 {
|
|
150 }
|
|
151
|
|
152 [Test]
|
|
153 public void CustomGenerateTest()
|
|
154 {
|
|
155 CustomObject o = TypeAccessor<CustomObject>.CreateInstanceEx();
|
|
156 Type type = o.GetType();
|
|
157
|
|
158 NonInheritedAttribute attr = (NonInheritedAttribute)
|
|
159 Attribute.GetCustomAttribute(type, typeof(NonInheritedAttribute));
|
|
160
|
|
161 Assert.That(attr, Is.Not.Null);
|
|
162 Assert.That(attr.Type, Is.EqualTo(typeof(int)));
|
|
163 Assert.That(attr.NamedArgument, Is.EqualTo("NamedValue"));
|
|
164 }
|
|
165 }
|
|
166 }
|