2 // FieldBuilderTest.cs - NUnit Test Cases for the FieldBuilder class
4 // Gert Driesen (drieseng@users.sourceforge.net)
6 // (C) Novell, Inc. http://www.novell.com
9 using System
.Globalization
;
10 using System
.Threading
;
11 using System
.Reflection
;
12 using System
.Reflection
.Emit
;
13 using System
.Runtime
.CompilerServices
;
14 using System
.Security
;
15 using System
.Security
.Permissions
;
16 using System
.Runtime
.InteropServices
;
18 using NUnit
.Framework
;
20 namespace MonoTests
.System
.Reflection
.Emit
23 public class FieldBuilderTest
25 private static int typeIndexer
= 0;
26 private TypeBuilder _tb
;
27 private ModuleBuilder module
;
30 protected void SetUp ()
32 AssemblyName assemblyName
= new AssemblyName ();
33 assemblyName
.Name
= "MonoTests.System.Reflection.Emit.FieldBuilderTest";
35 AssemblyBuilder assembly
= Thread
.GetDomain ().DefineDynamicAssembly (
36 assemblyName
, AssemblyBuilderAccess
.Run
);
38 module
= assembly
.DefineDynamicModule ("module1");
39 _tb
= module
.DefineType (genTypeName (), TypeAttributes
.Public
);
43 public void TestFieldProperties ()
45 FieldBuilder field
= _tb
.DefineField ("name",
46 typeof(string), FieldAttributes
.Public
);
47 Assert
.AreEqual (FieldAttributes
.Public
, field
.Attributes
);
48 Assert
.AreEqual (_tb
, field
.DeclaringType
);
49 Assert
.AreEqual (typeof(string), field
.FieldType
);
50 Assert
.AreEqual ("name", field
.Name
);
51 Assert
.AreEqual (_tb
, field
.ReflectedType
);
55 [ExpectedException (typeof(NotSupportedException
))]
56 public void TestFieldHandleIncomplete ()
58 FieldBuilder field
= _tb
.DefineField ("name",
59 typeof(string), FieldAttributes
.Public
);
60 RuntimeFieldHandle handle
= field
.FieldHandle
;
64 [ExpectedException (typeof(NotSupportedException
))]
65 public void TestFieldHandleComplete ()
67 FieldBuilder field
= _tb
.DefineField ("name",
68 typeof(string), FieldAttributes
.Public
);
70 RuntimeFieldHandle handle
= field
.FieldHandle
;
74 [ExpectedException (typeof(NotSupportedException
))]
75 public void TestGetCustomAttributesIncomplete ()
77 FieldBuilder field
= _tb
.DefineField ("name",
78 typeof(string), FieldAttributes
.Public
);
79 field
.GetCustomAttributes (false);
83 [ExpectedException (typeof(NotSupportedException
))]
84 [Ignore ("mono supports this")]
85 public void TestGetCustomAttributesComplete ()
87 FieldBuilder field
= _tb
.DefineField ("name",
88 typeof(string), FieldAttributes
.Public
);
90 field
.GetCustomAttributes (false);
94 [ExpectedException (typeof(NotSupportedException
))]
95 public void TestGetCustomAttributesOfTypeIncomplete ()
97 FieldBuilder field
= _tb
.DefineField ("name",
98 typeof(string), FieldAttributes
.Public
);
99 field
.GetCustomAttributes (typeof(ObsoleteAttribute
), false);
103 [ExpectedException (typeof(NotSupportedException
))]
104 [Ignore ("mono supports this")]
105 public void TestGetCustomAttributesOfTypeComplete ()
107 FieldBuilder field
= _tb
.DefineField ("name",
108 typeof(string), FieldAttributes
.Public
);
110 field
.GetCustomAttributes (typeof(ObsoleteAttribute
), false);
114 [ExpectedException (typeof(NotSupportedException
))]
115 public void TestGetValueIncomplete ()
117 FieldBuilder field
= _tb
.DefineField ("name",
118 typeof(string), FieldAttributes
.Public
);
119 field
.GetValue (_tb
);
123 [ExpectedException (typeof(NotSupportedException
))]
124 public void TestGetValueComplete ()
126 FieldBuilder field
= _tb
.DefineField ("name",
127 typeof(string), FieldAttributes
.Public
);
129 field
.GetValue (_tb
);
133 [ExpectedException (typeof(NotSupportedException
))]
134 public void TestIsDefinedIncomplete ()
136 FieldBuilder field
= _tb
.DefineField ("name",
137 typeof(string), FieldAttributes
.Public
);
138 field
.IsDefined (typeof(ObsoleteAttribute
), true);
142 [ExpectedException (typeof(NotSupportedException
))]
143 public void TestIsDefinedComplete ()
145 FieldBuilder field
= _tb
.DefineField ("name",
146 typeof(string), FieldAttributes
.Public
);
148 field
.IsDefined (typeof(ObsoleteAttribute
), true);
152 public void TestSetConstantIncomplete ()
154 FieldBuilder field
= _tb
.DefineField ("name",
155 typeof(string), FieldAttributes
.Public
);
156 field
.SetConstant ("default");
160 [ExpectedException (typeof(InvalidOperationException
))]
161 public void TestSetConstantComplete ()
163 FieldBuilder field
= _tb
.DefineField ("name",
164 typeof(string), FieldAttributes
.Public
);
166 field
.SetConstant ("default");
170 [ExpectedException (typeof(InvalidOperationException
))]
171 public void TestSetCustomAttributeCaBuilderComplete ()
173 FieldBuilder field
= _tb
.DefineField ("name",
174 typeof(string), FieldAttributes
.Public
);
177 ConstructorInfo guidCtor
= typeof(GuidAttribute
).GetConstructor (
181 CustomAttributeBuilder caBuilder
= new CustomAttributeBuilder (guidCtor
,
183 Guid
.NewGuid ().ToString ("D")
184 }, new FieldInfo
[0], new object[0]);
186 field
.SetCustomAttribute (caBuilder
);
190 [ExpectedException (typeof(InvalidOperationException
))]
191 public void TestSetCustomAttributeCtorComplete ()
193 FieldBuilder field
= _tb
.DefineField ("name",
194 typeof(string), FieldAttributes
.Public
);
197 ConstructorInfo guidCtor
= typeof(GuidAttribute
).GetConstructor (
202 field
.SetCustomAttribute (guidCtor
, new byte[] { 01,00,01,00,00 }
);
206 [ExpectedException (typeof(InvalidOperationException
))]
207 public void TestSetMarshalComplete ()
209 FieldBuilder field
= _tb
.DefineField ("name",
210 typeof(string), FieldAttributes
.Public
);
212 field
.SetMarshal (UnmanagedMarshal
.DefineSafeArray (UnmanagedType
.BStr
));
216 [ExpectedException (typeof(InvalidOperationException
))]
217 public void TestSetOffsetComplete ()
219 FieldBuilder field
= _tb
.DefineField ("name",
220 typeof(string), FieldAttributes
.Public
);
226 [ExpectedException (typeof(NotSupportedException
))]
227 public void TestSetValueComplete ()
229 FieldBuilder field
= _tb
.DefineField ("name",
230 typeof(string), FieldAttributes
.Public
);
232 field
.SetValue ((object) 1, 1, BindingFlags
.Public
, null,
233 CultureInfo
.InvariantCulture
);
237 public void GetCustomAttributes ()
239 FieldBuilder field
= _tb
.DefineField ("name",
240 typeof(string), FieldAttributes
.Public
);
242 Type attrType
= typeof (ObsoleteAttribute
);
243 ConstructorInfo ctorInfo
=
244 attrType
.GetConstructor (new Type
[] { typeof (String) }
);
246 field
.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo
, new object [] { "FOO" }
));
248 Type t
= _tb
.CreateType ();
250 // Try the created type
252 FieldInfo fi
= t
.GetField ("name");
253 object[] attrs
= fi
.GetCustomAttributes (true);
255 Assert
.AreEqual (1, attrs
.Length
);
256 Assert
.IsTrue (attrs
[0] is ObsoleteAttribute
);
257 Assert
.AreEqual ("FOO", ((ObsoleteAttribute
)attrs
[0]).Message
);
260 // Try the type builder
262 FieldInfo fi
= _tb
.GetField ("name");
263 object[] attrs
= fi
.GetCustomAttributes (true);
265 Assert
.AreEqual (1, attrs
.Length
);
266 Assert
.IsTrue (attrs
[0] is ObsoleteAttribute
);
267 Assert
.AreEqual ("FOO", ((ObsoleteAttribute
)attrs
[0]).Message
);
271 // Return a unique type name
272 private string genTypeName ()
274 return "class" + (typeIndexer
++);