Esempio n. 1
0
        public void ILOffset_SomethingEmitted_ReturnsNonZero()
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            MethodBuilder method      = module.DefineGlobalMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            Assert.Equal(1, ilGenerator.ILOffset);
        }
Esempio n. 2
0
        public void TestWithStaticAndPublic(FieldAttributes attributes)
        {
            ModuleBuilder module = Helpers.DynamicModule();
            FieldBuilder  field  = module.DefineInitializedData("MyField", new byte[] { 01, 00, 01 }, attributes);

            Assert.True(field.IsStatic);
            Assert.Equal((attributes & FieldAttributes.Public) != 0, field.IsPublic);
            Assert.Equal((attributes & FieldAttributes.Private) != 0, field.IsPrivate);
            Assert.Equal(field.Name, "MyField");
        }
Esempio n. 3
0
        public void DefineType_String_TypeAttributes(TypeAttributes attributes)
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   type   = module.DefineType("TestType", attributes);

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

            Assert.Equal("TestType", createdType.Name);
            Assert.Equal(attributes, createdType.GetTypeInfo().Attributes);
        }
        public void DefineInitializedData_CreateGlobalFunctionsCalled_ThrowsInvalidOperationException()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            FieldBuilder  field  = module.DefineInitializedData("MyField", new byte[] { 1, 0, 1 }, FieldAttributes.Public);

            module.CreateGlobalFunctions();

            Assert.Null(field.DeclaringType);
            Assert.Throws <InvalidOperationException>(() => module.DefineInitializedData("MyField2", new byte[] { 1, 0, 1 }, FieldAttributes.Public));
        }
Esempio n. 5
0
        public void AddArgument_DifferentCountsForCustomModifiers_ThrowsArgumentException()
        {
            ModuleBuilder   module = Helpers.DynamicModule();
            SignatureHelper helper = SignatureHelper.GetFieldSigHelper(module);

            helper.GetSignature();

            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArgument(typeof(string), new Type[] { typeof(int) }, null));
            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArgument(typeof(string), null, new Type[] { typeof(int) }));
        }
        public void Module()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   type   = module.DefineType("TestType", TypeAttributes.Public);

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

            Assert.Equal(module, typeParams[0].Module);
        }
Esempio n. 7
0
        public void AddArgument_SignatureFinished_ThrowsArgumentException()
        {
            ModuleBuilder   module = Helpers.DynamicModule();
            SignatureHelper helper = SignatureHelper.GetFieldSigHelper(module);

            helper.GetSignature();

            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArgument(typeof(string)));
            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArgument(typeof(string), null, null));
        }
Esempio n. 8
0
        public void Size_Set_ReturnsExpected(int size)
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   type   = module.DefineType("TestType", TypeAttributes.Class | TypeAttributes.Public, null, size);

            Assert.Equal(size, type.Size);

            type.DefineGenericParameters("T", "U");
            Assert.Equal(size, type.Size);
        }
Esempio n. 9
0
        public void ParentNotCreated_ThrowsNotSupportedExceptionOnCreation()
        {
            ModuleBuilder module     = Helpers.DynamicModule();
            TypeBuilder   type       = module.DefineType("Daughter", TypeAttributes.Public);
            TypeBuilder   parentType = module.DefineType("Parent", TypeAttributes.Public);

            type.SetParent(parentType.AsType());
            Assert.Equal(parentType.AsType(), type.BaseType);

            Assert.Throws <NotSupportedException>(() => type.CreateTypeInfo());
        }
Esempio n. 10
0
        public void GetMethodSigHelper_Module_CallingConventions_Type_Length_ReturnsThree(CallingConventions callingConventions, Type type)
        {
            ModuleBuilder   module  = Helpers.DynamicModule();
            SignatureHelper helper1 = SignatureHelper.GetMethodSigHelper(module, callingConventions, type);

            Assert.Equal(3, helper1.GetSignature().Length);

            SignatureHelper helper2 = SignatureHelper.GetMethodSigHelper(null, callingConventions, type);

            Assert.Equal(3, helper2.GetSignature().Length);
        }
