2 // MethodBuilderTest.cs - NUnit Test Cases for the MethodBuilder class
4 // Zoltan Varga (vargaz@freemail.hu)
6 // (C) Ximian, Inc. http://www.ximian.com
9 // - implement 'Signature' (what the hell it does???) and test it
10 // - implement Equals and test it
13 using System.Threading;
14 using System.Reflection;
15 using System.Reflection.Emit;
16 using System.Runtime.CompilerServices;
17 using System.Security;
18 using System.Security.Permissions;
19 using System.Runtime.InteropServices;
21 using NUnit.Framework;
23 namespace MonoTests.System.Reflection.Emit
27 public class MethodBuilderTest : Assertion
29 private TypeBuilder genClass;
31 private ModuleBuilder module;
34 protected void SetUp () {
35 AssemblyName assemblyName = new AssemblyName();
36 assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
38 AssemblyBuilder assembly
39 = Thread.GetDomain().DefineDynamicAssembly(
40 assemblyName, AssemblyBuilderAccess.Run);
42 module = assembly.DefineDynamicModule("module1");
44 genClass = module.DefineType(genTypeName (),
45 TypeAttributes.Public);
48 static int methodIndexer = 0;
50 static int typeIndexer = 0;
52 // Return a unique method name
53 private string genMethodName () {
54 return "m" + (methodIndexer ++);
57 // Return a unique type name
58 private string genTypeName () {
59 return "class" + (typeIndexer ++);
63 public void TestAttributes () {
64 MethodBuilder mb = genClass.DefineMethod (
65 genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
67 AssertEquals ("Attributes works",
68 MethodAttributes.Public, mb.Attributes);
72 public void TestCallingConvention () {
73 MethodBuilder mb = genClass.DefineMethod (
74 genMethodName (), 0, typeof (void), new Type[0]);
75 AssertEquals ("CallingConvetion defaults to Standard+HasThis",
76 CallingConventions.Standard | CallingConventions.HasThis,
77 mb.CallingConvention);
79 MethodBuilder mb3 = genClass.DefineMethod (
80 genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
81 AssertEquals ("CallingConvetion works",
82 CallingConventions.VarArgs | CallingConventions.HasThis,
83 mb3.CallingConvention);
85 MethodBuilder mb4 = genClass.DefineMethod (
86 genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
87 typeof (void), new Type [0]);
88 AssertEquals ("Static implies !HasThis",
89 CallingConventions.Standard,
90 mb4.CallingConvention);
94 public void TestDeclaringType () {
95 MethodBuilder mb = genClass.DefineMethod (
96 genMethodName (), 0, typeof (void), new Type[0]);
98 AssertEquals ("DeclaringType works",
99 genClass, mb.DeclaringType);
103 public void TestInitLocals () {
104 MethodBuilder mb = genClass.DefineMethod (
105 genMethodName (), 0, typeof (void), new Type[0]);
107 Assert ("InitLocals defaults to true", mb.InitLocals);
108 mb.InitLocals = false;
109 Assert ("InitLocals is settable", !mb.InitLocals);
113 [ExpectedException (typeof(NotSupportedException))]
114 public void TestMethodHandleIncomplete () {
115 MethodBuilder mb = genClass.DefineMethod (
116 genMethodName (), 0, typeof (void), new Type [0]);
118 RuntimeMethodHandle handle = mb.MethodHandle;
122 [ExpectedException (typeof(NotSupportedException))]
123 public void TestMethodHandleComplete () {
124 MethodBuilder mb = genClass.DefineMethod (
125 genMethodName (), 0, typeof (void), new Type [0]);
126 mb.CreateMethodBody (new byte[2], 1);
127 genClass.CreateType ();
129 RuntimeMethodHandle handle = mb.MethodHandle;
133 public void TestName () {
134 string name = genMethodName ();
135 MethodBuilder mb = genClass.DefineMethod (
136 name, 0, typeof (void), new Type [0]);
138 AssertEquals ("Name works", name, mb.Name);
142 public void TestReflectedType () {
143 MethodBuilder mb = genClass.DefineMethod (
144 genMethodName (), 0, typeof (void), new Type [0]);
146 AssertEquals ("ReflectedType works",
147 genClass, mb.ReflectedType);
151 public void TestReturnType () {
152 MethodBuilder mb = genClass.DefineMethod (
153 genMethodName (), 0, typeof (Console), new Type [0]);
155 AssertEquals ("ReturnType works", typeof (Console),
159 MethodBuilder mb2 = genClass.DefineMethod (
160 genMethodName (), 0, null, new Type [0]);
162 Assert ("void ReturnType works", (mb2.ReturnType == null) || (mb2.ReturnType == typeof (void)));
166 public void TestReturnTypeCustomAttributes () {
167 MethodBuilder mb = genClass.DefineMethod (
168 genMethodName (), 0, typeof (Console), new Type [0]);
170 AssertEquals ("ReturnTypeCustomAttributes must be null", null,
171 mb.ReturnTypeCustomAttributes);
175 public void TestSignature () {
176 MethodBuilder mb = genClass.DefineMethod (
177 "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
179 Console.WriteLine (mb.Signature);
184 public void TestCreateMethodBody () {
185 MethodBuilder mb = genClass.DefineMethod (
186 genMethodName (), 0, typeof (void), new Type [0]);
189 mb.CreateMethodBody (null, 999);
191 // Check arguments 1.
193 mb.CreateMethodBody (new byte[1], -1);
195 } catch (ArgumentOutOfRangeException) {
198 // Check arguments 2.
200 mb.CreateMethodBody (new byte[1], 2);
202 } catch (ArgumentOutOfRangeException) {
205 mb.CreateMethodBody (new byte[2], 1);
207 // Could only be called once
209 mb.CreateMethodBody (new byte[2], 1);
211 } catch (InvalidOperationException) {
214 // Can not be called on a created type
215 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
216 MethodBuilder mb2 = tb.DefineMethod (
217 genMethodName (), 0, typeof (void), new Type [0]);
218 ILGenerator ilgen = mb2.GetILGenerator ();
219 ilgen.Emit (OpCodes.Ret);
223 mb2.CreateMethodBody (new byte[2], 1);
225 } catch (InvalidOperationException) {
230 [ExpectedException (typeof(InvalidOperationException))]
231 public void TestDefineParameterInvalidIndexComplete ()
233 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
234 MethodBuilder mb = tb.DefineMethod (
235 genMethodName (), 0, typeof (void),
237 typeof(int), typeof(int)
239 mb.CreateMethodBody (new byte[2], 1);
241 mb.DefineParameter (-5, ParameterAttributes.None, "param1");
245 [ExpectedException (typeof(InvalidOperationException))]
246 public void TestDefineParameterValidIndexComplete ()
248 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
249 MethodBuilder mb = tb.DefineMethod (
250 genMethodName (), 0, typeof (void),
252 typeof(int), typeof(int)
254 mb.CreateMethodBody (new byte[2], 1);
256 mb.DefineParameter (1, ParameterAttributes.None, "param1");
260 public void TestDefineParameter () {
261 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
262 MethodBuilder mb = tb.DefineMethod (
263 genMethodName (), 0, typeof (void),
264 new Type [2] { typeof(int), typeof(int) });
266 // index out of range
268 // This fails on mono because the mono version accepts a 0 index
271 mb.DefineParameter (0, 0, "param1");
273 } catch (ArgumentOutOfRangeException) {
278 mb.DefineParameter (3, 0, "param1");
280 } catch (ArgumentOutOfRangeException) {
284 mb.DefineParameter (1, 0, "param1");
285 mb.DefineParameter (1, 0, "param1");
286 mb.DefineParameter (2, 0, null);
288 mb.CreateMethodBody (new byte[2], 1);
291 mb.DefineParameter (1, 0, "param1");
294 catch (InvalidOperationException) {
300 // MS.NET 2.x no longer allows a zero length method body
302 [ExpectedException (typeof (InvalidOperationException))]
304 public void ZeroLengthBodyTest1 ()
306 MethodBuilder mb = genClass.DefineMethod (
307 genMethodName (), 0, typeof (void),
308 new Type [2] { typeof(int), typeof(int) });
309 mb.CreateMethodBody (new byte[2], 0);
310 genClass.CreateType ();
313 // A zero length method body can be created
315 public void ZeroLengthBodyTest2 ()
317 MethodBuilder mb = genClass.DefineMethod (
318 genMethodName (), 0, typeof (void),
319 new Type [2] { typeof(int), typeof(int) });
320 mb.CreateMethodBody (new byte[2], 0);
324 public void TestHashCode ()
326 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
327 string methodName = genMethodName ();
328 MethodBuilder mb = tb.DefineMethod (
329 methodName, 0, typeof (void),
331 typeof(int), typeof(int)
333 AssertEquals ("Hashcode of method should be equal to hashcode of method name",
334 methodName.GetHashCode (), mb.GetHashCode ());
338 public void TestGetBaseDefinition () {
339 MethodBuilder mb = genClass.DefineMethod (
340 genMethodName (), 0, typeof (void), new Type [0]);
342 AssertEquals ("GetBaseDefinition works",
343 mb.GetBaseDefinition (), mb);
347 public void TestGetILGenerator () {
348 MethodBuilder mb = genClass.DefineMethod (
349 genMethodName (), 0, typeof (void), new Type [0]);
351 // The same instance is returned on the second call
352 ILGenerator ilgen1 = mb.GetILGenerator ();
353 ILGenerator ilgen2 = mb.GetILGenerator ();
355 AssertEquals ("The same ilgen is returned on the second call",
358 // Does not work on unmanaged code
359 MethodBuilder mb2 = genClass.DefineMethod (
360 genMethodName (), 0, typeof (void), new Type [0]);
362 mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
363 mb2.GetILGenerator ();
365 } catch (InvalidOperationException) {
368 mb2.SetImplementationFlags (MethodImplAttributes.Native);
369 mb2.GetILGenerator ();
371 } catch (InvalidOperationException) {
376 public void TestMethodImplementationFlags () {
377 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
378 MethodBuilder mb = tb.DefineMethod (
379 genMethodName (), 0, typeof (void), new Type [0]);
381 AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
382 MethodImplAttributes.Managed | MethodImplAttributes.IL,
383 mb.GetMethodImplementationFlags ());
385 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
387 AssertEquals ("SetImplementationFlags works",
388 MethodImplAttributes.OPTIL,
389 mb.GetMethodImplementationFlags ());
391 mb.CreateMethodBody (new byte[2], 1);
392 mb.SetImplementationFlags (MethodImplAttributes.Managed);
395 mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
398 catch (InvalidOperationException) {
403 public void TestGetModule () {
404 MethodBuilder mb = genClass.DefineMethod (
405 genMethodName (), 0, typeof (void), new Type [0]);
407 AssertEquals ("GetMethod works", module,
412 public void TestGetParameters () {
413 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
414 MethodBuilder mb = tb.DefineMethod (
415 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
418 * According to the MSDN docs, this method should fail with a
419 * NotSupportedException. In reality, it throws an
420 * InvalidOperationException under MS .NET, and returns the
421 * requested data under mono.
427 } catch (InvalidOperationException ex) {
428 Console.WriteLine (ex);
434 public void TestGetToken () {
435 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
436 MethodBuilder mb = tb.DefineMethod (
437 genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
443 public void TestInvoke () {
444 MethodBuilder mb = genClass.DefineMethod (
445 genMethodName (), 0, typeof (void),
446 new Type [1] {typeof(int)});
449 mb.Invoke (null, new object [1] { 42 });
451 } catch (NotSupportedException) {
455 mb.Invoke (null, 0, null, new object [1] { 42 }, null);
457 } catch (NotSupportedException) {
462 [ExpectedException (typeof (NotSupportedException))]
463 public void TestIsDefined () {
464 MethodBuilder mb = genClass.DefineMethod (
465 genMethodName (), 0, typeof (void),
466 new Type [1] {typeof(int)});
467 mb.IsDefined (null, true);
471 public void TestGetCustomAttributes () {
472 MethodBuilder mb = genClass.DefineMethod (
473 genMethodName (), 0, typeof (void),
474 new Type [1] {typeof(int)});
477 mb.GetCustomAttributes (true);
479 } catch (NotSupportedException) {
483 mb.GetCustomAttributes (null, true);
485 } catch (NotSupportedException) {
490 public void TestSetCustomAttribute () {
491 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
492 string name = genMethodName ();
493 MethodBuilder mb = tb.DefineMethod (
494 name, MethodAttributes.Public, typeof (void),
495 new Type [1] {typeof(int)});
499 mb.SetCustomAttribute (null);
501 } catch (ArgumentNullException) {
504 byte[] custAttrData = { 1, 0, 0, 0, 0};
505 Type attrType = Type.GetType
506 ("System.Reflection.AssemblyKeyNameAttribute");
507 Type[] paramTypes = new Type[1];
508 paramTypes[0] = typeof(String);
509 ConstructorInfo ctorInfo =
510 attrType.GetConstructor(paramTypes);
512 mb.SetCustomAttribute (ctorInfo, custAttrData);
514 // Test MethodImplAttribute
515 mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
516 mb.GetILGenerator ().Emit (OpCodes.Ret);
518 Type t = tb.CreateType ();
520 AssertEquals ("Setting MethodImplAttributes works",
521 t.GetMethod (name).GetMethodImplementationFlags (),
522 MethodImplAttributes.Synchronized);
524 // Null arguments again
526 mb.SetCustomAttribute (null, new byte[2]);
528 } catch (ArgumentNullException) {
532 mb.SetCustomAttribute (ctorInfo, null);
534 } catch (ArgumentNullException) {
538 [AttributeUsage (AttributeTargets.Parameter)]
539 class PrivateAttribute : Attribute {
541 public PrivateAttribute () {
546 public void GetCustomAttributes () {
547 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
548 MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public,
550 new Type [1] {typeof(int)});
551 mb.GetILGenerator ().Emit (OpCodes.Ret);
553 Type attrType = typeof (ObsoleteAttribute);
554 ConstructorInfo ctorInfo =
555 attrType.GetConstructor (new Type [] { typeof (String) });
557 mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
559 // Check that attributes not accessible are not returned
560 mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (PrivateAttribute).GetConstructor (new Type [0]), new object [] { }));
562 Type t = tb.CreateType ();
564 // Try the created type
566 MethodInfo mi = t.GetMethod ("foo");
567 object[] attrs = mi.GetCustomAttributes (true);
569 AssertEquals (1, attrs.Length);
570 Assert (attrs [0] is ObsoleteAttribute);
571 AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
574 // Try the type builder
576 MethodInfo mi = tb.GetMethod ("foo");
577 object[] attrs = mi.GetCustomAttributes (true);
579 AssertEquals (1, attrs.Length);
580 Assert (attrs [0] is ObsoleteAttribute);
581 AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
586 [ExpectedException (typeof (InvalidOperationException))]
587 public void TestAddDeclarativeSecurityAlreadyCreated () {
588 MethodBuilder mb = genClass.DefineMethod (
589 genMethodName (), MethodAttributes.Public, typeof (void),
591 ILGenerator ilgen = mb.GetILGenerator ();
592 ilgen.Emit (OpCodes.Ret);
593 genClass.CreateType ();
595 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
596 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
600 [ExpectedException (typeof (ArgumentNullException))]
601 public void TestAddDeclarativeSecurityNullPermissionSet () {
602 MethodBuilder mb = genClass.DefineMethod (
603 genMethodName (), MethodAttributes.Public, typeof (void),
605 mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
609 public void TestAddDeclarativeSecurityInvalidAction () {
610 MethodBuilder mb = genClass.DefineMethod (
611 genMethodName (), MethodAttributes.Public, typeof (void),
614 SecurityAction[] actions = new SecurityAction [] {
615 SecurityAction.RequestMinimum,
616 SecurityAction.RequestOptional,
617 SecurityAction.RequestRefuse };
618 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
620 foreach (SecurityAction action in actions) {
622 mb.AddDeclarativeSecurity (action, set);
624 } catch (ArgumentOutOfRangeException) {
630 [ExpectedException (typeof (InvalidOperationException))]
631 public void TestAddDeclarativeSecurityDuplicateAction () {
632 MethodBuilder mb = genClass.DefineMethod (
633 genMethodName (), MethodAttributes.Public, typeof (void),
635 PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
636 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
637 mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
640 [AttributeUsage (AttributeTargets.Parameter)]
641 class ParamAttribute : Attribute {
643 public ParamAttribute () {
648 public void TestDynamicParams () {
649 string mname = genMethodName ();
651 MethodBuilder mb = genClass.DefineMethod (
652 mname, MethodAttributes.Public, typeof (void),
653 new Type [] { typeof (int), typeof (string) });
654 ParameterBuilder pb = mb.DefineParameter (1, ParameterAttributes.In, "foo");
656 pb.SetCustomAttribute (new CustomAttributeBuilder (typeof (ParamAttribute).GetConstructors () [0], new object [] { }));
657 ParameterBuilder pb2 = mb.DefineParameter (2, 0, "bar");
658 pb2.SetConstant ("foo");
659 mb.GetILGenerator ().Emit (OpCodes.Ret);
661 Type t = genClass.CreateType ();
662 MethodInfo m = t.GetMethod (mname);
663 ParameterInfo[] pi = m.GetParameters ();
665 AssertEquals ("foo", pi [0].Name);
666 AssertEquals (true, pi [0].IsIn);
667 AssertEquals (52, pi [0].DefaultValue);
668 object[] cattrs = pi [0].GetCustomAttributes (true);
670 AssertEquals ("foo", pi [1].DefaultValue);
673 /* This test does not run under MS.NET: */
675 AssertEquals (1, cattrs.Length);
676 AssertEquals (typeof (ParamAttribute), cattrs [0].GetType ());
682 public void SetCustomAttribute_DllImport1 () {
683 string mname = genMethodName ();
685 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
686 MethodBuilder mb = tb.DefineMethod (
687 mname, MethodAttributes.Public, typeof (void),
688 new Type [] { typeof (int), typeof (string) });
690 // Create an attribute with default values
691 mb.SetCustomAttribute (new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { "kernel32" }));
693 Type t = tb.CreateType ();
695 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
697 AssertEquals (CallingConvention.Winapi, attr.CallingConvention);
698 AssertEquals (mname, attr.EntryPoint);
699 AssertEquals ("kernel32", attr.Value);
700 AssertEquals (false, attr.ExactSpelling);
701 AssertEquals (true, attr.PreserveSig);
702 AssertEquals (false, attr.SetLastError);
703 AssertEquals (false, attr.BestFitMapping);
704 AssertEquals (false, attr.ThrowOnUnmappableChar);
708 public void SetCustomAttribute_DllImport2 () {
709 string mname = genMethodName ();
711 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
712 MethodBuilder mb = tb.DefineMethod (
713 mname, MethodAttributes.Public, typeof (void),
714 new Type [] { typeof (int), typeof (string) });
716 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] {typeof (DllImportAttribute).GetField ("EntryPoint"), typeof (DllImportAttribute).GetField ("CallingConvention"), typeof (DllImportAttribute).GetField ("CharSet"), typeof (DllImportAttribute).GetField ("ExactSpelling"), typeof (DllImportAttribute).GetField ("PreserveSig")}, new object [] { "bar", CallingConvention.StdCall, CharSet.Unicode, true, false });
717 mb.SetCustomAttribute (cb);
719 Type t = tb.CreateType ();
721 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
723 AssertEquals (CallingConvention.StdCall, attr.CallingConvention);
724 AssertEquals (CharSet.Unicode, attr.CharSet);
725 AssertEquals ("bar", attr.EntryPoint);
726 AssertEquals ("foo", attr.Value);
727 AssertEquals (true, attr.ExactSpelling);
728 AssertEquals (false, attr.PreserveSig);
729 AssertEquals (false, attr.SetLastError);
730 AssertEquals (false, attr.BestFitMapping);
731 AssertEquals (false, attr.ThrowOnUnmappableChar);
735 public void SetCustomAttribute_DllImport3 () {
736 string mname = genMethodName ();
738 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
739 MethodBuilder mb = tb.DefineMethod (
740 mname, MethodAttributes.Public, typeof (void),
741 new Type [] { typeof (int), typeof (string) });
743 // Test attributes with three values (on/off/missing)
744 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] { typeof (DllImportAttribute).GetField ("BestFitMapping"), typeof (DllImportAttribute).GetField ("ThrowOnUnmappableChar")}, new object [] { false, false });
745 mb.SetCustomAttribute (cb);
747 Type t = tb.CreateType ();
749 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
751 AssertEquals (false, attr.BestFitMapping);
752 AssertEquals (false, attr.ThrowOnUnmappableChar);
756 public void SetCustomAttribute_DllImport4 () {
757 string mname = genMethodName ();
759 TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
760 MethodBuilder mb = tb.DefineMethod (
761 mname, MethodAttributes.Public, typeof (void),
762 new Type [] { typeof (int), typeof (string) });
764 CustomAttributeBuilder cb = new CustomAttributeBuilder (typeof (DllImportAttribute).GetConstructor (new Type [] {typeof (String)}), new object [] { "foo" }, new FieldInfo [] { typeof (DllImportAttribute).GetField ("SetLastError"), typeof (DllImportAttribute).GetField ("BestFitMapping"), typeof (DllImportAttribute).GetField ("ThrowOnUnmappableChar")}, new object [] { true, true, true });
765 mb.SetCustomAttribute (cb);
767 Type t = tb.CreateType ();
769 DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
771 AssertEquals (true, attr.SetLastError);
772 AssertEquals (true, attr.BestFitMapping);
773 AssertEquals (true, attr.ThrowOnUnmappableChar);