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

Conversation

buyaa-n
Copy link
Member

@buyaa-n buyaa-n commented Apr 18, 2024

On TypeBuilder.DefineMethodOverride(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration) TypeBuilderImpl validates if the methodInfoBody could override the methodInfoDeclaration

Type baseType = methodInfoDeclaration.DeclaringType!;
ValidateBaseType(baseType, methodInfoDeclaration.Name);
ValidateImplementedMethod(methodInfoBody, methodInfoDeclaration);

ValidateBaseType checks if the TypeBuilderImpl implements the declaring type of the methodInfoDeclaration

private void ValidateBaseType(Type baseType, string methodName)
{
if (baseType.IsInterface)
{
if (!IsInterfaceImplemented(baseType))
{
throw ArgumentExceptionInvalidMethodOverride(methodName);

In case the base type is constructed generic type that involves TypeBuilder (i.e. instance of TypeBuilderInstantiation) it throws as IsAssignableFrom is not implemented.

Static TypeBuilder.GetMethod(...) only allow getting declared methods, so just comparing the instance with the type should be enough for TypeBuilderInstantiation

Fixes #100537

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@jkotas
Copy link
Member

jkotas commented May 14, 2024

If you would like to keep going on the IsAssignableFrom implementation, you may want to steal some non-ref emit tests cases for it. The assignability (for generics in particular) is a complicated algorithm. I am not sure whether it is worth it to try to implement it just for the validation.

@buyaa-n
Copy link
Member Author

buyaa-n commented May 16, 2024

For the record: we end up removing DefineMethodOverride validations that doesn't exist in RuntimeTypeBuilder, as those validations was from runtime type loader, not from Reflection.Emit, thank you @jkotas!

@buyaa-n buyaa-n requested a review from jkotas May 16, 2024 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible to implement a self-referential static abstract interface method in Reflection.Emit?
4 participants