Esempio n. 11
0
        public void GetMethodSigHelper_Module_Type_TypeArray()
        {
            ModuleBuilder   module  = Helpers.DynamicModule();
            SignatureHelper helper1 = SignatureHelper.GetMethodSigHelper(module, typeof(string), new Type[] { typeof(char), typeof(int) });

            Assert.Equal(5, helper1.GetSignature().Length);

            SignatureHelper helper2 = SignatureHelper.GetMethodSigHelper(null, typeof(string), new Type[] { typeof(char), typeof(int) });

            Assert.Equal(5, helper2.GetSignature().Length);
        }
        public void AddArgument_DifferentCountsForCustomModifiers_ThrowsArgumentException()
        {
            ModuleBuilder   module = Helpers.DynamicModule();
            SignatureHelper helper = SignatureHelper.GetFieldSigHelper(module);

            AssertExtensions.Throws <ArgumentException>("requiredCustomModifiers", () => helper.AddArguments(new Type[] { typeof(string) }, new Type[][] { new Type[] { typeof(int), typeof(int[]) } }, null));
            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArguments(new Type[] { typeof(string) }, new Type[][] { new Type[] { typeof(int) }, new Type[] { typeof(char) } }, null));

            AssertExtensions.Throws <ArgumentException>("optionalCustomModifiers", () => helper.AddArguments(new Type[] { typeof(string) }, null, new Type[][] { new Type[] { typeof(int), typeof(int[]) } }));
            AssertExtensions.Throws <ArgumentException>(null, () => helper.AddArguments(new Type[] { typeof(string) }, null, new Type[][] { new Type[] { typeof(int) }, new Type[] { typeof(char) } }));
        }
Esempio n. 13
0
        public void GetArrayMethod_InvalidArgument_ThrowsArgumentException()
        {
            ModuleBuilder module = Helpers.DynamicModule();

            AssertExtensions.Throws <ArgumentNullException>("arrayClass", () => module.GetArrayMethod(null, "TestMethod", CallingConventions.Standard, typeof(void), new Type[0]));

            AssertExtensions.Throws <ArgumentNullException>("methodName", () => module.GetArrayMethod(typeof(string[]), null, CallingConventions.Standard, typeof(void), new Type[0]));
            AssertExtensions.Throws <ArgumentException>("methodName", () => module.GetArrayMethod(typeof(string[]), "", CallingConventions.Standard, typeof(void), new Type[0]));

            AssertExtensions.Throws <ArgumentNullException>("argument", () => module.GetArrayMethod(typeof(string[]), "TestMethod", CallingConventions.Standard, typeof(void), new Type[] { null }));
        }
Esempio n. 14
0
        public void DefineMethodOverride_GlobalMethod_ThrowsArgumentException()
        {
            ModuleBuilder module       = Helpers.DynamicModule();
            MethodBuilder globalMethod = module.DefineGlobalMethod("GlobalMethod", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[0]);

            globalMethod.GetILGenerator().Emit(OpCodes.Ret);

            TypeBuilder type = module.DefineType("Name");

            AssertExtensions.Throws <ArgumentException>(null, () => type.DefineMethodOverride(globalMethod, typeof(DefineMethodOverrideInterface).GetMethod(nameof(DefineMethodOverrideInterface.M))));
        }
Esempio n. 15
0
        public void CreateGlobalFunctions_SingleGlobalMethod()
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            MethodBuilder method      = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.EmitWriteLine("Hello World from global method.");
            ilGenerator.Emit(OpCodes.Ret);

            module.CreateGlobalFunctions();
        }
Esempio n. 16
0
        public void DeclareLocal_GlobalFunctionsCreated_ThrowsInvalidOperationException()
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            MethodBuilder method      = module.DefineGlobalMethod("method1", MethodAttributes.Public | MethodAttributes.Static, typeof(Type), new Type[0]);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.Emit(OpCodes.Ret);

            module.CreateGlobalFunctions();
            Assert.Throws <InvalidOperationException>(() => ilGenerator.DeclareLocal(typeof(int)));
        }
Esempio n. 17
0
        public void DefineEnum_DynamicUnderlyingType_Works()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            EnumBuilder   underlyingEnumTypeBuilder = module.DefineEnum("Enum1", TypeAttributes.Public, typeof(int));
            Type          underlyingEnumType        = underlyingEnumTypeBuilder.CreateTypeInfo().AsType();

            EnumBuilder enumBuilder = module.DefineEnum("Enum2", TypeAttributes.Public, underlyingEnumType);
            Type        enumType    = enumBuilder.CreateTypeInfo().AsType();

            Assert.Equal(typeof(int), Enum.GetUnderlyingType(enumType));
        }
        public void SetCustomAttribute_ConstructorInfo_ByteArray()
        {
            ModuleBuilder   module = Helpers.DynamicModule();
            ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });

            module.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });

            object[] attributes = module.GetCustomAttributes().ToArray();
            Assert.Equal(1, attributes.Length);
            Assert.True(attributes[0] is IntAllAttribute);
            Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
        }
