public void SetReturnType_GenericAndNonGenericParameters()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);

            string[] typeParamNames = new string[] { "T", "U" };
            GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters(typeParamNames);

            Type returnType = typeof(void);

            method.SetReturnType(returnType);

            type.CreateTypeInfo().AsType();
            VerifyReturnType(method, returnType);
        }
예제 #2
0
        public void ToString_AllFieldsSet()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);

            ILGenerator ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
            GenericTypeParameterBuilder   returnType     = typeParameters[0];

            method.SetSignature(returnType.AsType(), null, null, null, null, null);
            Assert.Contains(ExpectedToStrin(method), method.ToString());
        }
예제 #3
0
        public void SetParameters_WorksAfterTypeCreated()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);

            Type[] typeParameters = method.DefineGenericParameters("T").Select(a => a.AsType()).ToArray();

            method.SetParameters(typeParameters);
            ILGenerator ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            type.CreateTypeInfo().AsType();
            method.SetParameters(typeParameters);
        }
예제 #4
0
        public void SetInterfaceConstraints_MultipleCustomInterfaces()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);

            string[] typeParamNames = new string[] { "TFirst" };
            GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(typeParamNames);

            typeParams[0].SetInterfaceConstraints(new Type[] { typeof(EmptyNonGenericInterface1), typeof(EmptyNonGenericInterface2) });
            Type resultType = type.CreateTypeInfo().AsType();

            Type[] genericTypeParams = resultType.GetGenericArguments();

            Assert.Equal(1, genericTypeParams.Length);
            Assert.Equal(new Type[] { typeof(EmptyNonGenericInterface1), typeof(EmptyNonGenericInterface2) }, genericTypeParams[0].GetTypeInfo().GetGenericParameterConstraints());
        }
        public void SetInterfaceConstraints_Null()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);

            string[] typeParamNames = new string[] { "TFirst" };
            GenericTypeParameterBuilder[] typeParams = type.DefineGenericParameters(typeParamNames);

            typeParams[0].SetInterfaceConstraints(null);
            Type resultType = type.CreateType();

            Type[] genericTypeParams = resultType.GetGenericArguments();

            Assert.Equal(1, genericTypeParams.Length);
            Assert.Equal(new Type[0], genericTypeParams[0].GetTypeInfo().GetGenericParameterConstraints());
        }
예제 #6
0
        public void SetConstant_TypeAlreadyCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder     type     = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]);
            MethodBuilder   method   = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), new Type[] { typeof(int) });

            ILGenerator methodILGenerator = method.GetILGenerator();

            methodILGenerator.Emit(OpCodes.Ldarg_0);
            methodILGenerator.Emit(OpCodes.Ldarg_1);
            methodILGenerator.Emit(OpCodes.Ret);

            type.CreateTypeInfo().AsType();
            Assert.Throws <InvalidOperationException>(() => property.SetConstant(1));
        }
