コード例 #1
0
        public void IsArray()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.False(enumBuilder.IsArray);
            Assert.False(enumBuilder.IsSZArray);

            Type asType = enumBuilder.AsType();

            Assert.False(asType.IsArray);
            Assert.False(asType.IsSZArray);

            Type arrType = enumBuilder.MakeArrayType();

            Assert.True(arrType.IsArray);
            Assert.True(arrType.IsSZArray);

            arrType = enumBuilder.MakeArrayType(1);
            Assert.True(arrType.IsArray);
            Assert.False(arrType.IsSZArray);

            arrType = enumBuilder.MakeArrayType(2);
            Assert.True(arrType.IsArray);
            Assert.False(arrType.IsSZArray);
        }
コード例 #2
0
        public void DefineLiteral(Type underlyingType, object literalValue)
        {
            EnumBuilder  enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType);
            FieldBuilder literal     = enumBuilder.DefineLiteral("FieldOne", literalValue);

            Assert.Equal("FieldOne", literal.Name);
            Assert.Equal(enumBuilder.Name, literal.DeclaringType.Name);
            Assert.Equal(FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal, literal.Attributes);
            Assert.Equal(enumBuilder.AsType(), literal.FieldType);

            Type      createdEnum    = enumBuilder.CreateTypeInfo().AsType();
            FieldInfo createdLiteral = createdEnum.GetField("FieldOne");

            Assert.Equal(createdEnum, createdLiteral.FieldType);

            if (literalValue is bool || literalValue is float || literalValue is double)
            {
                // EnumBuilder generates invalid data for non-integer enums
                Assert.Throws <FormatException>(() => createdLiteral.GetValue(null));
            }
            else
            {
                Assert.Equal(Enum.ToObject(createdEnum, literalValue), createdLiteral.GetValue(null));
            }
        }
コード例 #3
0
        public void UnderlyingField_TypeCreated()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            enumBuilder.AsType();
            Assert.Equal(typeof(int), enumBuilder.UnderlyingField.FieldType);
        }
コード例 #4
0
        public void Namespace()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            enumBuilder.AsType();
            Assert.Empty(enumBuilder.Namespace);
        }
コード例 #5
0
        public void Guid_TypeCreated()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            enumBuilder.CreateTypeInfo().AsType();
            Assert.NotEqual(Guid.Empty, enumBuilder.GUID);
        }
コード例 #6
0
        public void Name()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");

            enumBuilder.AsType();
            Assert.Equal("TestEnum", enumBuilder.Name);
        }
コード例 #7
0
        public void DefineLiteral_InvalidLiteralValue_ThrowsTypeLoadExceptionOnCreation(Type underlyingType, object literalValue)
        {
            EnumBuilder  enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType);
            FieldBuilder literal     = enumBuilder.DefineLiteral("LiteralName", literalValue);

            Assert.Throws <TypeLoadException>(() => enumBuilder.CreateTypeInfo());
        }
コード例 #8
0
        public void IsAssignableFrom()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.False(enumBuilder.IsAssignableFrom(null));
            Assert.True(enumBuilder.IsAssignableFrom(typeof(int).GetTypeInfo()));
            Assert.False(enumBuilder.IsAssignableFrom(typeof(short).GetTypeInfo()));
        }
コード例 #9
0
        public void MakePointerType()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
            Type        arrayType   = enumBuilder.MakePointerType();

            Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
            Assert.Equal("TestEnum*", arrayType.Name);
        }
コード例 #10
0
        public void DefineLiteral()
        {
            EnumBuilder  enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
            FieldBuilder field       = enumBuilder.DefineLiteral("FieldOne", 1);

            enumBuilder.AsType();
            Assert.True(field.IsLiteral);
            Assert.True(field.IsPublic);
            Assert.True(field.IsStatic);
        }
コード例 #11
0
        public void SetCustomAttribute_ConstructorInfo_ByteArray()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            enumBuilder.CreateTypeInfo().AsType();

            ConstructorInfo attributeConstructor = typeof(BoolAttribute).GetConstructor(new Type[] { typeof(bool) });

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

            Attribute[] objVals = (Attribute[])CustomAttributeExtensions.GetCustomAttributes(enumBuilder, true).ToArray();
            Assert.Equal(new BoolAttribute(true), objVals[0]);
        }
コード例 #12
0
        public void SetCustomAttribute_CustomAttributeBuilder()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            enumBuilder.CreateTypeInfo().AsType();

            ConstructorInfo        attributeConstructor = typeof(BoolAttribute).GetConstructor(new Type[] { typeof(bool) });
            CustomAttributeBuilder attributeBuilder     = new CustomAttributeBuilder(attributeConstructor, new object[] { true });

            enumBuilder.SetCustomAttribute(attributeBuilder);

            object[] objVals = enumBuilder.GetCustomAttributes(true).ToArray();
            Assert.Equal(new BoolAttribute(true), objVals[0]);
        }
コード例 #13
0
        public void MakeArrayType_Int(int rank)
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
            Type        arrayType   = enumBuilder.MakeArrayType(rank);

            string ranks = rank == 1 ? "*" : string.Empty;

            for (int i = 1; i < rank; i++)
            {
                ranks += ",";
            }

            Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
            Assert.Equal($"TestEnum[{ranks}]", arrayType.Name);
        }
コード例 #14
0
        public void IsByRefLikeReturnsFalse()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.False(enumBuilder.IsByRefLike);
        }
コード例 #15
0
        public void DeclaringType()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.Null(enumBuilder.DeclaringType);
        }
コード例 #16
0
        public void DefineLiteral_EmptyLiteralName_ThrowsArgumentException(string literalName)
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.Throws <ArgumentException>("fieldName", () => enumBuilder.DefineLiteral(literalName, 1));
        }
コード例 #17
0
        public void MakeArrayType_Int_RankLessThanOne_ThrowsIndexOutOfRange(int rank)
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");

            Assert.Throws <IndexOutOfRangeException>(() => enumBuilder.MakeArrayType(rank));
        }
コード例 #18
0
        public void GetElementType_ThrowsNotSupportedException()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.Throws <NotSupportedException>(() => enumBuilder.GetElementType());
        }
コード例 #19
0
        public void Guid_TypeNotCreated_ThrowsNotSupportedException()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.Throws <NotSupportedException>(() => enumBuilder.GUID);
        }
コード例 #20
0
        public void BaseType()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            Assert.Equal(typeof(Enum), enumBuilder.BaseType);
        }
コード例 #21
0
        public void DefineLiteral_NullLiteralName_ThrowsArgumentNullException()
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));

            AssertExtensions.Throws <ArgumentNullException>("fieldName", () => enumBuilder.DefineLiteral(null, 1));
        }
コード例 #22
0
        public void DefineLiteral_InvalidLiteralValue_ThrowsArgumentException(Type underlyingType, object literalValue)
        {
            EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType);

            AssertExtensions.Throws <ArgumentException>(null, () => enumBuilder.DefineLiteral("LiteralName", literalValue));
        }