Skip to content

Commit

Permalink
Add EnumBuilder.CreateType back in the public API (dotnet#49319)
Browse files Browse the repository at this point in the history
  • Loading branch information
eerhardt authored Mar 10, 2021
1 parent b38ea6a commit d576da2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ internal AssemblyBuilder() { }
public override string? CodeBase { get { throw null; } }
public override System.Reflection.MethodInfo? EntryPoint { get { throw null; } }
public override string? FullName { get { throw null; } }
[System.ObsoleteAttribute("The Global Assembly Cache is not supported.", DiagnosticId = "SYSLIB0005", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public override bool GlobalAssemblyCache { get { throw null; } }
public override long HostContext { get { throw null; } }
public override string ImageRuntimeVersion { get { throw null; } }
public override bool IsCollectible { get { throw null; } }
public override bool IsDynamic { get { throw null; } }
public override string Location { get { throw null; } }
public override System.Reflection.Module ManifestModule { get { throw null; } }
Expand Down Expand Up @@ -65,6 +63,7 @@ internal ConstructorBuilder() { }
public override System.Reflection.CallingConventions CallingConvention { get { throw null; } }
public override System.Type? DeclaringType { get { throw null; } }
public bool InitLocals { get { throw null; } set { } }
public override int MetadataToken { get { throw null; } }
public override System.RuntimeMethodHandle MethodHandle { get { throw null; } }
public override System.Reflection.Module Module { get { throw null; } }
public override string Name { get { throw null; } }
Expand All @@ -79,7 +78,6 @@ internal ConstructorBuilder() { }
public override object Invoke(object? obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; }
public override object Invoke(System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture) { throw null; }
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
public override int MetadataToken { get { throw null; } }
public void SetCustomAttribute(System.Reflection.ConstructorInfo con, byte[] binaryAttribute) { }
public void SetCustomAttribute(System.Reflection.Emit.CustomAttributeBuilder customBuilder) { }
public void SetImplementationFlags(System.Reflection.MethodImplAttributes attributes) { }
Expand All @@ -98,7 +96,6 @@ internal EnumBuilder() { }
public override bool IsConstructedGenericType { get { throw null; } }
public override bool IsSZArray { get { throw null; } }
public override bool IsTypeDefinition { get { throw null; } }
public override bool IsVariableBoundArray { get { throw null; } }
public override System.Reflection.Module Module { get { throw null; } }
public override string Name { get { throw null; } }
public override string? Namespace { get { throw null; } }
Expand All @@ -107,6 +104,8 @@ internal EnumBuilder() { }
public System.Reflection.Emit.FieldBuilder UnderlyingField { get { throw null; } }
public override System.Type UnderlyingSystemType { get { throw null; } }
[return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
public System.Type? CreateType() { throw null; }
[return: System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
public System.Reflection.TypeInfo? CreateTypeInfo() { throw null; }
public System.Reflection.Emit.FieldBuilder DefineLiteral(string literalName, object? literalValue) { throw null; }
protected override System.Reflection.TypeAttributes GetAttributeFlagsImpl() { throw null; }
Expand Down Expand Up @@ -151,6 +150,7 @@ internal EnumBuilder() { }
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
public override object? InvokeMember(string name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object? target, object?[]? args, System.Reflection.ParameterModifier[]? modifiers, System.Globalization.CultureInfo? culture, string[]? namedParameters) { throw null; }
protected override bool IsArrayImpl() { throw null; }
public override bool IsAssignableFrom([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Reflection.TypeInfo? typeInfo) { throw null; }
protected override bool IsByRefImpl() { throw null; }
protected override bool IsCOMObjectImpl() { throw null; }
public override bool IsDefined(System.Type attributeType, bool inherit) { throw null; }
Expand Down Expand Up @@ -215,7 +215,6 @@ internal GenericTypeParameterBuilder() { }
public override bool IsGenericTypeDefinition { get { throw null; } }
public override bool IsSZArray { get { throw null; } }
public override bool IsTypeDefinition { get { throw null; } }
public override bool IsVariableBoundArray { get { throw null; } }
public override int MetadataToken { get { throw null; } }
public override System.Reflection.Module Module { get { throw null; } }
public override string Name { get { throw null; } }
Expand Down Expand Up @@ -268,6 +267,7 @@ internal GenericTypeParameterBuilder() { }
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
public override object InvokeMember(string name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object? target, object?[]? args, System.Reflection.ParameterModifier[]? modifiers, System.Globalization.CultureInfo? culture, string[]? namedParameters) { throw null; }
protected override bool IsArrayImpl() { throw null; }
public override bool IsAssignableFrom([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Reflection.TypeInfo? typeInfo) { throw null; }
public override bool IsAssignableFrom([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Type? c) { throw null; }
protected override bool IsByRefImpl() { throw null; }
protected override bool IsCOMObjectImpl() { throw null; }
Expand Down Expand Up @@ -296,7 +296,6 @@ internal MethodBuilder() { }
public override bool ContainsGenericParameters { get { throw null; } }
public override System.Type? DeclaringType { get { throw null; } }
public bool InitLocals { get { throw null; } set { } }
public override bool IsConstructedGenericMethod { get { throw null; } }
public override bool IsGenericMethod { get { throw null; } }
public override bool IsGenericMethodDefinition { get { throw null; } }
public override bool IsSecurityCritical { get { throw null; } }
Expand Down Expand Up @@ -371,6 +370,8 @@ public void CreateGlobalFunctions() { }
public override System.Reflection.FieldInfo[] GetFields(System.Reflection.BindingFlags bindingFlags) { throw null; }
public override int GetHashCode() { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")]
protected override System.Reflection.MethodInfo? GetMethodImpl(string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, System.Type[]? types, System.Reflection.ParameterModifier[]? modifiers) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Methods might be removed")]
public override System.Reflection.MethodInfo[] GetMethods(System.Reflection.BindingFlags bindingFlags) { throw null; }
public override void GetPEKind(out System.Reflection.PortableExecutableKinds peKind, out System.Reflection.ImageFileMachine machine) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("Types might be removed")]
Expand Down Expand Up @@ -450,7 +451,6 @@ internal TypeBuilder() { }
public override bool IsSecurityTransparent { get { throw null; } }
public override bool IsSZArray { get { throw null; } }
public override bool IsTypeDefinition { get { throw null; } }
public override bool IsVariableBoundArray { get { throw null; } }
public override int MetadataToken { get { throw null; } }
public override System.Reflection.Module Module { get { throw null; } }
public override string Name { get { throw null; } }
Expand Down Expand Up @@ -541,6 +541,7 @@ public void DefineMethodOverride(System.Reflection.MethodInfo methodInfoBody, Sy
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
public override object? InvokeMember(string name, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object? target, object?[]? args, System.Reflection.ParameterModifier[]? modifiers, System.Globalization.CultureInfo? culture, string[]? namedParameters) { throw null; }
protected override bool IsArrayImpl() { throw null; }
public override bool IsAssignableFrom([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Reflection.TypeInfo? typeInfo) { throw null; }
public override bool IsAssignableFrom([System.Diagnostics.CodeAnalysis.NotNullWhenAttribute(true)] System.Type? c) { throw null; }
protected override bool IsByRefImpl() { throw null; }
protected override bool IsCOMObjectImpl() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ Compat issues with assembly System.Reflection.Emit:
MembersMustExist : Member 'public System.Reflection.Emit.ModuleBuilder System.Reflection.Emit.AssemblyBuilder.DefineDynamicModule(System.String, System.Boolean)' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.Reflection.Module System.Reflection.Emit.ConstructorBuilder.GetModule()' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.String System.Reflection.Emit.ConstructorBuilder.Signature.get()' does not exist in the reference but it does exist in the implementation.
CannotRemoveBaseTypeOrInterface : Type 'System.Reflection.Emit.EnumBuilder' does not inherit from base type 'System.Reflection.TypeInfo' in the reference but it does in the implementation.
MembersMustExist : Member 'public System.Type System.Reflection.Emit.EnumBuilder.CreateType()' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.Boolean System.Reflection.Emit.EnumBuilder.IsAssignableFrom(System.Reflection.TypeInfo)' does not exist in the reference but it does exist in the implementation.
CannotRemoveBaseTypeOrInterface : Type 'System.Reflection.Emit.GenericTypeParameterBuilder' does not inherit from base type 'System.Reflection.TypeInfo' in the reference but it does in the implementation.
MembersMustExist : Member 'public System.Boolean System.Reflection.Emit.GenericTypeParameterBuilder.IsAssignableFrom(System.Reflection.TypeInfo)' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.Reflection.Module System.Reflection.Emit.MethodBuilder.GetModule()' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.String System.Reflection.Emit.MethodBuilder.Signature.get()' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.Diagnostics.SymbolStore.ISymbolDocumentWriter System.Reflection.Emit.ModuleBuilder.DefineDocument(System.String, System.Guid, System.Guid, System.Guid)' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'protected System.ModuleHandle System.Reflection.Emit.ModuleBuilder.GetModuleHandleImpl()' does not exist in the reference but it does exist in the implementation.
MembersMustExist : Member 'public System.Boolean System.Reflection.Emit.ModuleBuilder.IsTransient()' does not exist in the reference but it does exist in the implementation.
CannotRemoveBaseTypeOrInterface : Type 'System.Reflection.Emit.TypeBuilder' does not inherit from base type 'System.Reflection.TypeInfo' in the reference but it does in the implementation.
MembersMustExist : Member 'public System.Boolean System.Reflection.Emit.TypeBuilder.IsAssignableFrom(System.Reflection.TypeInfo)' does not exist in the reference but it does exist in the implementation.
Total Issues: 15
Total Issues: 8
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ namespace System.Reflection.Emit.Tests
public class EnumBuilderMethodTests
{
public static IEnumerable<object[]> DefineLiteral_TestData()
{
foreach (object[] coreData in DefineLiteralTestDataCore())
{
yield return coreData.Concat(new object[] { true }).ToArray();
yield return coreData.Concat(new object[] { false }).ToArray();
}
}

private static IEnumerable<object[]> DefineLiteralTestDataCore()
{
yield return new object[] { typeof(byte), (byte)0 };
yield return new object[] { typeof(byte), (byte)1 };
Expand Down Expand Up @@ -51,7 +60,7 @@ public static IEnumerable<object[]> DefineLiteral_TestData()
[Theory]
[ActiveIssue("https://github.com/dotnet/runtime/issues/2389", TestRuntimes.Mono)]
[MemberData(nameof(DefineLiteral_TestData))]
public void DefineLiteral(Type underlyingType, object literalValue)
public void DefineLiteral(Type underlyingType, object literalValue, bool useCreateTypeInfo)
{
EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType);
FieldBuilder literal = enumBuilder.DefineLiteral("FieldOne", literalValue);
Expand All @@ -61,7 +70,10 @@ public void DefineLiteral(Type underlyingType, object literalValue)
Assert.Equal(FieldAttributes.Public | FieldAttributes.Static | FieldAttributes.Literal, literal.Attributes);
Assert.Equal(enumBuilder.AsType(), literal.FieldType);

Type createdEnum = enumBuilder.CreateTypeInfo().AsType();
Type createdEnum = useCreateTypeInfo ?
enumBuilder.CreateTypeInfo().AsType() :
enumBuilder.CreateType();

FieldInfo createdLiteral = createdEnum.GetField("FieldOne");
Assert.Equal(createdEnum, createdLiteral.FieldType);

Expand Down Expand Up @@ -123,17 +135,27 @@ public void DefineLiteral_InvalidLiteralValue_ThrowsArgumentException(Type under

public static IEnumerable<object[]> DefineLiteral_InvalidLiteralValue_ThrowsTypeLoadExceptionOnCreation_TestData()
{
yield return new object[] { typeof(DateTime), DateTime.Now };
yield return new object[] { typeof(string), "" }; ;
yield return new object[] { typeof(DateTime), DateTime.Now, true };
yield return new object[] { typeof(DateTime), DateTime.Now, false };

yield return new object[] { typeof(string), "", true };
yield return new object[] { typeof(string), "", false };
}

[Theory]
[MemberData(nameof(DefineLiteral_InvalidLiteralValue_ThrowsTypeLoadExceptionOnCreation_TestData))]
public void DefineLiteral_InvalidLiteralValue_ThrowsTypeLoadExceptionOnCreation(Type underlyingType, object literalValue)
public void DefineLiteral_InvalidLiteralValue_ThrowsTypeLoadExceptionOnCreation(Type underlyingType, object literalValue, bool useCreateTypeInfo)
{
EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, underlyingType);
FieldBuilder literal = enumBuilder.DefineLiteral("LiteralName", literalValue);
Assert.Throws<TypeLoadException>(() => enumBuilder.CreateTypeInfo());
if (useCreateTypeInfo)
{
Assert.Throws<TypeLoadException>(() => enumBuilder.CreateTypeInfo());
}
else
{
Assert.Throws<TypeLoadException>(() => enumBuilder.CreateType());
}
}

[Fact]
Expand Down

0 comments on commit d576da2

Please sign in to comment.