예제 #7
0
        public void GetValue_ThrowsNotSupportedException()
        {
            TypeBuilder     type     = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
            MethodBuilder   method   = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), null);

            ILGenerator methodILGenerator = method.GetILGenerator();

            methodILGenerator.Emit(OpCodes.Ldarg_0);
            methodILGenerator.Emit(OpCodes.Ret);
            property.AddOtherMethod(method);

            type.CreateTypeInfo().AsType();
            Assert.Throws <NotSupportedException>(() => property.GetValue(null, null));
        }
        public void SetCustomAttribute()
        {
            TypeBuilder            type        = Helpers.DynamicType(TypeAttributes.NotPublic);
            ConstructorInfo        constructor = typeof(TypeBuilderStringAttribute).GetConstructor(new Type[] { typeof(string) });
            CustomAttributeBuilder cuatbu      = new CustomAttributeBuilder(constructor, new object[] { "hello" });

            type.SetCustomAttribute(cuatbu);

            type.CreateTypeInfo().AsType();

            object[] attributes = type.GetCustomAttributes(false).ToArray();
            Assert.Equal(1, attributes.Length);
            Assert.True(attributes[0] is TypeBuilderStringAttribute);
            Assert.Equal("hello", ((TypeBuilderStringAttribute)attributes[0]).Creator);
        }
        public void AddInterfaceImplementation_TypeAlreadyCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder testTypeBuilder = Helpers.DynamicType(TypeAttributes.Public);

            testTypeBuilder.CreateTypeInfo().AsType();

            TypeBuilder interfaceBuilder = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Interface | TypeAttributes.Public);

            interfaceBuilder.DefineMethod("TestMethod",
                                          MethodAttributes.Abstract | MethodAttributes.Virtual | MethodAttributes.Public,
                                          typeof(void),
                                          new Type[] { typeof(int), typeof(int) });
            Type createdInterface = interfaceBuilder.CreateTypeInfo().AsType();

            Assert.Throws <InvalidOperationException>(() => testTypeBuilder.AddInterfaceImplementation(createdInterface));
        }
예제 #10
0
        public void Properties()
        {
            TypeBuilder   type          = Helpers.DynamicType(TypeAttributes.Public);
            MethodBuilder methodBuilder = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), new Type[] { typeof(int?) });
            ILGenerator   generator     = methodBuilder.GetILGenerator();

            generator.Emit(OpCodes.Ret);
            ParameterBuilder parameter = methodBuilder.DefineParameter(1, ParameterAttributes.In, "paramName");

            Assert.Equal(ParameterAttributes.In, (ParameterAttributes)parameter.Attributes);
            Assert.True(parameter.IsIn);
            Assert.False(parameter.IsOptional);
            Assert.False(parameter.IsOut);
            Assert.Equal("paramName", parameter.Name);
            Assert.Equal(1, parameter.Position);
        }
예제 #11
0
        public void DefineMethodOverride_MethodNotVirtual_ThrowsTypeLoadExceptionOnCreation()
        {
            TypeBuilder   type        = Helpers.DynamicType(TypeAttributes.Public);
            MethodBuilder method      = type.DefineMethod("M", MethodAttributes.Public, typeof(int), null);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ldc_I4, 2);
            ilGenerator.Emit(OpCodes.Ret);

            type.AddInterfaceImplementation(typeof(DefineMethodOverrideInterface));
            MethodInfo declaration = typeof(DefineMethodOverrideInterface).GetMethod(method.Name);

            type.DefineMethodOverride(method, declaration);

            Assert.Throws <TypeLoadException>(() => type.CreateTypeInfo());
        }
예제 #12
0
        public void DefineNestedType_String_TypeAttributes_Type(TypeAttributes attributes)
        {
            TypeBuilder type   = Helpers.DynamicType(TypeAttributes.NotPublic);
            Type        parent = type.GetType();

            foreach (string name in new string[] { "abc", "a\0b\0cd" })
            {
                TypeBuilder nestedType = type.DefineNestedType(name, attributes, parent);
                Assert.Equal(name, nestedType.Name);
                Assert.Equal(attributes, nestedType.Attributes);
                Assert.Equal(parent, nestedType.BaseType);
                Assert.Equal(0, nestedType.Size);
                Assert.Equal(PackingSize.Unspecified, nestedType.PackingSize);
                Assert.Empty(nestedType.ImplementedInterfaces);
            }
        }
        public void GetILGenerator_Int(int size)
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Public);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[0]);

            ILGenerator ilGenerator    = method.GetILGenerator(size);
            int         expectedReturn = 5;

            ilGenerator.Emit(OpCodes.Ldc_I4, expectedReturn);
            ilGenerator.Emit(OpCodes.Ret);

            Type       createdType    = type.CreateTypeInfo().AsType();
            MethodInfo createedMethod = createdType.GetMethod("TestMethod");

            Assert.Equal(expectedReturn, createedMethod.Invoke(null, null));
        }
