Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeBuilder.DefineMethodOverride validation error #101246

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ public override bool ContainsGenericParameters

[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); }
public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); }

public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) => this == c;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved

public override bool IsSubclassOf(Type c)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,32 @@ public void DefineMethodOverride_InterfaceImplementationWithByRefArrayTypes()
implType.CreateType(); // succeeds
}

public interface IConstructable<T>
{
static abstract T Create(int value);
}

[Fact]
public void DefineMethodOverride_ImplementStaticAbstractMethodOfGenericInterface()
{
PersistedAssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder typeBuilder);
Type intfType = typeof(IConstructable<>).MakeGenericType(typeBuilder);
typeBuilder.AddInterfaceImplementation(intfType);
ConstructorBuilder ctor = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, [typeof(int)]);
var il = ctor.GetILGenerator();
il.Emit(OpCodes.Ret);

MethodBuilder impl = typeBuilder.DefineMethod("Create", MethodAttributes.Public | MethodAttributes.Static, typeBuilder, [typeof(int)]);
il = impl.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, ctor);
il.Emit(OpCodes.Ret);

MethodInfo intfMethod = TypeBuilder.GetMethod(intfType, typeof(IConstructable<>).GetMethod("Create")!)!;
typeBuilder.DefineMethodOverride(impl, intfMethod);
typeBuilder.CreateType(); // should succeed
}

buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
[Fact]
public void TypeBuilderImplementsGenericInterfaceWithTypeBuilderGenericConstraint()
{
Expand Down
Loading