Skip to content

Commit

Permalink
Remove DefineMethodOverride validations that not exist in RuntimeType…
Browse files Browse the repository at this point in the history
…Builder
  • Loading branch information
buyaa-n committed May 16, 2024
1 parent 016f4be commit 0910cfe
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 358 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,34 +183,8 @@ public override Type? BaseType
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
public override Type GetInterface(string name, bool ignoreCase) { throw new NotSupportedException(); }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2080:DynamicallyAccessedMemberTypes",
Justification = "Interfaces used in Reflection.Emit either dynamic or accessible")]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
public override Type[] GetInterfaces()
{
Type[] ifaces = _genericType.GetInterfaces();

if (ifaces.Length == 0)
{
return ifaces;
}

Type[] ifacesCopy = new Type[ifaces.Length];

for(int i = 0; i < ifaces.Length; i++)
{
if (ifaces[i] is TypeBuilderInstantiation typeBldrIface)
{
typeBldrIface.Substitute(GetGenericArguments());
}
else
{
ifacesCopy[i] = ifaces[i];
}
}

return ifacesCopy;
}
public override Type[] GetInterfaces() { throw new NotSupportedException(); }

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override EventInfo GetEvent(string name, BindingFlags bindingAttr) { throw new NotSupportedException(); }
Expand Down Expand Up @@ -279,72 +253,7 @@ 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)); }

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:DynamicallyAccessedMemberTypes",
Justification = "Interfaces used in Reflection.Emit either dynamic or accessible")]
public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
{
if (c == null)
{
return false;
}

if (this == c || IsAssignableFrom(c.BaseType))
{
return true;
}

if (!c.IsConstructedGenericType)
{
return false;
}

Type typeDefinition = c.GetGenericTypeDefinition();

if (typeDefinition.IsAssignableFrom(_genericType) &&
CheckArguments(c.GenericTypeArguments))
{
return true;
}

foreach (Type iface in typeDefinition.GetInterfaces())
{
if (iface.IsConstructedGenericType &&
iface.GetGenericTypeDefinition().IsAssignableFrom(_genericType) &&
CheckArguments(c.GenericTypeArguments))
{
return true;
}
}

if (c.BaseType != null &&
c.BaseType.IsConstructedGenericType &&
c.BaseType.GetGenericTypeDefinition().IsAssignableFrom(_genericType) &&
CheckArguments(c.GenericTypeArguments))
{
return true;
}

return false;
}

private bool CheckArguments(Type[] genericTypeArguments)
{
if (_typeArguments.Length != genericTypeArguments.Length)
{
return false;
}

for (int i = 0; i < _typeArguments.Length; i++)
{
if (_typeArguments[i] != genericTypeArguments[i])
{
return false;
}
}

return true;
}
public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); }

public override bool IsSubclassOf(Type c)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ protected override void DefineMethodOverrideCore(MethodInfo methodInfoBody, Meth
throw new ArgumentException(SR.ArgumentException_BadMethodImplBody);
}
Type baseType = methodInfoDeclaration.DeclaringType!;
ValidateBaseType(baseType, methodInfoDeclaration.Name);
ValidateImplementedMethod(methodInfoBody, methodInfoDeclaration);
_methodOverrides ??= new();

if (_methodOverrides.TryGetValue(baseType, out List<(MethodInfo ifaceMethod, MethodInfo targetMethod)>? im))
Expand All @@ -342,24 +340,6 @@ protected override void DefineMethodOverrideCore(MethodInfo methodInfoBody, Meth
}
}

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

return;
}

if (!baseType.IsAssignableFrom(_typeParent))
{
throw ArgumentExceptionInvalidMethodOverride(methodName);
}
}

private bool IsInterfaceImplemented(Type interfaceType)
{
if (_interfaces != null)
Expand Down Expand Up @@ -390,47 +370,6 @@ private bool IsInterfaceImplemented(Type interfaceType)
private ArgumentException ArgumentExceptionInvalidMethodOverride(string methodName) =>
new ArgumentException(SR.Format(SR.Argument_InvalidMethodOverride, FullName, methodName), "methodInfoBody");

private void ValidateImplementedMethod(MethodInfo methodInfoBody, MethodInfo methodInfoDeclaration)
{
if ((methodInfoBody.IsVirtual || methodInfoBody.IsStatic) &&
(methodInfoDeclaration.IsAbstract || methodInfoDeclaration.IsVirtual) &&
methodInfoDeclaration.ReturnType.IsAssignableFrom(methodInfoBody.ReturnType))
{
ParameterInfo[] bodyParameters = methodInfoBody.GetParameters();
ParameterInfo[] declarationParameters = methodInfoDeclaration.GetParameters();
if (bodyParameters.Length == declarationParameters.Length)
{
for (int i = 0; i < bodyParameters.Length; i++)
{
Type? bodyType = bodyParameters[i].ParameterType;
Type? declType = declarationParameters[i].ParameterType;

if (bodyType.IsArray != declType.IsArray ||
bodyType.IsByRef != declType.IsByRef ||
bodyType.IsPointer != declType.IsPointer)
{
throw ArgumentExceptionInvalidMethodOverride(methodInfoDeclaration.Name);
}

if (bodyType.HasElementType || declType.HasElementType)
{
bodyType = bodyType.GetElementType();
declType = declType.GetElementType();
}

if (declType == null || !declType.IsAssignableFrom(bodyType))
{
throw ArgumentExceptionInvalidMethodOverride(methodInfoDeclaration.Name);
}
}

return;
}
}

throw ArgumentExceptionInvalidMethodOverride(methodInfoDeclaration.Name);
}

protected override TypeBuilder DefineNestedTypeCore(string name, TypeAttributes attr,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, PackingSize packSize, int typeSize)
{
Expand Down
Loading

0 comments on commit 0910cfe

Please sign in to comment.