예제 #14
0
        public void PosTest3()
        {
            TypeBuilder   type      = Helpers.DynamicType(TypeAttributes.NotPublic);
            MethodBuilder method    = type.DefineMethod("Method", MethodAttributes.Public | MethodAttributes.Static);
            ILGenerator   generator = method.GetILGenerator();
            LocalBuilder  arg       = generator.DeclareLocal(typeof(object));

            generator.BeginExceptionBlock();

            generator.Emit(OpCodes.Ldnull, arg);
            generator.Emit(OpCodes.Ldarg_0, arg);
            generator.Emit(OpCodes.Nop, arg);

            // Try emit opcode which takes multiple args
            generator.Emit(OpCodes.Add, arg);
        }
예제 #15
0
        public void DefineField(string name, Type fieldType, FieldAttributes attributes, FieldAttributes expectedAttributes)
        {
            TypeBuilder  type  = Helpers.DynamicType(TypeAttributes.Public);
            FieldBuilder field = type.DefineField(name, fieldType, attributes);

            Assert.Equal(name, field.Name);
            Assert.Equal(fieldType, field.FieldType);
            Assert.Equal(expectedAttributes, field.Attributes);
            Assert.Equal(type.AsType(), field.DeclaringType);
            Assert.Equal(field.Module, field.Module);

            Type createdType = type.CreateTypeInfo().AsType();

            Assert.Equal(type.AsType().GetFields(Helpers.AllFlags), createdType.GetFields(Helpers.AllFlags));
            Assert.Equal(type.AsType().GetField(name, Helpers.AllFlags), createdType.GetField(name, Helpers.AllFlags));
        }
예제 #16
0
        public void GetMethod_GenericTypeMethod()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);

            type.DefineGenericParameters("T");

            MethodBuilder genericMethod = type.DefineMethod("GM", MethodAttributes.Public | MethodAttributes.Static);

            GenericTypeParameterBuilder[] methodParams = genericMethod.DefineGenericParameters("U");
            genericMethod.SetSignature(null, null, null, new Type[] { methodParams[0].AsType() }, null, null);

            MethodInfo createdGenericMethod = TypeBuilder.GetMethod(type.AsType(), genericMethod);

            Assert.True(createdGenericMethod.IsGenericMethodDefinition);
            Assert.Equal("U", createdGenericMethod.GetGenericArguments()[0].ToString());
        }
예제 #17
0
        public void GetMethod_MethodDefinitionNotInTypeGenericDefinition_ThrowsArgumentException()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);

            type.DefineGenericParameters("T");

            MethodBuilder genericMethod = type.DefineMethod("GM", MethodAttributes.Public | MethodAttributes.Static);

            GenericTypeParameterBuilder[] methodParams = genericMethod.DefineGenericParameters("U");
            genericMethod.SetSignature(null, null, null, new Type[] { methodParams[0].AsType() }, null, null);

            Type       genericIntType       = type.MakeGenericType(typeof(int));
            MethodInfo createdGenericMethod = genericMethod.MakeGenericMethod(typeof(int));

            AssertExtensions.Throws <ArgumentException>("method", () => TypeBuilder.GetMethod(genericIntType, createdGenericMethod));
        }
예제 #18
0
        public void DefineParameter_TypeAlreadyCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder type       = Helpers.DynamicType(TypeAttributes.Public);
            Type        returnType = typeof(int);

            Type[] paramTypes = new Type[] { typeof(string), typeof(object) };

            MethodBuilder method      = type.DefineMethod("TestMethod", MethodAttributes.Public, returnType, paramTypes);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            Type resultType = type.CreateType();

            Assert.Throws <InvalidOperationException>(() => method.DefineParameter(1, ParameterAttributes.Retval, "param1"));
        }
        public void IsGenericTypeDefinition_NonGenericType_ReturnsTrue()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            MethodBuilder method = type.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, null, null);

            ILGenerator ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ldstr, "Test string here.");
            MethodInfo writeLine = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });

            ilGenerator.EmitCall(OpCodes.Call, writeLine, null);
            ilGenerator.Emit(OpCodes.Ret);

            type.CreateTypeInfo().AsType();
            Assert.False(type.IsGenericTypeDefinition);
        }
