public void Invoke() { AssemblyName an = new AssemblyName("moon-unit-emit-test"); AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); ModuleBuilder mb = ab.DefineDynamicModule("emit-module"); TypeBuilder tb = mb.DefineType("typeConstructorBuilder"); ConstructorBuilder cb = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Any, Type.EmptyTypes); Assert.IsNotNull(cb, ".ctor()"); cb.GetILGenerator().Emit(OpCodes.Ret); Assert.Throws <NotSupportedException> (delegate { cb.Invoke(new object [0]); }, "Invoke(object[])"); Assert.Throws <NotSupportedException> (delegate { cb.Invoke(null, new object [0]); }, "Invoke(null,object[])"); Assert.Throws <NotSupportedException> (delegate { cb.Invoke(BindingFlags.Public | BindingFlags.Instance, null, new object [0], CultureInfo.InvariantCulture); }, "Invoke(BindingFlags,Binder,object[],CultureInfo"); Assert.Throws <NotSupportedException> (delegate { cb.Invoke(null, BindingFlags.Public | BindingFlags.Instance, null, new object [0], CultureInfo.InvariantCulture); }, "Invoke(BindingFlags,Binder,object[],CultureInfo"); }
public static void VerifyConstructor(ConstructorBuilder constructor, TypeBuilder type, MethodAttributes attributes, CallingConventions callingConvention, Type[] parameterTypes) { string expectedName = (attributes & MethodAttributes.Static) != 0 ? ConstructorInfo.TypeConstructorName : ConstructorInfo.ConstructorName; Assert.Equal(expectedName, constructor.Name); Assert.Equal(attributes | MethodAttributes.SpecialName, constructor.Attributes); Assert.Equal(CallingConventions.Standard, constructor.CallingConvention); Assert.Equal(type.AsType(), constructor.DeclaringType); Assert.Equal(type.Module, constructor.Module); Assert.Equal(MethodImplAttributes.IL, constructor.MethodImplementationFlags); Assert.Throws <NotSupportedException>(() => constructor.Invoke(null)); Assert.Throws <NotSupportedException>(() => constructor.Invoke(null, null)); Type createdType = type.CreateTypeInfo().AsType(); Assert.Equal(type.AsType().GetConstructors(AllFlags), createdType.GetConstructors(AllFlags)); Assert.Equal(type.AsType().GetConstructor(parameterTypes), createdType.GetConstructor(parameterTypes)); ConstructorInfo createdConstructor = createdType.GetConstructors(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) .Single(ctor => ctor.IsStatic == constructor.IsStatic); CallingConventions expectedCallingConvention = CallingConventions.Standard; if ((callingConvention & CallingConventions.VarArgs) != 0) { expectedCallingConvention = CallingConventions.VarArgs; } if ((attributes & MethodAttributes.Static) == 0) { expectedCallingConvention |= CallingConventions.HasThis; } MethodAttributes expectedAttributes = attributes | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName; expectedAttributes &= ~MethodAttributes.RequireSecObject; Assert.Equal(expectedName, constructor.Name); Assert.Equal(expectedAttributes, createdConstructor.Attributes); Assert.Equal(expectedCallingConvention, createdConstructor.CallingConvention); Assert.Equal(createdType, createdConstructor.DeclaringType); Assert.Equal(MethodImplAttributes.IL, constructor.MethodImplementationFlags); }
[Test] // Invoke (Object, BindingFlags, Binder, Object [], CultureInfo) public void Invoke4() { ConstructorBuilder cb = genClass.DefineConstructor( 0, 0, new Type [1] { typeof(int) }); cb.GetILGenerator().Emit(OpCodes.Ret); try { cb.Invoke(null, 0, null, new object [1] { 42 }, null); Assert.Fail("#A1"); } catch (NotSupportedException ex) { // The invoked member is not supported in a dynamic // module Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#A2"); Assert.IsNull(ex.InnerException, "#A3"); Assert.IsNotNull(ex.Message, "#A4"); } genClass.CreateType(); try { cb.Invoke(null, 0, null, new object [1] { 42 }, null); Assert.Fail("#B1"); } catch (NotSupportedException ex) { // The invoked member is not supported in a dynamic // module Assert.AreEqual(typeof(NotSupportedException), ex.GetType(), "#B2"); Assert.IsNull(ex.InnerException, "#B3"); Assert.IsNotNull(ex.Message, "#B4"); } }
public void DefineConstructor(MethodAttributes methodAttributes, Type[] parameterTypes, CallingConventions callingConvention, BindingFlags bindingFlags) { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); FieldBuilder fieldBuilderA = type.DefineField("TestField", typeof(int), FieldAttributes.Private); FieldBuilder fieldBuilderB = type.DefineField("TestField", typeof(int), FieldAttributes.Private); ConstructorBuilder ctorBuilder = type.DefineConstructor(methodAttributes, callingConvention, parameterTypes); Assert.Equal(type.Module, ctorBuilder.Module); Assert.Equal(type.AsType(), ctorBuilder.DeclaringType); Assert.Throws <NotSupportedException>(() => ctorBuilder.Invoke(null)); Assert.Throws <NotSupportedException>(() => ctorBuilder.Invoke(null, null)); ILGenerator ctorIlGenerator = ctorBuilder.GetILGenerator(); if (parameterTypes.Length != 0) { //Calling base class constructor ctorIlGenerator.Emit(OpCodes.Ldarg_0); ctorIlGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0])); ctorIlGenerator.Emit(OpCodes.Ldarg_0); ctorIlGenerator.Emit(OpCodes.Ldarg_1); ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderA); ctorIlGenerator.Emit(OpCodes.Ldarg_0); ctorIlGenerator.Emit(OpCodes.Ldarg_2); ctorIlGenerator.Emit(OpCodes.Stfld, fieldBuilderB); } ctorIlGenerator.Emit(OpCodes.Ret); Type createdType = type.CreateTypeInfo().AsType(); Assert.NotNull(createdType.GetConstructors(bindingFlags).FirstOrDefault()); }