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 public void TestGetCustomAttributesComplete ()
86 FieldBuilder field = _tb.DefineField ("name",
87 typeof(string), FieldAttributes.Public);
89 field.GetCustomAttributes (false);
93 [ExpectedException (typeof(NotSupportedException))]
94 public void TestGetCustomAttributesOfTypeIncomplete ()
96 FieldBuilder field = _tb.DefineField ("name",
97 typeof(string), FieldAttributes.Public);
98 field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
102 [ExpectedException (typeof(NotSupportedException))]
103 public void TestGetCustomAttributesOfTypeComplete ()
105 FieldBuilder field = _tb.DefineField ("name",
106 typeof(string), FieldAttributes.Public);
108 field.GetCustomAttributes (typeof(ObsoleteAttribute), false);
112 [ExpectedException (typeof(NotSupportedException))]
113 public void TestGetValueIncomplete ()
115 FieldBuilder field = _tb.DefineField ("name",
116 typeof(string), FieldAttributes.Public);
117 field.GetValue (_tb);
121 [ExpectedException (typeof(NotSupportedException))]
122 public void TestGetValueComplete ()
124 FieldBuilder field = _tb.DefineField ("name",
125 typeof(string), FieldAttributes.Public);
127 field.GetValue (_tb);
131 [ExpectedException (typeof(NotSupportedException))]
132 public void TestIsDefinedIncomplete ()
134 FieldBuilder field = _tb.DefineField ("name",
135 typeof(string), FieldAttributes.Public);
136 field.IsDefined (typeof(ObsoleteAttribute), true);
140 [ExpectedException (typeof(NotSupportedException))]
141 public void TestIsDefinedComplete ()
143 FieldBuilder field = _tb.DefineField ("name",
144 typeof(string), FieldAttributes.Public);
146 field.IsDefined (typeof(ObsoleteAttribute), true);
150 public void TestSetConstantIncomplete ()
152 FieldBuilder field = _tb.DefineField ("name",
153 typeof(string), FieldAttributes.Public);
154 field.SetConstant ("default");
158 [ExpectedException (typeof(InvalidOperationException))]
159 public void TestSetConstantComplete ()
161 FieldBuilder field = _tb.DefineField ("name",
162 typeof(string), FieldAttributes.Public);
164 field.SetConstant ("default");
168 [ExpectedException (typeof(InvalidOperationException))]
169 public void TestSetCustomAttributeCaBuilderComplete ()
171 FieldBuilder field = _tb.DefineField ("name",
172 typeof(string), FieldAttributes.Public);
175 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
179 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor,
181 Guid.NewGuid ().ToString ("D")
182 }, new FieldInfo[0], new object[0]);
184 field.SetCustomAttribute (caBuilder);
188 [ExpectedException (typeof(InvalidOperationException))]
189 public void TestSetCustomAttributeCtorComplete ()
191 FieldBuilder field = _tb.DefineField ("name",
192 typeof(string), FieldAttributes.Public);
195 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor (
200 field.SetCustomAttribute (guidCtor, new byte[] { 01,00,01,00,00 });
204 [ExpectedException (typeof(InvalidOperationException))]
205 public void TestSetMarshalComplete ()
207 FieldBuilder field = _tb.DefineField ("name",
208 typeof(string), FieldAttributes.Public);
210 field.SetMarshal (UnmanagedMarshal.DefineSafeArray (UnmanagedType.BStr));
214 [ExpectedException (typeof(InvalidOperationException))]
215 public void TestSetOffsetComplete ()
217 FieldBuilder field = _tb.DefineField ("name",
218 typeof(string), FieldAttributes.Public);
224 [ExpectedException (typeof(NotSupportedException))]
225 public void TestSetValueComplete ()
227 FieldBuilder field = _tb.DefineField ("name",
228 typeof(string), FieldAttributes.Public);
230 field.SetValue ((object) 1, 1, BindingFlags.Public, null,
231 CultureInfo.InvariantCulture);
234 // Return a unique type name
235 private string genTypeName ()
237 return "class" + (typeIndexer++);