Esempio n. 19
0
        public void CreateGlobalFunctions_CalledMultipleTimes_ThrowsInvalidOperationException()
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            MethodBuilder method      = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.EmitWriteLine("Hello World from global method.");
            ilGenerator.Emit(OpCodes.Ret);

            module.CreateGlobalFunctions();
            Assert.Throws <InvalidOperationException>(() => module.CreateGlobalFunctions());
        }
Esempio n. 20
0
        public void DefineEnum_ValueType(TypeAttributes visibility)
        {
            foreach (Type integerType in s_builtInIntegerTypes)
            {
                ModuleBuilder module      = Helpers.DynamicModule();
                EnumBuilder   enumBuilder = module.DefineEnum("MyEnum", visibility, integerType);
                Assert.True(enumBuilder.IsEnum);
                Assert.Equal("MyEnum", enumBuilder.FullName);

                enumBuilder.CreateTypeInfo().AsType();
            }
        }
        public void DefineType_String_TypeAttributes_Type_TypeCreatedInModule()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   type1  = module.DefineType("TestType1");
            Type          parent = type1.CreateTypeInfo().AsType();

            TypeBuilder type2       = module.DefineType("TestType2", TypeAttributes.NotPublic, parent);
            Type        createdType = type2.CreateTypeInfo().AsType();

            Assert.Equal("TestType2", createdType.Name);
            Assert.Equal(TypeAttributes.NotPublic, createdType.GetTypeInfo().Attributes);
            Assert.Equal(parent, createdType.GetTypeInfo().BaseType);
        }
        public void DefineUninitializedData(FieldAttributes attributes)
        {
            ModuleBuilder module = Helpers.DynamicModule();

            foreach (int size in new int[] { 1, 2, 0x003f0000 - 1 })
            {
                FieldBuilder field = module.DefineUninitializedData(size.ToString(), size, attributes);

                int expectedAttributes = ((int)attributes | (int)FieldAttributes.Static) & ~ReservedMaskFieldAttribute;
                Assert.Equal(size.ToString(), field.Name);
                Assert.Equal((FieldAttributes)expectedAttributes, field.Attributes);
            }
        }
Esempio n. 23
0
        public void Size_Set_ReturnsExpected(int size)
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   type   = module.DefineType("TestType", TypeAttributes.Class | TypeAttributes.Public, null, size);

            Assert.Equal(size, type.Size);

            type.DefineGenericParameters("T", "U");
            Assert.Equal(size, type.Size);

            // We should be able to create the type no matter the size
            Assert.Equal(type.Name, type.CreateTypeInfo().Name);
        }
Esempio n. 24
0
        public void DefineDefaultConstructor_GenericParentNotCreated_ThrowsNotSupportedException()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder   genericTypeDefinition = module.DefineType("GenericType", TypeAttributes.Public);

            genericTypeDefinition.DefineGenericParameters("T");
            Type genericParent = genericTypeDefinition.MakeGenericType(typeof(int));

            TypeBuilder type = module.DefineType("Type");

            type.SetParent(genericParent);
            Assert.Throws <NotSupportedException>(() => type.DefineDefaultConstructor(MethodAttributes.Public));
        }
Esempio n. 25
0
        private void VerificationHelperNegative(string name, TypeAttributes myTypeAttribute, Type mytype, bool flag)
        {
            ModuleBuilder myModuleBuilder = Helpers.DynamicModule();

            Assert.Throws <ArgumentException>(() =>
            {
                EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum(name, myTypeAttribute, mytype);
                if (!flag)
                {
                    myEnumBuilder = myModuleBuilder.DefineEnum(name, myTypeAttribute, typeof(int));
                }
            });
        }
        public void DefineType_NullName_ThrowsArgumentNullException()
        {
            ModuleBuilder module = Helpers.DynamicModule();

            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null));
            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic));
            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType)));

            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), PackingSize.Unspecified));
            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), 0));
            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), PackingSize.Unspecified, 0));

            AssertExtensions.Throws <ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), new Type[0]));
        }
        public void DefineType_TypeAlreadyExists_ThrowsArgumentException()
        {
            ModuleBuilder module = Helpers.DynamicModule();

            module.DefineType("TestType");
            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType"));
            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic));
            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType)));

            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), PackingSize.Unspecified));
            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), 0));
            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), PackingSize.Unspecified, 0));

            AssertExtensions.Throws <ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType), new Type[0]));
        }
