namespace MonoTests.System.Reflection.Emit
{
+ [TestFixture]
+ public class MethodBuilderTest
+ {
+ private TypeBuilder genClass;
+ private ModuleBuilder module;
-[TestFixture]
-public class MethodBuilderTest : Assertion
-{
- private TypeBuilder genClass;
-
- private ModuleBuilder module;
-
- [SetUp]
- protected void SetUp () {
- AssemblyName assemblyName = new AssemblyName();
- assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
+ [SetUp]
+ protected void SetUp ()
+ {
+ AssemblyName assemblyName = new AssemblyName ();
+ assemblyName.Name = "MonoTests.System.Reflection.Emit.MethodBuilderTest";
- AssemblyBuilder assembly
- = Thread.GetDomain().DefineDynamicAssembly(
+ AssemblyBuilder assembly = Thread.GetDomain ().DefineDynamicAssembly (
assemblyName, AssemblyBuilderAccess.Run);
+ module = assembly.DefineDynamicModule ("module1");
+ genClass = module.DefineType (genTypeName (), TypeAttributes.Public);
+ }
- module = assembly.DefineDynamicModule("module1");
-
- genClass = module.DefineType(genTypeName (),
- TypeAttributes.Public);
- }
+ static int methodIndexer = 0;
+ static int typeIndexer = 0;
- static int methodIndexer = 0;
+ // Return a unique method name
+ private string genMethodName ()
+ {
+ return "m" + (methodIndexer++);
+ }
- static int typeIndexer = 0;
+ // Return a unique type name
+ private string genTypeName ()
+ {
+ return "class" + (typeIndexer++);
+ }
- // Return a unique method name
- private string genMethodName () {
- return "m" + (methodIndexer ++);
- }
+ [Test]
+ public void TestAttributes ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
+ Assert.AreEqual (MethodAttributes.Public, mb.Attributes);
+ }
- // Return a unique type name
- private string genTypeName () {
- return "class" + (typeIndexer ++);
- }
+ [Test]
+ public void TestCallingConvention ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ Assert.AreEqual (CallingConventions.Standard | CallingConventions.HasThis,
+ mb.CallingConvention, "CallingConvetion defaults to Standard+HasThis");
+
+ MethodBuilder mb3 = genClass.DefineMethod (
+ genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type [0]);
+ Assert.AreEqual (CallingConventions.VarArgs | CallingConventions.HasThis,
+ mb3.CallingConvention, "CallingConvetion works");
+
+ MethodBuilder mb4 = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
+ typeof (void), new Type [0]);
+ Assert.AreEqual (CallingConventions.Standard, mb4.CallingConvention,
+ "Static implies !HasThis");
+ }
- [Test]
- public void TestAttributes () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Public, typeof (void), new Type [0]);
+ [Test]
+ public void TestDeclaringType ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- AssertEquals ("Attributes works",
- MethodAttributes.Public, mb.Attributes);
- }
+ Assert.AreEqual (genClass, mb.DeclaringType, "DeclaringType works");
+ }
- [Test]
- public void TestCallingConvention () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type[0]);
- AssertEquals ("CallingConvetion defaults to Standard+HasThis",
- CallingConventions.Standard | CallingConventions.HasThis,
- mb.CallingConvention);
-
- MethodBuilder mb3 = genClass.DefineMethod (
- genMethodName (), 0, CallingConventions.VarArgs, typeof (void), new Type[0]);
- AssertEquals ("CallingConvetion works",
- CallingConventions.VarArgs | CallingConventions.HasThis,
- mb3.CallingConvention);
-
- MethodBuilder mb4 = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Static, CallingConventions.Standard,
- typeof (void), new Type [0]);
- AssertEquals ("Static implies !HasThis",
- CallingConventions.Standard,
- mb4.CallingConvention);
- }
+ [Test]
+ public void TestInitLocals ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- [Test]
- public void TestDeclaringType () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type[0]);
+ Assert.IsTrue (mb.InitLocals, "#1");
+ mb.InitLocals = false;
+ Assert.IsFalse (mb.InitLocals, "#2");
+ }
- AssertEquals ("DeclaringType works",
- genClass, mb.DeclaringType);
- }
+ [Test]
+ [ExpectedException (typeof (NotSupportedException))]
+ public void TestMethodHandleIncomplete ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- [Test]
- public void TestInitLocals () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type[0]);
+ RuntimeMethodHandle handle = mb.MethodHandle;
+ }
- Assert ("InitLocals defaults to true", mb.InitLocals);
- mb.InitLocals = false;
- Assert ("InitLocals is settable", !mb.InitLocals);
- }
-
- [Test]
- [ExpectedException (typeof(NotSupportedException))]
- public void TestMethodHandleIncomplete () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
-
- RuntimeMethodHandle handle = mb.MethodHandle;
- }
+ [Test]
+ [ExpectedException (typeof (NotSupportedException))]
+ public void TestMethodHandleComplete ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ mb.CreateMethodBody (new byte [2], 1);
+ genClass.CreateType ();
- [Test]
- [ExpectedException (typeof(NotSupportedException))]
- public void TestMethodHandleComplete () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
- mb.CreateMethodBody (new byte[2], 1);
- genClass.CreateType ();
+ RuntimeMethodHandle handle = mb.MethodHandle;
+ }
- RuntimeMethodHandle handle = mb.MethodHandle;
- }
+ [Test]
+ public void TestName ()
+ {
+ string name = genMethodName ();
+ MethodBuilder mb = genClass.DefineMethod (
+ name, 0, typeof (void), new Type [0]);
- [Test]
- public void TestName () {
- string name = genMethodName ();
- MethodBuilder mb = genClass.DefineMethod (
- name, 0, typeof (void), new Type [0]);
+ Assert.AreEqual (name, mb.Name);
+ }
- AssertEquals ("Name works", name, mb.Name);
- }
+ [Test]
+ public void TestReflectedType ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- [Test]
- public void TestReflectedType () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
+ Assert.AreEqual (genClass, mb.ReflectedType);
+ }
- AssertEquals ("ReflectedType works",
- genClass, mb.ReflectedType);
- }
+ [Test]
+ public void TestReturnType ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (Console), new Type [0]);
+ Assert.AreEqual (typeof (Console), mb.ReturnType, "#1");
- [Test]
- public void TestReturnType () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (Console), new Type [0]);
+ MethodBuilder mb2 = genClass.DefineMethod (
+ genMethodName (), 0, null, new Type [0]);
+ Assert.IsTrue (mb2.ReturnType == null || mb2.ReturnType == typeof (void), "#2");
+ }
- AssertEquals ("ReturnType works", typeof (Console),
- mb.ReturnType);
+ [Test]
+ public void TestReturnTypeCustomAttributes ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (Console), new Type [0]);
+ Assert.IsNull (mb.ReturnTypeCustomAttributes);
+ }
-
- MethodBuilder mb2 = genClass.DefineMethod (
- genMethodName (), 0, null, new Type [0]);
+ /*
+ public void TestSignature () {
+ MethodBuilder mb = genClass.DefineMethod (
+ "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
- Assert ("void ReturnType works", (mb2.ReturnType == null) || (mb2.ReturnType == typeof (void)));
- }
+ Console.WriteLine (mb.Signature);
+ }
+ */
- [Test]
- public void TestReturnTypeCustomAttributes () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (Console), new Type [0]);
+ [Test]
+ public void TestCreateMethodBody ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- AssertEquals ("ReturnTypeCustomAttributes must be null", null,
- mb.ReturnTypeCustomAttributes);
- }
+ // Clear body
+ mb.CreateMethodBody (null, 999);
- /*
- public void TestSignature () {
- MethodBuilder mb = genClass.DefineMethod (
- "m91", 0, typeof (Console), new Type [1] { typeof (Console) });
+ // Check arguments 1.
+ try {
+ mb.CreateMethodBody (new byte [1], -1);
+ Assert.Fail ("#1");
+ } catch (ArgumentOutOfRangeException) {
+ }
- Console.WriteLine (mb.Signature);
- }
- */
+ // Check arguments 2.
+ try {
+ mb.CreateMethodBody (new byte [1], 2);
+ Assert.Fail ("#2");
+ } catch (ArgumentOutOfRangeException) {
+ }
- [Test]
- public void TestCreateMethodBody () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
+ mb.CreateMethodBody (new byte [2], 1);
- // Clear body
- mb.CreateMethodBody (null, 999);
+ // Could only be called once
+ try {
+ mb.CreateMethodBody (new byte [2], 1);
+ Assert.Fail ("#3");
+ } catch (InvalidOperationException) {
+ }
- // Check arguments 1.
- try {
- mb.CreateMethodBody (new byte[1], -1);
- Fail ();
- } catch (ArgumentOutOfRangeException) {
- }
+ // Can not be called on a created type
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb2 = tb.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ ILGenerator ilgen = mb2.GetILGenerator ();
+ ilgen.Emit (OpCodes.Ret);
+ tb.CreateType ();
- // Check arguments 2.
- try {
- mb.CreateMethodBody (new byte[1], 2);
- Fail ();
- } catch (ArgumentOutOfRangeException) {
+ try {
+ mb2.CreateMethodBody (new byte [2], 1);
+ Assert.Fail ("#4");
+ } catch (InvalidOperationException) {
+ }
}
- mb.CreateMethodBody (new byte[2], 1);
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void TestDefineParameterInvalidIndexComplete ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (genMethodName (), 0, typeof (void),
+ new Type [2] { typeof(int), typeof(int) });
+ mb.CreateMethodBody (new byte [2], 1);
+ tb.CreateType ();
+ mb.DefineParameter (-5, ParameterAttributes.None, "param1");
+ }
- // Could only be called once
- try {
- mb.CreateMethodBody (new byte[2], 1);
- Fail ();
- } catch (InvalidOperationException) {
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void TestDefineParameterValidIndexComplete ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (genMethodName (), 0, typeof (void),
+ new Type [2] { typeof(int), typeof(int) });
+ mb.CreateMethodBody (new byte [2], 1);
+ tb.CreateType ();
+ mb.DefineParameter (1, ParameterAttributes.None, "param1");
}
- // Can not be called on a created type
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb2 = tb.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
- ILGenerator ilgen = mb2.GetILGenerator ();
- ilgen.Emit (OpCodes.Ret);
- tb.CreateType ();
+ [Test]
+ public void TestDefineParameter ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [2] { typeof (int), typeof (int) });
- try {
- mb2.CreateMethodBody (new byte[2], 1);
- Fail ();
- } catch (InvalidOperationException) {
- }
- }
+ // index out of range
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void TestDefineParameterInvalidIndexComplete ()
- {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type[2] {
- typeof(int), typeof(int)
- });
- mb.CreateMethodBody (new byte[2], 1);
- tb.CreateType ();
- mb.DefineParameter (-5, ParameterAttributes.None, "param1");
- }
+ // This fails on mono because the mono version accepts a 0 index
+ /*
+ try {
+ mb.DefineParameter (0, 0, "param1");
+ Fail ();
+ } catch (ArgumentOutOfRangeException) {
+ }
+ */
- [Test]
- [ExpectedException (typeof(InvalidOperationException))]
- public void TestDefineParameterValidIndexComplete ()
- {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type[2] {
- typeof(int), typeof(int)
- });
- mb.CreateMethodBody (new byte[2], 1);
- tb.CreateType ();
- mb.DefineParameter (1, ParameterAttributes.None, "param1");
- }
+ try {
+ mb.DefineParameter (3, 0, "param1");
+ Assert.Fail ("#1");
+ } catch (ArgumentOutOfRangeException) {
+ }
- [Test]
- public void TestDefineParameter () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [2] { typeof(int), typeof(int) });
+ // Normal usage
+ mb.DefineParameter (1, 0, "param1");
+ mb.DefineParameter (1, 0, "param1");
+ mb.DefineParameter (2, 0, null);
- // index out of range
+ mb.CreateMethodBody (new byte [2], 1);
+ tb.CreateType ();
+ try {
+ mb.DefineParameter (1, 0, "param1");
+ Assert.Fail ("#2");
+ } catch (InvalidOperationException) {
+ }
+ }
- // This fails on mono because the mono version accepts a 0 index
- /*
- try {
- mb.DefineParameter (0, 0, "param1");
- Fail ();
- } catch (ArgumentOutOfRangeException) {
+ [Test]
+#if NET_2_0
+ // MS.NET 2.x no longer allows a zero length method body
+ // to be emitted
+ [ExpectedException (typeof (InvalidOperationException))]
+#endif
+ public void ZeroLengthBodyTest1 ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [2] { typeof (int), typeof (int) });
+ mb.CreateMethodBody (new byte [2], 0);
+ genClass.CreateType ();
}
- */
- try {
- mb.DefineParameter (3, 0, "param1");
- Fail ();
- } catch (ArgumentOutOfRangeException) {
+ // A zero length method body can be created
+ [Test]
+ public void ZeroLengthBodyTest2 ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [2] { typeof (int), typeof (int) });
+ mb.CreateMethodBody (new byte [2], 0);
}
- // Normal usage
- mb.DefineParameter (1, 0, "param1");
- mb.DefineParameter (1, 0, "param1");
- mb.DefineParameter (2, 0, null);
+ [Test]
+ public void TestHashCode ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ string methodName = genMethodName ();
+ MethodBuilder mb = tb.DefineMethod (methodName, 0, typeof (void),
+ new Type [2] { typeof(int), typeof(int) });
+ Assert.AreEqual (methodName.GetHashCode (), mb.GetHashCode ());
+ }
- mb.CreateMethodBody (new byte[2], 1);
- tb.CreateType ();
- try {
- mb.DefineParameter (1, 0, "param1");
- Fail ();
+ [Test]
+ public void TestGetBaseDefinition ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ Assert.AreEqual (mb.GetBaseDefinition (), mb);
}
- catch (InvalidOperationException) {
+
+ [Test]
+ public void TestGetILGenerator ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+
+ // The same instance is returned on the second call
+ ILGenerator ilgen1 = mb.GetILGenerator ();
+ ILGenerator ilgen2 = mb.GetILGenerator ();
+
+ Assert.AreEqual (ilgen1, ilgen2, "#1");
+
+ // Does not work on unmanaged code
+ MethodBuilder mb2 = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ try {
+ mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
+ mb2.GetILGenerator ();
+ Assert.Fail ("#2");
+ } catch (InvalidOperationException) {
+ }
+ try {
+ mb2.SetImplementationFlags (MethodImplAttributes.Native);
+ mb2.GetILGenerator ();
+ Assert.Fail ("#3");
+ } catch (InvalidOperationException) {
+ }
}
- }
- [Test]
-#if NET_2_0
- // MS.NET 2.x no longer allows a zero length method body
- // to be emitted
- [ExpectedException (typeof (InvalidOperationException))]
-#endif
- public void ZeroLengthBodyTest1 ()
- {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [2] { typeof(int), typeof(int) });
- mb.CreateMethodBody (new byte[2], 0);
- genClass.CreateType ();
- }
+ [Test]
+ public void TestMethodImplementationFlags ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
- // A zero length method body can be created
- [Test]
- public void ZeroLengthBodyTest2 ()
- {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [2] { typeof(int), typeof(int) });
- mb.CreateMethodBody (new byte[2], 0);
- }
+ Assert.AreEqual (MethodImplAttributes.Managed | MethodImplAttributes.IL,
+ mb.GetMethodImplementationFlags (), "#1");
- [Test]
- public void TestHashCode ()
- {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- string methodName = genMethodName ();
- MethodBuilder mb = tb.DefineMethod (
- methodName, 0, typeof (void),
- new Type[2] {
- typeof(int), typeof(int)
- });
- AssertEquals ("Hashcode of method should be equal to hashcode of method name",
- methodName.GetHashCode (), mb.GetHashCode ());
- }
+ mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
- [Test]
- public void TestGetBaseDefinition () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
+ Assert.AreEqual (MethodImplAttributes.OPTIL, mb.GetMethodImplementationFlags (), "#2");
- AssertEquals ("GetBaseDefinition works",
- mb.GetBaseDefinition (), mb);
- }
+ mb.CreateMethodBody (new byte [2], 1);
+ mb.SetImplementationFlags (MethodImplAttributes.Managed);
+ tb.CreateType ();
+ try {
+ mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
+ Assert.Fail ("#3");
+ } catch (InvalidOperationException) {
+ }
+ }
- [Test]
- public void TestGetILGenerator () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
-
- // The same instance is returned on the second call
- ILGenerator ilgen1 = mb.GetILGenerator ();
- ILGenerator ilgen2 = mb.GetILGenerator ();
-
- AssertEquals ("The same ilgen is returned on the second call",
- ilgen1, ilgen2);
-
- // Does not work on unmanaged code
- MethodBuilder mb2 = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
- try {
- mb2.SetImplementationFlags (MethodImplAttributes.Unmanaged);
- mb2.GetILGenerator ();
- Fail ();
- } catch (InvalidOperationException) {
- }
- try {
- mb2.SetImplementationFlags (MethodImplAttributes.Native);
- mb2.GetILGenerator ();
- Fail ();
- } catch (InvalidOperationException) {
+ [Test]
+ public void TestGetModule ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [0]);
+ Assert.AreEqual (module, mb.GetModule ());
}
- }
- [Test]
- public void TestMethodImplementationFlags () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
+ [Test]
+ public void TestGetParameters ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (genMethodName (), 0, typeof (void),
+ new Type [1] { typeof (void) });
+
+ /*
+ * According to the MSDN docs, this method should fail with a
+ * NotSupportedException. In reality, it throws an
+ * InvalidOperationException under MS .NET, and returns the
+ * requested data under mono.
+ */
+ /*
+ try {
+ mb.GetParameters ();
+ Fail ("#161");
+ } catch (InvalidOperationException ex) {
+ Console.WriteLine (ex);
+ }
+ */
+ }
- AssertEquals ("MethodImplementationFlags defaults to Managed+IL",
- MethodImplAttributes.Managed | MethodImplAttributes.IL,
- mb.GetMethodImplementationFlags ());
+ [Test]
+ public void TestGetToken ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ genMethodName (), 0, typeof (void), new Type [1] { typeof (void) });
+ mb.GetToken ();
+ }
- mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
+ [Test]
+ public void TestInvoke ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [1] { typeof (int) });
- AssertEquals ("SetImplementationFlags works",
- MethodImplAttributes.OPTIL,
- mb.GetMethodImplementationFlags ());
+ try {
+ mb.Invoke (null, new object [1] { 42 });
+ Assert.Fail ("#1");
+ } catch (NotSupportedException) {
+ }
- mb.CreateMethodBody (new byte[2], 1);
- mb.SetImplementationFlags (MethodImplAttributes.Managed);
- tb.CreateType ();
- try {
- mb.SetImplementationFlags (MethodImplAttributes.OPTIL);
- Fail ();
+ try {
+ mb.Invoke (null, 0, null, new object [1] { 42 }, null);
+ Assert.Fail ("#2");
+ } catch (NotSupportedException) {
+ }
}
- catch (InvalidOperationException) {
+
+ [Test]
+ [ExpectedException (typeof (NotSupportedException))]
+ public void TestIsDefined ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [1] { typeof (int) });
+ mb.IsDefined (null, true);
}
- }
- [Test]
- public void TestGetModule () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [0]);
+ [Test]
+ public void TestGetCustomAttributes_Incomplete ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [1] { typeof (int) });
- AssertEquals ("GetMethod works", module,
- mb.GetModule ());
- }
+ try {
+ mb.GetCustomAttributes (true);
+ Assert.Fail ("#1");
+ } catch (NotSupportedException) {
+ }
- [Test]
- public void TestGetParameters () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
+ try {
+ mb.GetCustomAttributes (null, true);
+ Assert.Fail ("#2");
+ } catch (NotSupportedException) {
+ }
+ }
- /*
- * According to the MSDN docs, this method should fail with a
- * NotSupportedException. In reality, it throws an
- * InvalidOperationException under MS .NET, and returns the
- * requested data under mono.
- */
- /*
- try {
- mb.GetParameters ();
- Fail ("#161");
- } catch (InvalidOperationException ex) {
- Console.WriteLine (ex);
+ [Test]
+ [Category ("NotWorking")]
+ public void TestGetCustomAttributes_Complete ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [1] { typeof (int) });
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+ genClass.CreateType ();
+
+ try {
+ mb.GetCustomAttributes (true);
+ Assert.Fail ("#1");
+ } catch (NotSupportedException) {
+ }
+
+ try {
+ mb.GetCustomAttributes (null, true);
+ Assert.Fail ("#2");
+ } catch (NotSupportedException) {
+ }
}
- */
- }
- [Test]
- public void TestGetToken () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- genMethodName (), 0, typeof (void), new Type [1] {typeof(void)});
+ [Test]
+ public void TestSetCustomAttribute ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ string name = genMethodName ();
+ MethodBuilder mb = tb.DefineMethod (
+ name, MethodAttributes.Public, typeof (void),
+ new Type [1] { typeof (int) });
- mb.GetToken ();
- }
+ // Null argument
+ try {
+ mb.SetCustomAttribute (null);
+ Assert.Fail ("#1");
+ } catch (ArgumentNullException) {
+ }
+
+ byte [] custAttrData = { 1, 0, 0, 0, 0 };
+ Type attrType = Type.GetType
+ ("System.Reflection.AssemblyKeyNameAttribute");
+ Type [] paramTypes = new Type [1];
+ paramTypes [0] = typeof (String);
+ ConstructorInfo ctorInfo =
+ attrType.GetConstructor (paramTypes);
+
+ mb.SetCustomAttribute (ctorInfo, custAttrData);
+
+ // Test MethodImplAttribute
+ mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type [1] { typeof (short) }), new object [1] { (short) MethodImplAttributes.Synchronized }));
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+
+ Type t = tb.CreateType ();
- [Test]
- public void TestInvoke () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [1] {typeof(int)});
+ Assert.AreEqual (t.GetMethod (name).GetMethodImplementationFlags (),
+ MethodImplAttributes.Synchronized, "#2");
- try {
- mb.Invoke (null, new object [1] { 42 });
- Fail ();
- } catch (NotSupportedException) {
+ // Null arguments again
+ try {
+ mb.SetCustomAttribute (null, new byte [2]);
+ Assert.Fail ("#3");
+ } catch (ArgumentNullException) {
+ }
+
+ try {
+ mb.SetCustomAttribute (ctorInfo, null);
+ Assert.Fail ("#4");
+ } catch (ArgumentNullException) {
+ }
}
- try {
- mb.Invoke (null, 0, null, new object [1] { 42 }, null);
- Fail ();
- } catch (NotSupportedException) {
+ [Test]
+ public void SetCustomAttribute_DllImport_DllName_Empty ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (genMethodName (),
+ MethodAttributes.Public, typeof (void),
+ new Type [1] { typeof (int) });
+
+ ConstructorInfo ctorInfo = typeof (DllImportAttribute).GetConstructor (
+ new Type [] { typeof (string) });
+ try {
+ mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo,
+ new object [] { string.Empty }));
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName, "#5");
+ }
}
- }
- [Test]
- [ExpectedException (typeof (NotSupportedException))]
- public void TestIsDefined () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [1] {typeof(int)});
- mb.IsDefined (null, true);
- }
+ [Test]
+ public void SetCustomAttribute_DllImport_DllName_Null ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (genMethodName (),
+ MethodAttributes.Public, typeof (void),
+ new Type [1] { typeof (int) });
- [Test]
- public void TestGetCustomAttributes () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), 0, typeof (void),
- new Type [1] {typeof(int)});
-
- try {
- mb.GetCustomAttributes (true);
- Fail ();
- } catch (NotSupportedException) {
+ ConstructorInfo ctorInfo = typeof (DllImportAttribute).GetConstructor (
+ new Type [] { typeof (string) });
+ try {
+ mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo,
+ new object [] { null }));
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName, "#5");
+ }
}
- try {
- mb.GetCustomAttributes (null, true);
- Fail ();
- } catch (NotSupportedException) {
+ [Test]
+ public void SetCustomAttribute_SuppressUnmanagedCodeSecurity ()
+ {
+ string mname = genMethodName ();
+
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (mname, MethodAttributes.Public,
+ typeof (void), new Type [] { typeof (int), typeof (string) });
+ ConstructorInfo attrCtor = typeof (SuppressUnmanagedCodeSecurityAttribute).
+ GetConstructor (new Type [0]);
+ CustomAttributeBuilder caBuilder = new CustomAttributeBuilder (
+ attrCtor, new object [0]);
+ Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#1");
+ mb.SetCustomAttribute (caBuilder);
+ //Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#2");
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+ Type emittedType = tb.CreateType ();
+ MethodInfo emittedMethod = emittedType.GetMethod (mname);
+ Assert.AreEqual (MethodAttributes.HasSecurity, emittedMethod.Attributes & MethodAttributes.HasSecurity, "#3");
+ //Assert.IsTrue ((mb.Attributes & MethodAttributes.HasSecurity) == 0, "#4");
+ object [] emittedAttrs = emittedMethod.GetCustomAttributes (typeof (SuppressUnmanagedCodeSecurityAttribute), true);
+ Assert.AreEqual (1, emittedAttrs.Length, "#5");
}
- }
- [Test]
- public void TestSetCustomAttribute () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- string name = genMethodName ();
- MethodBuilder mb = tb.DefineMethod (
- name, MethodAttributes.Public, typeof (void),
- new Type [1] {typeof(int)});
+ [AttributeUsage (AttributeTargets.Parameter)]
+ class PrivateAttribute : Attribute
+ {
- // Null argument
- try {
- mb.SetCustomAttribute (null);
- Fail ();
- } catch (ArgumentNullException) {
+ public PrivateAttribute ()
+ {
+ }
}
- byte[] custAttrData = { 1, 0, 0, 0, 0};
- Type attrType = Type.GetType
- ("System.Reflection.AssemblyKeyNameAttribute");
- Type[] paramTypes = new Type[1];
- paramTypes[0] = typeof(String);
- ConstructorInfo ctorInfo =
- attrType.GetConstructor(paramTypes);
+ [Test]
+ public void GetCustomAttributes ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public,
+ typeof (void),
+ new Type [1] { typeof (int) });
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+
+ Type attrType = typeof (ObsoleteAttribute);
+ ConstructorInfo ctorInfo =
+ attrType.GetConstructor (new Type [] { typeof (String) });
- mb.SetCustomAttribute (ctorInfo, custAttrData);
+ mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
- // Test MethodImplAttribute
- mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (MethodImplAttribute).GetConstructor (new Type[1] { typeof (short) }), new object[1] {(short)MethodImplAttributes.Synchronized}));
- mb.GetILGenerator ().Emit (OpCodes.Ret);
+ // Check that attributes not accessible are not returned
+ mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (PrivateAttribute).GetConstructor (new Type [0]), new object [] { }));
- Type t = tb.CreateType ();
+ Type t = tb.CreateType ();
- AssertEquals ("Setting MethodImplAttributes works",
- t.GetMethod (name).GetMethodImplementationFlags (),
- MethodImplAttributes.Synchronized);
+ // Try the created type
+ {
+ MethodInfo mi = t.GetMethod ("foo");
+ object [] attrs = mi.GetCustomAttributes (true);
- // Null arguments again
- try {
- mb.SetCustomAttribute (null, new byte[2]);
- Fail ();
- } catch (ArgumentNullException) {
+ Assert.AreEqual (1, attrs.Length, "#A1");
+ Assert.IsTrue (attrs [0] is ObsoleteAttribute, "#A2");
+ Assert.AreEqual ("FOO", ((ObsoleteAttribute) attrs [0]).Message, "#A3");
+ }
+
+ // Try the type builder
+ {
+ MethodInfo mi = tb.GetMethod ("foo");
+ object [] attrs = mi.GetCustomAttributes (true);
+
+ Assert.AreEqual (1, attrs.Length, "#B1");
+ Assert.IsTrue (attrs [0] is ObsoleteAttribute, "#B2");
+ Assert.AreEqual ("FOO", ((ObsoleteAttribute) attrs [0]).Message, "#B3");
+ }
+ }
+
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void TestAddDeclarativeSecurityAlreadyCreated ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Public, typeof (void),
+ new Type [0]);
+ ILGenerator ilgen = mb.GetILGenerator ();
+ ilgen.Emit (OpCodes.Ret);
+ genClass.CreateType ();
+
+ PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
+ mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
}
- try {
- mb.SetCustomAttribute (ctorInfo, null);
- Fail ();
- } catch (ArgumentNullException) {
+ [Test]
+ [ExpectedException (typeof (ArgumentNullException))]
+ public void TestAddDeclarativeSecurityNullPermissionSet ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Public, typeof (void),
+ new Type [0]);
+ mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
}
- }
- [AttributeUsage (AttributeTargets.Parameter)]
- class PrivateAttribute : Attribute {
+ [Test]
+ public void TestAddDeclarativeSecurityInvalidAction ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Public, typeof (void),
+ new Type [0]);
- public PrivateAttribute () {
+ SecurityAction [] actions = new SecurityAction [] {
+ SecurityAction.RequestMinimum,
+ SecurityAction.RequestOptional,
+ SecurityAction.RequestRefuse };
+ PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
+
+ foreach (SecurityAction action in actions) {
+ try {
+ mb.AddDeclarativeSecurity (action, set);
+ Assert.Fail ();
+ } catch (ArgumentOutOfRangeException) {
+ }
+ }
}
- }
- [Test]
- public void GetCustomAttributes () {
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public,
- typeof (void),
- new Type [1] {typeof(int)});
- mb.GetILGenerator ().Emit (OpCodes.Ret);
+ [Test]
+ [ExpectedException (typeof (InvalidOperationException))]
+ public void TestAddDeclarativeSecurityDuplicateAction ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), MethodAttributes.Public, typeof (void),
+ new Type [0]);
+ PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
+ mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
+ mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
+ }
- Type attrType = typeof (ObsoleteAttribute);
- ConstructorInfo ctorInfo =
- attrType.GetConstructor (new Type [] { typeof (String) });
+ [AttributeUsage (AttributeTargets.Parameter)]
+ class ParamAttribute : Attribute
+ {
- mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
+ public ParamAttribute ()
+ {
+ }
+ }
- // Check that attributes not accessible are not returned
- mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (PrivateAttribute).GetConstructor (new Type [0]), new object [] { }));
+ [Test]
+ public void TestDynamicParams ()
+ {
+ string mname = genMethodName ();
+
+ MethodBuilder mb = genClass.DefineMethod (
+ mname, MethodAttributes.Public, typeof (void),
+ new Type [] { typeof (int), typeof (string) });
+ ParameterBuilder pb = mb.DefineParameter (1, ParameterAttributes.In, "foo");
+ pb.SetConstant (52);
+ pb.SetCustomAttribute (new CustomAttributeBuilder (typeof (ParamAttribute).GetConstructors () [0], new object [] { }));
+ ParameterBuilder pb2 = mb.DefineParameter (2, 0, "bar");
+ pb2.SetConstant ("foo");
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+
+ Type t = genClass.CreateType ();
+ MethodInfo m = t.GetMethod (mname);
+ ParameterInfo [] pi = m.GetParameters ();
+
+ Assert.AreEqual ("foo", pi [0].Name, "#A1");
+ Assert.IsTrue (pi [0].IsIn, "#A2");
+ Assert.AreEqual (52, pi [0].DefaultValue, "#A3");
+ object [] cattrs = pi [0].GetCustomAttributes (true);
+#if NET_2_0
+ Assert.AreEqual (1, cattrs.Length, "#A4");
+ Assert.AreEqual (typeof (InAttribute), cattrs [0].GetType (), "#A5");
+#else
+ Assert.AreEqual (0, cattrs.Length, "#A4");
+#endif
- Type t = tb.CreateType ();
+ cattrs = pi [1].GetCustomAttributes (true);
+ Assert.AreEqual ("foo", pi [1].DefaultValue, "#B1");
+ }
- // Try the created type
+ [Test]
+ public void SetCustomAttribute_DllImport1 ()
{
- MethodInfo mi = t.GetMethod ("foo");
- object[] attrs = mi.GetCustomAttributes (true);
+ string mname = genMethodName ();
+
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ mname, MethodAttributes.Public, typeof (void),
+ new Type [] { typeof (int), typeof (string) });
+
+ // Create an attribute with default values
+ mb.SetCustomAttribute (new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { "kernel32" }));
- AssertEquals (1, attrs.Length);
- Assert (attrs [0] is ObsoleteAttribute);
- AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+ Type t = tb.CreateType ();
+
+ DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+
+ Assert.AreEqual (CallingConvention.Winapi, attr.CallingConvention, "#1");
+ Assert.AreEqual (mname, attr.EntryPoint, "#2");
+ Assert.AreEqual ("kernel32", attr.Value, "#3");
+ Assert.IsFalse (attr.ExactSpelling, "#4");
+ Assert.IsTrue (attr.PreserveSig, "#5");
+ Assert.IsFalse (attr.SetLastError, "#6");
+ Assert.IsFalse (attr.BestFitMapping, "#7");
+ Assert.IsFalse (attr.ThrowOnUnmappableChar, "#8");
+ }
+
+ [Test]
+ public void SetCustomAttribute_DllImport2 () {
+ string mname = genMethodName ();
+
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ mname, MethodAttributes.Public, typeof (void),
+ new Type [] { typeof (int), typeof (string) });
+
+ 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 });
+ mb.SetCustomAttribute (cb);
+
+ Type t = tb.CreateType ();
+
+ DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+
+ Assert.AreEqual (CallingConvention.StdCall, attr.CallingConvention, "#1");
+ Assert.AreEqual (CharSet.Unicode, attr.CharSet, "#2");
+ Assert.AreEqual ("bar", attr.EntryPoint, "#3");
+ Assert.AreEqual ("foo", attr.Value, "#4");
+ Assert.IsTrue (attr.ExactSpelling, "#5");
+ Assert.IsFalse (attr.PreserveSig, "#6");
+ Assert.IsFalse (attr.SetLastError, "#7");
+ Assert.IsFalse (attr.BestFitMapping, "#8");
+ Assert.IsFalse (attr.ThrowOnUnmappableChar, "#9");
}
- // Try the type builder
+ [Test]
+ public void SetCustomAttribute_DllImport3 () {
+ string mname = genMethodName ();
+
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ mname, MethodAttributes.Public, typeof (void),
+ new Type [] { typeof (int), typeof (string) });
+
+ // Test attributes with three values (on/off/missing)
+ 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 });
+ mb.SetCustomAttribute (cb);
+
+ Type t = tb.CreateType ();
+
+ DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+
+ Assert.IsFalse (attr.BestFitMapping, "#1");
+ Assert.IsFalse (attr.ThrowOnUnmappableChar, "#2");
+ }
+
+ [Test]
+ public void SetCustomAttribute_DllImport4 ()
{
- MethodInfo mi = tb.GetMethod ("foo");
- object[] attrs = mi.GetCustomAttributes (true);
+ string mname = genMethodName ();
+
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod (
+ mname, MethodAttributes.Public, typeof (void),
+ new Type [] { typeof (int), typeof (string) });
- AssertEquals (1, attrs.Length);
- Assert (attrs [0] is ObsoleteAttribute);
- AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+ 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 });
+ mb.SetCustomAttribute (cb);
+
+ Type t = tb.CreateType ();
+
+ DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+
+ Assert.IsTrue (attr.SetLastError, "#1");
+ Assert.IsTrue (attr.BestFitMapping, "#2");
+ Assert.IsTrue (attr.ThrowOnUnmappableChar, "#3");
}
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void TestAddDeclarativeSecurityAlreadyCreated () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Public, typeof (void),
- new Type [0]);
- ILGenerator ilgen = mb.GetILGenerator ();
- ilgen.Emit (OpCodes.Ret);
- genClass.CreateType ();
-
- PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
- mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
- }
+ public class GenericFoo <T> {
+ public static T field;
+ }
- [Test]
- [ExpectedException (typeof (ArgumentNullException))]
- public void TestAddDeclarativeSecurityNullPermissionSet () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Public, typeof (void),
- new Type [0]);
- mb.AddDeclarativeSecurity (SecurityAction.Demand, null);
- }
+ [Test]
+ public void ILGen_GenericTypeParameterBuilder ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("box_int",
+ MethodAttributes.Public|MethodAttributes.Static, typeof (object), new Type [] { typeof (int) });
- [Test]
- public void TestAddDeclarativeSecurityInvalidAction () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Public, typeof (void),
- new Type [0]);
+ GenericTypeParameterBuilder[] pars = mb.DefineGenericParameters (new string [] { "foo" });
- SecurityAction[] actions = new SecurityAction [] {
- SecurityAction.RequestMinimum,
- SecurityAction.RequestOptional,
- SecurityAction.RequestRefuse };
- PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
+ ILGenerator ilgen = mb.GetILGenerator ();
+ ilgen.Emit (OpCodes.Ldarg_0);
+ ilgen.Emit (OpCodes.Box, pars [0]);
+ ilgen.Emit (OpCodes.Ret);
- foreach (SecurityAction action in actions) {
- try {
- mb.AddDeclarativeSecurity (action, set);
- Fail ();
- } catch (ArgumentOutOfRangeException) {
- }
+ Type t = tb.CreateType ();
+ MethodInfo mi = t.GetMethod ("box_int");
+ MethodInfo mi2 = mi.MakeGenericMethod (new Type [] { typeof (int) });
+ Assert.AreEqual (1, mi2.Invoke (null, new object [] { 1 }));
}
- }
- [Test]
- [ExpectedException (typeof (InvalidOperationException))]
- public void TestAddDeclarativeSecurityDuplicateAction () {
- MethodBuilder mb = genClass.DefineMethod (
- genMethodName (), MethodAttributes.Public, typeof (void),
- new Type [0]);
- PermissionSet set = new PermissionSet (PermissionState.Unrestricted);
- mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
- mb.AddDeclarativeSecurity (SecurityAction.Demand, set);
- }
+ public void ILGen_InstantiatedGenericType ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("return_type",
+ MethodAttributes.Public|MethodAttributes.Static, typeof (object), new Type [] { });
+
+ GenericTypeParameterBuilder[] pars = mb.DefineGenericParameters (new string [] { "foo" });
+
+ ILGenerator ilgen = mb.GetILGenerator ();
- [AttributeUsage (AttributeTargets.Parameter)]
- class ParamAttribute : Attribute {
+ Type genericFoo = typeof (GenericFoo<int>).GetGenericTypeDefinition ().MakeGenericType (new Type [] { pars [0] });
- public ParamAttribute () {
+ ilgen.Emit (OpCodes.Ldtoken, genericFoo);
+ ilgen.Emit (OpCodes.Call, typeof (Type).GetMethod ("GetTypeFromHandle"));
+ ilgen.Emit (OpCodes.Ret);
+
+ Type t = tb.CreateType ();
+ MethodInfo mi = t.GetMethod ("box_int");
+ MethodInfo mi2 = mi.MakeGenericMethod (new Type [] { typeof (int) });
+ Assert.AreEqual (typeof (GenericFoo<int>), mi2.Invoke (null, new object [] { 1 }));
}
- }
- [Test]
- public void TestDynamicParams () {
- string mname = genMethodName ();
-
- MethodBuilder mb = genClass.DefineMethod (
- mname, MethodAttributes.Public, typeof (void),
- new Type [] { typeof (int), typeof (string) });
- ParameterBuilder pb = mb.DefineParameter (1, ParameterAttributes.In, "foo");
- pb.SetConstant (52);
- pb.SetCustomAttribute (new CustomAttributeBuilder (typeof (ParamAttribute).GetConstructors () [0], new object [] { }));
- ParameterBuilder pb2 = mb.DefineParameter (2, 0, "bar");
- pb2.SetConstant ("foo");
- mb.GetILGenerator ().Emit (OpCodes.Ret);
-
- Type t = genClass.CreateType ();
- MethodInfo m = t.GetMethod (mname);
- ParameterInfo[] pi = m.GetParameters ();
-
- AssertEquals ("foo", pi [0].Name);
- AssertEquals (true, pi [0].IsIn);
- AssertEquals (52, pi [0].DefaultValue);
- object[] cattrs = pi [0].GetCustomAttributes (true);
-
- AssertEquals ("foo", pi [1].DefaultValue);
-
-
- /* This test does not run under MS.NET: */
- /*
- AssertEquals (1, cattrs.Length);
- AssertEquals (typeof (ParamAttribute), cattrs [0].GetType ());
- */
- }
+ public void ILGen_InstantiatedTypeBuilder ()
+ {
+ TypeBuilder genericTb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ genericTb.DefineGenericParameters (new string [] { "foo" });
+ Type generatedGenericType = genericTb.CreateType ();
-#if NET_2_0
- [Test]
- public void SetCustomAttribute_DllImport1 () {
- string mname = genMethodName ();
-
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- mname, MethodAttributes.Public, typeof (void),
- new Type [] { typeof (int), typeof (string) });
-
- // Create an attribute with default values
- mb.SetCustomAttribute (new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { "kernel32" }));
-
- Type t = tb.CreateType ();
-
- DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
-
- AssertEquals (CallingConvention.Winapi, attr.CallingConvention);
- AssertEquals (mname, attr.EntryPoint);
- AssertEquals ("kernel32", attr.Value);
- AssertEquals (false, attr.ExactSpelling);
- AssertEquals (true, attr.PreserveSig);
- AssertEquals (false, attr.SetLastError);
- AssertEquals (false, attr.BestFitMapping);
- AssertEquals (false, attr.ThrowOnUnmappableChar);
- }
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("return_type",
+ MethodAttributes.Public|MethodAttributes.Static, typeof (object), new Type [] { });
- [Test]
- public void SetCustomAttribute_DllImport2 () {
- string mname = genMethodName ();
+ GenericTypeParameterBuilder[] pars = mb.DefineGenericParameters (new string [] { "foo" });
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- mname, MethodAttributes.Public, typeof (void),
- new Type [] { typeof (int), typeof (string) });
+ ILGenerator ilgen = mb.GetILGenerator ();
- 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 });
- mb.SetCustomAttribute (cb);
+ ilgen.Emit (OpCodes.Ldtoken, genericTb.MakeGenericType (new Type [] { pars [0] }));
+ ilgen.Emit (OpCodes.Call, typeof (Type).GetMethod ("GetTypeFromHandle"));
+ ilgen.Emit (OpCodes.Ret);
- Type t = tb.CreateType ();
+ Type t = tb.CreateType ();
+ MethodInfo mi = t.GetMethod ("return_type");
+ MethodInfo mi2 = mi.MakeGenericMethod (new Type [] { typeof (int) });
+ Assert.AreEqual (generatedGenericType.MakeGenericType (new Type [] { typeof (int) }), mi2.Invoke (null, new object [] { 1 }));
+ }
- DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+ [Test]
+ public void Bug354757 ()
+ {
+ TypeBuilder gtb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ gtb.DefineGenericParameters ("T");
+ MethodBuilder mb = gtb.DefineMethod ("foo", MethodAttributes.Public);
+ mb.DefineGenericParameters ("S");
+ Assert.IsTrue (mb.IsGenericMethodDefinition);
+
+ Type gt = gtb.MakeGenericType (typeof (object));
+ MethodInfo m = TypeBuilder.GetMethod (gt, mb);
+ Assert.IsTrue (m.IsGenericMethodDefinition);
+
+ MethodInfo mopen = m.MakeGenericMethod (m.GetGenericArguments ());
+ Assert.IsFalse (mopen.IsGenericMethodDefinition);
+ }
- AssertEquals (CallingConvention.StdCall, attr.CallingConvention);
- AssertEquals (CharSet.Unicode, attr.CharSet);
- AssertEquals ("bar", attr.EntryPoint);
- AssertEquals ("foo", attr.Value);
- AssertEquals (true, attr.ExactSpelling);
- AssertEquals (false, attr.PreserveSig);
- AssertEquals (false, attr.SetLastError);
- AssertEquals (false, attr.BestFitMapping);
- AssertEquals (false, attr.ThrowOnUnmappableChar);
- }
+ [Test]
+ public void DefineGenericParameters_Names_Empty ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public);
- [Test]
- public void SetCustomAttribute_DllImport3 () {
- string mname = genMethodName ();
+ try {
+ mb.DefineGenericParameters (new string [0]);
+ Assert.Fail ("#1");
+ } catch (ArgumentException ex) {
+ // Value does not fall within the expected range
+ Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
+ Assert.IsNull (ex.InnerException, "#3");
+ Assert.IsNotNull (ex.Message, "#4");
+ Assert.IsNull (ex.ParamName, "#5");
+ }
+ }
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- mname, MethodAttributes.Public, typeof (void),
- new Type [] { typeof (int), typeof (string) });
- // Test attributes with three values (on/off/missing)
- 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 });
- mb.SetCustomAttribute (cb);
- Type t = tb.CreateType ();
+ [Test]
+ public void DefineGenericParameters_Names_Null ()
+ {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public);
- DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+ try {
+ mb.DefineGenericParameters ((string []) null);
+ Assert.Fail ("#A1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
+ Assert.IsNull (ex.InnerException, "#A3");
+ Assert.IsNotNull (ex.Message, "#A4");
+ Assert.AreEqual ("names", ex.ParamName, "#A5");
+ }
- AssertEquals (false, attr.BestFitMapping);
- AssertEquals (false, attr.ThrowOnUnmappableChar);
- }
+ try {
+ mb.DefineGenericParameters ("K", null, "V");
+ Assert.Fail ("#B1");
+ } catch (ArgumentNullException ex) {
+ Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
+ Assert.IsNull (ex.InnerException, "#B3");
+ Assert.IsNotNull (ex.Message, "#B4");
+ Assert.AreEqual ("names", ex.ParamName, "#B5");
+ }
+ }
+
+
+ public static int Foo<T> (T a, T b) {
+ return 99;
+ }
- [Test]
- public void SetCustomAttribute_DllImport4 () {
- string mname = genMethodName ();
+ [Test]//bug #591226
+ public void GenericMethodIsProperlyInflated ()
+ {
+ var tb = module.DefineType ("foo");
+ var met = typeof (MethodBuilderTest).GetMethod ("Foo");
- TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
- MethodBuilder mb = tb.DefineMethod (
- mname, MethodAttributes.Public, typeof (void),
- new Type [] { typeof (int), typeof (string) });
+ var mb = tb.DefineMethod ("myFunc", MethodAttributes.Public | MethodAttributes.Static, typeof (int), Type.EmptyTypes);
+ var garg = mb.DefineGenericParameters ("a") [0];
+ mb.SetParameters (garg, garg);
- 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 });
- mb.SetCustomAttribute (cb);
+ var ilgen = mb.GetILGenerator ();
+ ilgen.Emit (OpCodes.Ldarg_0);
+ ilgen.Emit (OpCodes.Ldarg_1);
+ ilgen.Emit (OpCodes.Call, met.MakeGenericMethod (garg));
+ ilgen.Emit (OpCodes.Ret);
- Type t = tb.CreateType ();
+ var res = tb.CreateType ();
+ var mm = res.GetMethod ("myFunc").MakeGenericMethod (typeof (int));
- DllImportAttribute attr = (DllImportAttribute)((t.GetMethod (mname).GetCustomAttributes (typeof (DllImportAttribute), true)) [0]);
+ var rt = mm.Invoke (null, new object[] { 10, 20 });
+ Assert.AreEqual (99, rt, "#1");
+ }
- AssertEquals (true, attr.SetLastError);
- AssertEquals (true, attr.BestFitMapping);
- AssertEquals (true, attr.ThrowOnUnmappableChar);
}
-#endif
-}
}