1 // 2 // FieldBuilderTest.cs - NUnit Test Cases for the FieldBuilder class 3 // 4 // Gert Driesen (drieseng@users.sourceforge.net) 5 // 6 // (C) Novell, Inc. http://www.novell.com 7 8 using System; 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; 17 18 using NUnit.Framework; 19 20 namespace MonoTests.System.Reflection.Emit 21 { 22 [TestFixture] 23 public class FieldBuilderTest 24 { 25 private static int typeIndexer = 0; 26 private TypeBuilder _tb; 27 private ModuleBuilder module; 28 29 [SetUp] SetUp()30 protected void SetUp () 31 { 32 AssemblyName assemblyName = new AssemblyName (); 33 assemblyName.Name = "MonoTests.System.Reflection.Emit.FieldBuilderTest"; 34 35 AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly ( 36 assemblyName, AssemblyBuilderAccess.Run); 37 38 module = assembly.DefineDynamicModule ("module1"); 39 _tb = module.DefineType (genTypeName (), TypeAttributes.Public); 40 } 41 42 [Test] TestFieldProperties()43 public void TestFieldProperties () 44 { 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); 52 } 53 54 [Test] 55 [ExpectedException (typeof(NotSupportedException))] TestFieldHandleIncomplete()56 public void TestFieldHandleIncomplete () 57 { 58 FieldBuilder field = _tb.DefineField ("name", 59 typeof(string), FieldAttributes.Public); 60 RuntimeFieldHandle handle = field.FieldHandle; 61 } 62 63 [Test] 64 [ExpectedException (typeof(NotSupportedException))] TestFieldHandleComplete()65 public void TestFieldHandleComplete () 66 { 67 FieldBuilder field = _tb.DefineField ("name", 68 typeof(string), FieldAttributes.Public); 69 _tb.CreateType (); 70 RuntimeFieldHandle handle = field.FieldHandle; 71 } 72 73 [Test] 74 [ExpectedException (typeof(NotSupportedException))] TestGetCustomAttributesIncomplete()75 public void TestGetCustomAttributesIncomplete () 76 { 77 FieldBuilder field = _tb.DefineField ("name", 78 typeof(string), FieldAttributes.Public); 79 field.GetCustomAttributes (false); 80 } 81 82 [Test] 83 [ExpectedException (typeof(NotSupportedException))] 84 [Ignore ("mono supports this")] TestGetCustomAttributesComplete()85 public void TestGetCustomAttributesComplete () 86 { 87 FieldBuilder field = _tb.DefineField ("name", 88 typeof(string), FieldAttributes.Public); 89 _tb.CreateType (); 90 field.GetCustomAttributes (false); 91 } 92 93 [Test] 94 [ExpectedException (typeof(NotSupportedException))] TestGetCustomAttributesOfTypeIncomplete()95 public void TestGetCustomAttributesOfTypeIncomplete () 96 { 97 FieldBuilder field = _tb.DefineField ("name", 98 typeof(string), FieldAttributes.Public); 99 field.GetCustomAttributes (typeof(ObsoleteAttribute), false); 100 } 101 102 [Test] 103 [ExpectedException (typeof(NotSupportedException))] 104 [Ignore ("mono supports this")] TestGetCustomAttributesOfTypeComplete()105 public void TestGetCustomAttributesOfTypeComplete () 106 { 107 FieldBuilder field = _tb.DefineField ("name", 108 typeof(string), FieldAttributes.Public); 109 _tb.CreateType (); 110 field.GetCustomAttributes (typeof(ObsoleteAttribute), false); 111 } 112 113 [Test] 114 [ExpectedException (typeof(NotSupportedException))] TestGetValueIncomplete()115 public void TestGetValueIncomplete () 116 { 117 FieldBuilder field = _tb.DefineField ("name", 118 typeof(string), FieldAttributes.Public); 119 field.GetValue (_tb); 120 } 121 122 [Test] 123 [ExpectedException (typeof(NotSupportedException))] TestGetValueComplete()124 public void TestGetValueComplete () 125 { 126 FieldBuilder field = _tb.DefineField ("name", 127 typeof(string), FieldAttributes.Public); 128 _tb.CreateType (); 129 field.GetValue (_tb); 130 } 131 132 [Test] 133 [ExpectedException (typeof(NotSupportedException))] TestIsDefinedIncomplete()134 public void TestIsDefinedIncomplete () 135 { 136 FieldBuilder field = _tb.DefineField ("name", 137 typeof(string), FieldAttributes.Public); 138 field.IsDefined (typeof(ObsoleteAttribute), true); 139 } 140 141 [Test] 142 [ExpectedException (typeof(NotSupportedException))] TestIsDefinedComplete()143 public void TestIsDefinedComplete () 144 { 145 FieldBuilder field = _tb.DefineField ("name", 146 typeof(string), FieldAttributes.Public); 147 _tb.CreateType (); 148 field.IsDefined (typeof(ObsoleteAttribute), true); 149 } 150 151 [Test] TestSetConstantIncomplete()152 public void TestSetConstantIncomplete () 153 { 154 FieldBuilder field = _tb.DefineField ("name", 155 typeof(string), FieldAttributes.Public); 156 field.SetConstant ("default"); 157 } 158 159 [Test] 160 [ExpectedException (typeof(InvalidOperationException))] TestSetConstantComplete()161 public void TestSetConstantComplete () 162 { 163 FieldBuilder field = _tb.DefineField ("name", 164 typeof(string), FieldAttributes.Public); 165 _tb.CreateType (); 166 field.SetConstant ("default"); 167 } 168 169 [Test] TestSetConstantDateTime()170 public void TestSetConstantDateTime () 171 { 172 FieldBuilder field = _tb.DefineField ("datetime", 173 typeof(DateTime), FieldAttributes.Public); 174 field.SetConstant (DateTime.MinValue); 175 _tb.CreateType (); 176 } 177 178 [Test] 179 [ExpectedException (typeof(InvalidOperationException))] TestSetCustomAttributeCaBuilderComplete()180 public void TestSetCustomAttributeCaBuilderComplete () 181 { 182 FieldBuilder field = _tb.DefineField ("name", 183 typeof(string), FieldAttributes.Public); 184 _tb.CreateType (); 185 186 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor ( 187 new Type[] { 188 typeof(string) 189 }); 190 CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (guidCtor, 191 new object[] { 192 Guid.NewGuid ().ToString ("D") 193 }, new FieldInfo[0], new object[0]); 194 195 field.SetCustomAttribute (caBuilder); 196 } 197 198 [Test] 199 [ExpectedException (typeof(InvalidOperationException))] TestSetCustomAttributeCtorComplete()200 public void TestSetCustomAttributeCtorComplete () 201 { 202 FieldBuilder field = _tb.DefineField ("name", 203 typeof(string), FieldAttributes.Public); 204 _tb.CreateType (); 205 206 ConstructorInfo guidCtor = typeof(GuidAttribute).GetConstructor ( 207 new Type[] { 208 typeof(string) 209 }); 210 211 field.SetCustomAttribute (guidCtor, new byte[] { 01,00,01,00,00 }); 212 } 213 214 [Test] 215 [ExpectedException (typeof(InvalidOperationException))] TestSetMarshalComplete()216 public void TestSetMarshalComplete () 217 { 218 FieldBuilder field = _tb.DefineField ("name", 219 typeof(string), FieldAttributes.Public); 220 _tb.CreateType (); 221 field.SetMarshal (UnmanagedMarshal.DefineSafeArray (UnmanagedType.BStr)); 222 } 223 224 [Test] 225 [ExpectedException (typeof(InvalidOperationException))] TestSetOffsetComplete()226 public void TestSetOffsetComplete () 227 { 228 FieldBuilder field = _tb.DefineField ("name", 229 typeof(string), FieldAttributes.Public); 230 _tb.CreateType (); 231 field.SetOffset (1); 232 } 233 234 [Test] 235 [ExpectedException (typeof(NotSupportedException))] TestSetValueComplete()236 public void TestSetValueComplete () 237 { 238 FieldBuilder field = _tb.DefineField ("name", 239 typeof(string), FieldAttributes.Public); 240 _tb.CreateType (); 241 field.SetValue ((object) 1, 1, BindingFlags.Public, null, 242 CultureInfo.InvariantCulture); 243 } 244 245 [Test] GetCustomAttributes()246 public void GetCustomAttributes () 247 { 248 FieldBuilder field = _tb.DefineField ("name", 249 typeof(string), FieldAttributes.Public); 250 251 Type attrType = typeof (ObsoleteAttribute); 252 ConstructorInfo ctorInfo = 253 attrType.GetConstructor (new Type [] { typeof (String) }); 254 255 field.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" })); 256 257 Type t = _tb.CreateType (); 258 259 // Try the created type 260 { 261 FieldInfo fi = t.GetField ("name"); 262 object[] attrs = fi.GetCustomAttributes (true); 263 264 Assert.AreEqual (1, attrs.Length); 265 Assert.IsTrue (attrs [0] is ObsoleteAttribute); 266 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message); 267 } 268 269 // Try the type builder 270 { 271 FieldInfo fi = _tb.GetField ("name"); 272 object[] attrs = fi.GetCustomAttributes (true); 273 274 Assert.AreEqual (1, attrs.Length); 275 Assert.IsTrue (attrs [0] is ObsoleteAttribute); 276 Assert.AreEqual ("FOO", ((ObsoleteAttribute)attrs [0]).Message); 277 } 278 } 279 280 [Test] GetRawConstantValue()281 public void GetRawConstantValue () { 282 EnumBuilder enumBuilder = module.DefineEnum ("gggg", 283 TypeAttributes.Public, typeof(int)); 284 enumBuilder.DefineLiteral ("TF", 4); 285 286 enumBuilder.CreateType (); 287 288 var fields = enumBuilder.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); 289 Assert.AreEqual (4, fields[0].GetRawConstantValue ()); 290 } 291 292 // Return a unique type name genTypeName()293 private string genTypeName () 294 { 295 return "class" + (typeIndexer++); 296 } 297 } 298 } 299