Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public void DefineEnum_ValueType(string name, TypeAttributes visibility, Type underlyingType)
        {
            ModuleBuilder module      = Helpers.DynamicModule();
            EnumBuilder   enumBuilder = module.DefineEnum(name, visibility, underlyingType);

            Assert.True(enumBuilder.IsEnum);

            Assert.Same(module.Assembly, enumBuilder.Assembly);
            Assert.Same(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.Equal(null, enumBuilder.DeclaringType);

            Assert.True(enumBuilder.Attributes.HasFlag(visibility));
            Assert.Equal(underlyingType, enumBuilder.UnderlyingField.FieldType);

            enumBuilder.CreateTypeInfo().AsType();
        }