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); }
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)); } }
public void UnderlyingField_TypeCreated() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); enumBuilder.AsType(); Assert.Equal(typeof(int), enumBuilder.UnderlyingField.FieldType); }
public void Namespace() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); enumBuilder.AsType(); Assert.Empty(enumBuilder.Namespace); }
public void Guid_TypeCreated() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); enumBuilder.CreateTypeInfo().AsType(); Assert.NotEqual(Guid.Empty, enumBuilder.GUID); }
public void Name() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum"); enumBuilder.AsType(); Assert.Equal("TestEnum", enumBuilder.Name); }
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()); }
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())); }
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); }
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); }
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]); }
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]); }
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); }
public void IsByRefLikeReturnsFalse() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.False(enumBuilder.IsByRefLike); }
public void DeclaringType() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.Null(enumBuilder.DeclaringType); }
public void DefineLiteral_EmptyLiteralName_ThrowsArgumentException(string literalName) { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.Throws <ArgumentException>("fieldName", () => enumBuilder.DefineLiteral(literalName, 1)); }
public void MakeArrayType_Int_RankLessThanOne_ThrowsIndexOutOfRange(int rank) { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum"); Assert.Throws <IndexOutOfRangeException>(() => enumBuilder.MakeArrayType(rank)); }
public void GetElementType_ThrowsNotSupportedException() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.Throws <NotSupportedException>(() => enumBuilder.GetElementType()); }
public void Guid_TypeNotCreated_ThrowsNotSupportedException() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.Throws <NotSupportedException>(() => enumBuilder.GUID); }
public void BaseType() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); Assert.Equal(typeof(Enum), enumBuilder.BaseType); }
public void DefineLiteral_NullLiteralName_ThrowsArgumentNullException() { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int)); AssertExtensions.Throws <ArgumentNullException>("fieldName", () => enumBuilder.DefineLiteral(null, 1)); }
public void DefineLiteral_InvalidLiteralValue_ThrowsArgumentException(Type underlyingType, object literalValue) { EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType); AssertExtensions.Throws <ArgumentException>(null, () => enumBuilder.DefineLiteral("LiteralName", literalValue)); }