Esempio n. 28
0
        public void DefineEnum(string name, TypeAttributes visibility, Type underlyingType)
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            EnumBuilder   enumBuilder = module.DefineEnum(name, visibility, underlyingType);

            Assert.True(enumBuilder.IsEnum);

            Assert.Equal(module.Assembly, enumBuilder.Assembly);
            Assert.Equal(module, enumBuilder.Module);

            Assert.Equal(name, enumBuilder.Name);
            Assert.Equal(Helpers.GetFullName(name), enumBuilder.FullName);
            Assert.Equal(enumBuilder.FullName + ", " + module.Assembly.FullName, enumBuilder.AssemblyQualifiedName);

            Assert.Equal(typeof(Enum), enumBuilder.BaseType);
            Assert.Null(enumBuilder.DeclaringType);

            Assert.Equal(visibility | TypeAttributes.Sealed, enumBuilder.Attributes);

            Assert.Equal("value__", enumBuilder.UnderlyingField.Name);
            Assert.Equal(underlyingType, enumBuilder.UnderlyingField.FieldType);
            Assert.Equal(FieldAttributes.Public | FieldAttributes.SpecialName, enumBuilder.UnderlyingField.Attributes);

            // Verify we can create the Enum properly
            TypeInfo createdEnum = enumBuilder.CreateTypeInfo();

            Assert.True(createdEnum.IsEnum);

            Assert.Equal(module.Assembly.ToString(), createdEnum.Assembly.ToString());
            Assert.Equal(module.ToString(), createdEnum.Module.ToString());

            Assert.Equal(Helpers.GetFullName(name), createdEnum.Name);
            Assert.Equal(Helpers.GetFullName(name), enumBuilder.FullName);
            Assert.Equal(enumBuilder.FullName + ", " + module.Assembly.FullName, enumBuilder.AssemblyQualifiedName);

            Assert.Equal(typeof(Enum), createdEnum.BaseType);
            Assert.Null(createdEnum.DeclaringType);

            Assert.Equal(visibility | TypeAttributes.Sealed, createdEnum.Attributes);
            Type expectedUnderlyingType = underlyingType.GetTypeInfo().IsEnum ? Enum.GetUnderlyingType(underlyingType) : underlyingType;

            Assert.Equal(expectedUnderlyingType, Enum.GetUnderlyingType(createdEnum.AsType()));

            // There should be a field called "value__" created
            FieldInfo createdUnderlyingField = createdEnum.AsType().GetField("value__", Helpers.AllFlags);

            Assert.Equal(FieldAttributes.Public | FieldAttributes.SpecialName | FieldAttributes.RTSpecialName, createdUnderlyingField.Attributes);
        }
        public void GetConstructor_DeclaringTypeOfConstructorNotGenericTypeDefinitionOfType_ThrowsArgumentException()
        {
            ModuleBuilder module = Helpers.DynamicModule();
            TypeBuilder type1 = module.DefineType("TestType1", TypeAttributes.Class | TypeAttributes.Public);
            type1.DefineGenericParameters("T");

            TypeBuilder type2 = module.DefineType("TestType2", TypeAttributes.Class | TypeAttributes.Public);
            type2.DefineGenericParameters("T");

            ConstructorBuilder ctor1 = type1.DefineDefaultConstructor(MethodAttributes.PrivateScope | MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);

            ConstructorBuilder ctor2 = type2.DefineDefaultConstructor(MethodAttributes.PrivateScope | MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);

            Type genericInt = type1.MakeGenericType(typeof(int));
            AssertExtensions.Throws<ArgumentException>("type", () => TypeBuilder.GetConstructor(genericInt, ctor2));
        }
        public void CreateGlobalFunctions_MultipleGlobalMethods()
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            MethodBuilder method      = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
            ILGenerator   ilGenerator = method.GetILGenerator();

            ilGenerator.EmitWriteLine("Hello World from global method.");
            ilGenerator.Emit(OpCodes.Ret);

            method = module.DefineGlobalMethod("MyMethod2", MethodAttributes.Static | MethodAttributes.Public,
                                               null, null);
            ilGenerator = method.GetILGenerator();
            ilGenerator.EmitWriteLine("Hello World from global method again!");

            module.CreateGlobalFunctions();
            Assert.Null(method.DeclaringType);
        }