0
|
1 using System;
|
|
2 using System.Collections;
|
|
3
|
|
4 using NUnit.Framework;
|
|
5
|
|
6 using BLToolkit.Reflection;
|
|
7 using BLToolkit.TypeBuilder;
|
|
8
|
|
9 namespace TypeBuilder
|
|
10 {
|
|
11 [TestFixture]
|
|
12 public class InstanceTypeAttributeTest
|
|
13 {
|
|
14 public class TestClass
|
|
15 {
|
|
16 public class IntFieldInstance
|
|
17 {
|
|
18 public int Value;
|
|
19 }
|
|
20
|
|
21 public class IntPropertyInstance
|
|
22 {
|
|
23 private int _value;
|
|
24 public int Value
|
|
25 {
|
|
26 get { return _value * 2; }
|
|
27 set { _value = value; }
|
|
28 }
|
|
29 }
|
|
30
|
|
31 public class ObjFieldInstance
|
|
32 {
|
|
33 public object Value;
|
|
34 }
|
|
35
|
|
36 public class ObjPropertyInstance
|
|
37 {
|
|
38 private object _value;
|
|
39 public object Value
|
|
40 {
|
|
41 get { return _value is int? (int)_value * 3: _value; }
|
|
42 set { _value = value; }
|
|
43 }
|
|
44 }
|
|
45
|
|
46 public abstract class TestObject1
|
|
47 {
|
|
48 [InstanceType(typeof(IntFieldInstance))] public abstract int IntField { get; set; }
|
|
49 [InstanceType(typeof(IntPropertyInstance))] public abstract int IntProp { get; set; }
|
|
50 [InstanceType(typeof(ObjFieldInstance))] public abstract int ObjField { get; set; }
|
|
51 [InstanceType(typeof(ObjPropertyInstance))] public abstract int ObjProp { get; set; }
|
|
52 [InstanceType(typeof(IntFieldInstance))] public abstract DayOfWeek DowField { get; set; }
|
|
53 [InstanceType(typeof(IntPropertyInstance))] public abstract DayOfWeek DowProp { get; set; }
|
|
54 [InstanceType(typeof(ObjFieldInstance))] public abstract DateTime DateField { get; set; }
|
|
55 [InstanceType(typeof(ObjPropertyInstance))] public abstract DateTime DateProp { get; set; }
|
|
56 [InstanceType(typeof(ObjFieldInstance))] public abstract ArrayList ArrField { get; set; }
|
|
57 [InstanceType(typeof(ObjPropertyInstance))] public abstract ArrayList ArrProp { get; set; }
|
|
58 }
|
|
59
|
|
60 public static void Test()
|
|
61 {
|
|
62 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
|
|
63
|
|
64 o.IntField = 10;
|
|
65 o.IntProp = 11;
|
|
66 o.ObjField = 12;
|
|
67 o.ObjProp = 13;
|
|
68
|
|
69 Assert.AreEqual(10, o.IntField);
|
|
70 Assert.AreEqual(22, o.IntProp);
|
|
71 Assert.AreEqual(12, o.ObjField);
|
|
72 Assert.AreEqual(39, o.ObjProp);
|
|
73
|
|
74 DateTime testDate = new DateTime(2000, 1, 1);
|
|
75
|
|
76 o.DowField = DayOfWeek.Monday;
|
|
77 o.DowProp = DayOfWeek.Sunday;
|
|
78 o.DateField = testDate;
|
|
79 o.DateProp = testDate;
|
|
80
|
|
81 Assert.AreEqual(DayOfWeek.Monday, o.DowField);
|
|
82 Assert.AreEqual(DayOfWeek.Sunday, o.DowProp);
|
|
83 Assert.AreEqual(testDate, o.DateField);
|
|
84 Assert.AreEqual(testDate, o.DateProp);
|
|
85
|
|
86 o.ArrField = new ArrayList(17);
|
|
87 o.ArrProp = new ArrayList(21);
|
|
88
|
|
89 Assert.AreEqual(17, o.ArrField.Capacity);
|
|
90 Assert.AreEqual(21, o.ArrProp. Capacity);
|
|
91 }
|
|
92 }
|
|
93
|
|
94 [Test]
|
|
95 public void InstanceClassTest()
|
|
96 {
|
|
97 TestClass.Test();
|
|
98 }
|
|
99
|
|
100 public class TestStruct
|
|
101 {
|
|
102 public struct IntFieldInstance
|
|
103 {
|
|
104 public int Value;
|
|
105 }
|
|
106
|
|
107 public struct IntPropertyInstance
|
|
108 {
|
|
109 private int _value;
|
|
110 public int Value
|
|
111 {
|
|
112 get { return _value * 2; }
|
|
113 set { _value = value; }
|
|
114 }
|
|
115 }
|
|
116
|
|
117 public struct ObjFieldInstance
|
|
118 {
|
|
119 public object Value;
|
|
120 }
|
|
121
|
|
122 public struct ObjPropertyInstance
|
|
123 {
|
|
124 private object _value;
|
|
125 public object Value
|
|
126 {
|
|
127 get { return _value is int? (int)_value * 3: _value; }
|
|
128 set { _value = value; }
|
|
129 }
|
|
130 }
|
|
131
|
|
132 public abstract class TestObject1
|
|
133 {
|
|
134 [InstanceType(typeof(IntFieldInstance))] public abstract int IntField { get; set; }
|
|
135 [InstanceType(typeof(IntPropertyInstance))] public abstract int IntProp { get; set; }
|
|
136 [InstanceType(typeof(ObjFieldInstance))] public abstract int ObjField { get; set; }
|
|
137 [InstanceType(typeof(ObjPropertyInstance))] public abstract int ObjProp { get; set; }
|
|
138 [InstanceType(typeof(IntFieldInstance))] public abstract DayOfWeek DowField { get; set; }
|
|
139 [InstanceType(typeof(IntPropertyInstance))] public abstract DayOfWeek DowProp { get; set; }
|
|
140 [InstanceType(typeof(ObjFieldInstance))] public abstract DateTime DateField { get; set; }
|
|
141 [InstanceType(typeof(ObjPropertyInstance))] public abstract DateTime DateProp { get; set; }
|
|
142 [InstanceType(typeof(ObjFieldInstance))] public abstract ArrayList ArrField { get; set; }
|
|
143 [InstanceType(typeof(ObjPropertyInstance))] public abstract ArrayList ArrProp { get; set; }
|
|
144 }
|
|
145
|
|
146 public static void Test()
|
|
147 {
|
|
148 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
|
|
149
|
|
150 o.IntField = 10;
|
|
151 o.IntProp = 11;
|
|
152 o.ObjField = 12;
|
|
153 o.ObjProp = 13;
|
|
154
|
|
155 Assert.AreEqual(10, o.IntField);
|
|
156 Assert.AreEqual(22, o.IntProp);
|
|
157 Assert.AreEqual(12, o.ObjField);
|
|
158 Assert.AreEqual(39, o.ObjProp);
|
|
159
|
|
160 DateTime testDate = new DateTime(2000, 1, 1);
|
|
161
|
|
162 o.DowField = DayOfWeek.Monday;
|
|
163 o.DowProp = DayOfWeek.Sunday;
|
|
164 o.DateField = testDate;
|
|
165 o.DateProp = testDate;
|
|
166
|
|
167 Assert.AreEqual(DayOfWeek.Monday, o.DowField);
|
|
168 Assert.AreEqual(DayOfWeek.Sunday, o.DowProp);
|
|
169 Assert.AreEqual(testDate, o.DateField);
|
|
170 Assert.AreEqual(testDate, o.DateProp);
|
|
171
|
|
172 o.ArrField = new ArrayList(17);
|
|
173 o.ArrProp = new ArrayList(21);
|
|
174
|
|
175 Assert.AreEqual(17, o.ArrField.Capacity);
|
|
176 Assert.AreEqual(21, o.ArrProp. Capacity);
|
|
177 }
|
|
178 }
|
|
179
|
|
180 [Test]
|
|
181 public void InstanceStructTest()
|
|
182 {
|
|
183 TestStruct.Test();
|
|
184 }
|
|
185
|
|
186 public struct IntParamInstance
|
|
187 {
|
|
188 public IntParamInstance(int value)
|
|
189 {
|
|
190 Value = value;
|
|
191 }
|
|
192
|
|
193 public int Value;
|
|
194 }
|
|
195
|
|
196 public abstract class TestObject1
|
|
197 {
|
|
198 [InstanceType(typeof(IntParamInstance), 58)] public abstract int IntField { get; set; }
|
|
199 }
|
|
200
|
|
201 [Test]
|
|
202 public void ParamTest()
|
|
203 {
|
|
204 TestObject1 o = (TestObject1)TypeAccessor.CreateInstance(typeof(TestObject1));
|
|
205
|
|
206 Assert.AreEqual(58, o.IntField);
|
|
207 }
|
|
208
|
|
209 public class Instance2
|
|
210 {
|
|
211 public Instance2(int n)
|
|
212 {
|
|
213 _n = n;
|
|
214 }
|
|
215
|
|
216 private int _n;
|
|
217
|
|
218 private int _value;
|
|
219 public int Value
|
|
220 {
|
|
221 get { return _value * _n; }
|
|
222 set { _value = value; }
|
|
223 }
|
|
224 }
|
|
225
|
|
226 [GlobalInstanceType(typeof(int), typeof(Instance2), 3)]
|
|
227 public abstract class Object2
|
|
228 {
|
|
229 [InstanceType(typeof(Instance2), 5)]
|
|
230 public abstract int Int1 { get; set; }
|
|
231 public abstract int Int2 { get; set; }
|
|
232 public abstract short Short1 { get; set; }
|
|
233 }
|
|
234
|
|
235 [Test]
|
|
236 public void GlobalParamTest()
|
|
237 {
|
|
238 Object2 o = (Object2)TypeAccessor.CreateInstance(typeof(Object2));
|
|
239
|
|
240 o.Int1 = 5;
|
|
241 o.Int2 = 5;
|
|
242 o.Short1 = 10;
|
|
243
|
|
244 Assert.AreEqual(25, o.Int1);
|
|
245 Assert.AreEqual(15, o.Int2);
|
|
246 Assert.AreEqual(10, o.Short1);
|
|
247 }
|
|
248 }
|
|
249 }
|