예제 #20
0
        public void SetParameters_NullParameterTypes()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public);

            method.SetParameters(null);
            ILGenerator ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            Type       createdType   = type.CreateTypeInfo().AsType();
            MethodInfo createdMethod = createdType.GetMethod(method.Name);

            ParameterInfo[] parameters = createdMethod.GetParameters();
            VerifyParameters(parameters, new Type[0], null);
        }
예제 #21
0
        public void SetOffset_DifferentForTwoProperties()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);

            FieldBuilder field1 = type.DefineField("TestField1", typeof(int), FieldAttributes.Public);

            field1.SetOffset(0);

            FieldBuilder field2 = type.DefineField("TestField2", typeof(int), FieldAttributes.Public);

            field2.SetOffset(4);

            FieldBuilder field3 = type.DefineField("TestField3", typeof(int), FieldAttributes.Public);

            field3.SetOffset(4);
        }
예제 #22
0
        public void CanWrite_PublicStaticSetAccessor_ReturnsTrue()
        {
            TypeBuilder     type     = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            FieldBuilder    field    = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);

            MethodAttributes setMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
            MethodBuilder    method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) });

            ILGenerator methodILGenerator = method.GetILGenerator();

            methodILGenerator.Emit(OpCodes.Ldfld, field);
            methodILGenerator.Emit(OpCodes.Ret);

            property.SetSetMethod(method);
            Assert.True(property.CanWrite);
        }
        public void SetRaiseMethod_MultipleDifferentMethods()
        {
            TypeBuilder   type         = Helpers.DynamicType(TypeAttributes.Abstract);
            EventBuilder  eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(TestEventHandler));
            MethodBuilder method1      = type.DefineMethod("PInvokeMethod", MethodAttributes.PinvokeImpl);
            MethodBuilder method2      = type.DefineMethod("InstanceMethod", MethodAttributes.Public);
            ILGenerator   ilGenerator  = method2.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);
            MethodBuilder method3 = type.DefineMethod("StaticMethod", MethodAttributes.Static);
            MethodBuilder method4 = type.DefineMethod("AbstractMethod", MethodAttributes.Abstract | MethodAttributes.Virtual);

            eventBuilder.SetRaiseMethod(method1);
            eventBuilder.SetRaiseMethod(method2);
            eventBuilder.SetRaiseMethod(method3);
            eventBuilder.SetRaiseMethod(method4);
        }
        public void SetGetMethod_TypeAlreadyCreated_ThrowsInvalidOperationException()
        {
            TypeBuilder     type     = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
            FieldBuilder    field    = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
            PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);

            MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
            MethodBuilder    method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null);

            ILGenerator methodILGenerator = method.GetILGenerator();

            methodILGenerator.Emit(OpCodes.Ldfld, field);
            methodILGenerator.Emit(OpCodes.Ret);

            type.CreateTypeInfo().AsType();
            Assert.Throws <InvalidOperationException>(() => property.SetGetMethod(method));
        }
예제 #25
0
        public void SetParameters(Type[] parameterTypes, string[] typeParamNames)
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, typeof(void), parameterTypes);

            Type[] typeParameters = method.DefineGenericParameters(typeParamNames).Select(a => a.AsType()).ToArray();
            method.SetParameters(typeParameters);

            ILGenerator ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            Type       createdType   = type.CreateTypeInfo().AsType();
            MethodInfo createdMethod = createdType.GetMethod(method.Name);

            VerifyParameters(createdMethod.GetParameters(), typeParameters, typeParamNames);
        }
