diff --git a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/TypeBuilderImpl.cs b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/TypeBuilderImpl.cs index a698d744212a6..733c79ad86d5f 100644 --- a/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/TypeBuilderImpl.cs +++ b/src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/TypeBuilderImpl.cs @@ -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)) @@ -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) @@ -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 (bodyType == null || !bodyType.Equals(declType)) - { - 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) { diff --git a/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs index e533a23446621..54199f67e1f98 100644 --- a/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs +++ b/src/libraries/System.Reflection.Emit/tests/PersistedAssemblyBuilder/AssemblySaveTypeBuilderAPIsTests.cs @@ -134,38 +134,6 @@ public void DefineMethodOverride_TypeCreated_ThrowsInvalidOperationException() Assert.Throws(() => type.DefineMethodOverride(method, declaration)); } - [Fact] - public void DefineMethodOverride_MethodNotVirtual_ThrowsArgumentException() - { - AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); - MethodBuilder method = type.DefineMethod("M", MethodAttributes.Public, typeof(int), null); - ILGenerator ilGenerator = method.GetILGenerator(); - ilGenerator.Emit(OpCodes.Ldc_I4, 2); - ilGenerator.Emit(OpCodes.Ret); - - type.AddInterfaceImplementation(typeof(DefineMethodOverrideInterface)); - MethodInfo declaration = typeof(DefineMethodOverrideInterface).GetMethod(method.Name); - - Assert.Throws("methodInfoBody", () => type.DefineMethodOverride(method, declaration)); - } - - [Fact] - public void DefineMethodOverride_TypeDoesNotImplementOrInheritMethod_ThrowsArgumentException() - { - AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); - MethodBuilder method = type.DefineMethod("M", MethodAttributes.Public | MethodAttributes.Virtual, typeof(int), null); - method.GetILGenerator().Emit(OpCodes.Ret); - MethodInfo interfaceMethod = typeof(DefineMethodOverrideInterface).GetMethod("M"); - MethodInfo baseTypeMethod = typeof(DefineMethodOverrideClass).GetMethod("M"); - - Assert.Throws("methodInfoBody", () => type.DefineMethodOverride(method, interfaceMethod)); - Assert.Throws("methodInfoBody", () => type.DefineMethodOverride(method, baseTypeMethod)); - - type.AddInterfaceImplementation(typeof(GenericInterface)); - MethodInfo implementingMethod = typeof(GenericInterface).GetMethod(nameof(GenericInterface.Method)); - Assert.Throws("methodInfoBody", () => type.DefineMethodOverride(method, implementingMethod)); - } - [Fact] public void DefineMethodOverride_CalledAgainWithSameDeclaration_ThrowsArgumentException() { @@ -188,24 +156,6 @@ public void DefineMethodOverride_CalledAgainWithSameDeclaration_ThrowsArgumentEx Assert.Throws(() => type.DefineMethodOverride(method2, declaration)); } - [Theory] - [InlineData(typeof(int), new Type[0])] - [InlineData(typeof(int), new Type[] { typeof(int), typeof(int) })] - [InlineData(typeof(int), new Type[] { typeof(string), typeof(string) })] - [InlineData(typeof(int), new Type[] { typeof(int), typeof(string), typeof(bool) })] - [InlineData(typeof(string), new Type[] { typeof(string), typeof(int) })] - public void DefineMethodOverride_BodyAndDeclarationHaveDifferentSignatures_ThrowsArgumentException(Type returnType, Type[] parameterTypes) - { - AssemblySaveTools.PopulateAssemblyBuilderAndTypeBuilder(out TypeBuilder type); - MethodBuilder method = type.DefineMethod("M", MethodAttributes.Public | MethodAttributes.Virtual, returnType, parameterTypes); - method.GetILGenerator().Emit(OpCodes.Ret); - type.AddInterfaceImplementation(typeof(InterfaceWithMethod)); - - MethodInfo declaration = typeof(InterfaceWithMethod).GetMethod(nameof(InterfaceWithMethod.Method)); - - Assert.Throws(() => type.DefineMethodOverride(method, declaration)); - } - public interface GenericInterface { T Method(); @@ -429,22 +379,6 @@ public interface InterfaceDerivedFromOtherInterface : DefineMethodOverrideInterf public string M2(int a); } - public abstract class PartialImplementation : InterfaceDerivedFromOtherInterface - { - public int M() => 1; - public abstract string M2(int a); - } - - public interface IDefaultImplementation - { - void Method() => Console.WriteLine("Hello"); - } - - public interface IStaticAbstract - { - static abstract void Method(); - } - [Fact] public void CreateType_ValidateMethods() {