예제 #26
0
        public void DefineByRefField_ValueType_ByRefLike()
        {
            TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public, baseType: typeof(ValueType));

            // Define type to be ByRefLike
            CustomAttributeBuilder ca = new(typeof(IsByRefLikeAttribute).GetConstructors()[0], new object[] { });

            type.SetCustomAttribute(ca);

            type.DefineField("Name", typeof(int).MakeByRefType(), FieldAttributes.Public);

            Type createdType = type.CreateTypeInfo().AsType();

            FieldInfo[] fields = createdType.GetFields();
            Assert.Equal(1, fields.Length);
            Assert.True(fields[0].FieldType.IsByRef);
        }
        public void AddInterfaceImplementation(TypeAttributes typeAttributes)
        {
            TypeBuilder interfaceBuilder = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Interface | TypeAttributes.Public);

            interfaceBuilder.DefineMethod("TestMethod",
                                          MethodAttributes.Abstract | MethodAttributes.Virtual | MethodAttributes.Public,
                                          typeof(int),
                                          new Type[] { typeof(int), typeof(int) });
            Type createdInterface = interfaceBuilder.CreateTypeInfo().AsType();

            TypeBuilder type = Helpers.DynamicType(typeAttributes);

            type.AddInterfaceImplementation(createdInterface);
            Type testType = type.CreateTypeInfo().AsType();

            Assert.Equal(createdInterface, testType.GetTypeInfo().ImplementedInterfaces.Where(i => i.Name == createdInterface.Name).FirstOrDefault());
        }
        public void DefineDefaultConstructor_PrivateDefaultConstructor_ThrowsNotSupportedException(MethodAttributes attributes)
        {
            TypeBuilder baseType = Helpers.DynamicType(TypeAttributes.Public | TypeAttributes.Class);

            ConstructorBuilder constructor = baseType.DefineConstructor(attributes, CallingConventions.HasThis, new Type[] { typeof(int) });

            ILGenerator baseCtorIL = constructor.GetILGenerator();

            baseCtorIL.Emit(OpCodes.Ret);

            Type baseTestType = baseType.CreateTypeInfo().AsType();

            TypeBuilder nestedType = Helpers.DynamicType(TypeAttributes.Public | TypeAttributes.Class);

            nestedType.SetParent(baseTestType);
            Assert.Throws <NotSupportedException>(() => nestedType.DefineDefaultConstructor(MethodAttributes.Public));
        }
예제 #29
0
        public void ToString_GenericMethod()
        {
            TypeBuilder   type   = Helpers.DynamicType(TypeAttributes.Public);
            MethodBuilder method = type.DefineMethod("method1", MethodAttributes.Public, typeof(int), new Type[0]);

            method.DefineGenericParameters("T", "U", "V");
            method.MakeGenericMethod(typeof(string), typeof(int), typeof(object));

            string toString = method.ToString();

            Assert.True(toString.LastIndexOf("Name: method1") != -1 &&
                        toString.LastIndexOf("Attributes: 6") != -1 &&
                        toString.LastIndexOf("Method Signature: Length: 4") != -1 &&
                        toString.LastIndexOf("Arguments: 0") != -1 &&
                        toString.LastIndexOf("Signature:") != -1 &&
                        toString.LastIndexOf("48  3  0  8  0") != -1);
        }
        public void SetParameters_EmptyParameterTypes()
        {
            TypeBuilder   type    = Helpers.DynamicType(TypeAttributes.Abstract);
            MethodBuilder builder = type.DefineMethod("TestMethod", MethodAttributes.Public);

            builder.SetParameters(new Type[0]);
            ILGenerator ilGenerator = builder.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            Type createdType = type.CreateType();

            MethodInfo method = createdType.GetMethod(builder.Name);

            ParameterInfo[] parameters = builder.GetParameters();
            VerifyParameters(parameters, new Type[0], null);
        }