From 0be894732e08aec5265f7e4e93d2ca8c70e3c945 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 29 Jan 2018 17:37:27 +0100 Subject: Fix reflection to work with default interface methods (#16034) Fixes #15645. Fixes #15644. --- .../GetInterfaceMapConsumer.cs | 101 +++++++++++++++++++++ .../GetInterfaceMapConsumer.csproj | 38 ++++++++ .../GetInterfaceMapProvider.il | 81 +++++++++++++++++ .../GetInterfaceMapProvider.ilproj | 36 ++++++++ .../DefaultInterfaceMethods/InvokeConsumer.cs | 45 +++++++++ .../DefaultInterfaceMethods/InvokeConsumer.csproj | 38 ++++++++ .../DefaultInterfaceMethods/InvokeProvider.il | 74 +++++++++++++++ .../DefaultInterfaceMethods/InvokeProvider.ilproj | 36 ++++++++ 8 files changed, 449 insertions(+) create mode 100644 tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.cs create mode 100644 tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.csproj create mode 100644 tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.il create mode 100644 tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.ilproj create mode 100644 tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.cs create mode 100644 tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.csproj create mode 100644 tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.il create mode 100644 tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.ilproj (limited to 'tests') diff --git a/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.cs b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.cs new file mode 100644 index 0000000000..ecb6373291 --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.cs @@ -0,0 +1,101 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Reflection; + +class Program +{ + static int Main() + { + bool failed = false; + + { + var map = typeof(Fooer).GetInterfaceMap(typeof(IFoo)); + + int foundMatchMask = 0; + + MethodInfo ifooDefaultMethod = typeof(IFoo).GetMethod("DefaultMethod"); + MethodInfo ifooOtherMethod = typeof(IFoo).GetMethod("OtherMethod"); + MethodInfo ibarOtherMethod = typeof(IBar).GetMethod("OtherMethod"); + + for (int i = 0; i < map.InterfaceMethods.Length; i++) + { + MethodInfo declMethod = map.InterfaceMethods[i]; + MethodInfo implMethod = map.TargetMethods[i]; + + Console.Write("{0} ({1}) - {2} ({3}) - ", declMethod, declMethod.DeclaringType, implMethod, implMethod.DeclaringType); + + if (declMethod.Equals(ifooDefaultMethod)) + { + foundMatchMask |= 1; + CheckEqual(ref failed, implMethod, ifooDefaultMethod); + } + else if (declMethod.Equals(ifooOtherMethod)) + { + foundMatchMask |= 2; + CheckEqual(ref failed, implMethod, ibarOtherMethod); + } + else + { + Console.WriteLine("UNEXPECTED"); + failed = true; + } + } + + if (foundMatchMask != 3) + return 10; + } + + { + var map = typeof(Fooer).GetInterfaceMap(typeof(IFoo)); + + int foundMatchMask = 0; + + MethodInfo ifooDefaultMethod = typeof(IFoo).GetMethod("DefaultMethod"); + MethodInfo ifooOtherMethod = typeof(IFoo).GetMethod("OtherMethod"); + MethodInfo ibarOtherMethod = typeof(IBar).GetMethod("OtherMethod"); + + for (int i = 0; i < map.InterfaceMethods.Length; i++) + { + MethodInfo declMethod = map.InterfaceMethods[i]; + MethodInfo implMethod = map.TargetMethods[i]; + + Console.Write("{0} ({1}) - {2} ({3}) - ", declMethod, declMethod.DeclaringType, implMethod, implMethod.DeclaringType); + + if (declMethod.Equals(ifooDefaultMethod)) + { + foundMatchMask |= 1; + CheckEqual(ref failed, implMethod, ifooDefaultMethod); + } + else if (declMethod.Equals(ifooOtherMethod)) + { + foundMatchMask |= 2; + CheckEqual(ref failed, implMethod, ibarOtherMethod); + } + else + { + Console.WriteLine("UNEXPECTED"); + failed = true; + } + } + + if (foundMatchMask != 3) + return 10; + } + + return failed ? -1 : 100; + } + + static void CheckEqual(ref bool failed, MethodInfo method1, MethodInfo method2) + { + if (method1.Equals(method2)) + Console.WriteLine("OK"); + else + { + Console.WriteLine("FAIL"); + failed = true; + } + } +} \ No newline at end of file diff --git a/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.csproj b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.csproj new file mode 100644 index 0000000000..9557d00f3c --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapConsumer.csproj @@ -0,0 +1,38 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + BuildAndRun + 0 + + + + + + + False + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.il b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.il new file mode 100644 index 0000000000..03785988e0 --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.il @@ -0,0 +1,81 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +.assembly extern mscorlib { } + +.assembly GetInterfaceMapProvider { } + +.class interface public abstract auto ansi IFoo +{ + .method public hidebysig newslot virtual instance int32 DefaultMethod(int32) cil managed + { + ldarg.1 + ldc.i4 50 + add + ret + } + + .method public hidebysig newslot virtual abstract instance int32 OtherMethod(int32) cil managed + { + } + + .method public hidebysig instance int32 InstanceMethod(int32) cil managed + { + ldnull + throw + } + + .method public hidebysig static int32 StaticMethod(int32) cil managed + { + ldnull + throw + } +} + +.class interface public abstract auto ansi IBar implements IFoo +{ + .method public hidebysig newslot virtual final instance int32 OtherMethod(int32) cil managed + { + .override IFoo::OtherMethod + ldarg.1 + ret + } +} + +.class interface public abstract auto ansi IFoo`1 +{ + .method public hidebysig newslot virtual instance valuetype [mscorlib]System.RuntimeTypeHandle DefaultMethod() cil managed + { + ldtoken !T + ret + } + + .method public hidebysig newslot virtual abstract instance int32 OtherMethod(int32) cil managed + { + } +} + +.class interface public abstract auto ansi IBar`1 implements class IFoo`1 +{ + .method public hidebysig newslot virtual final instance int32 OtherMethod(int32) cil managed + { + .override class IFoo`1::OtherMethod + ldarg.1 + ret + } + +} + +.class public auto ansi beforefieldinit Fooer + extends [mscorlib]System.Object + implements IBar, class IBar`1 +{ + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + ldarg.0 + call instance void [mscorlib]System.Object::.ctor() + ret + } +} diff --git a/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.ilproj b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.ilproj new file mode 100644 index 0000000000..513f38e550 --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/GetInterfaceMapProvider.ilproj @@ -0,0 +1,36 @@ + + + + + Debug + AnyCPU + $(MSBuildProjectName) + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Library + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + + + + + + + + False + + + + + + + + + + + + + + + diff --git a/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.cs b/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.cs new file mode 100644 index 0000000000..742fd244e5 --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.cs @@ -0,0 +1,45 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; + +class Program +{ + static int Main() + { + if ((int)typeof(IFoo).GetMethod("StaticMethod").Invoke(null, new object[] { 1 }) != 31) + return 1; + + if ((int)typeof(IFoo).GetMethod("DefaultMethod").Invoke(new Fooer(), new object[] { 1 }) != 51) + return 2; + + if ((int)typeof(IFoo).GetMethod("InstanceMethod").Invoke(new Fooer(), new object[] { 1 }) != 21) + return 3; + + if (!((RuntimeTypeHandle)typeof(IFoo).GetMethod("StaticMethod").Invoke(null, new object[] { })).Equals(typeof(Fooer[,]).TypeHandle)) + return 11; + + if (!((RuntimeTypeHandle)typeof(IFoo).GetMethod("DefaultMethod").Invoke(new Fooer(), new object[] { })).Equals(typeof(Fooer).TypeHandle)) + return 12; + + // Likely failing due to https://github.com/dotnet/coreclr/issues/15241 + //if (!((RuntimeTypeHandle)typeof(IFoo).GetMethod("InstanceMethod").Invoke(new Fooer(), new object[] { })).Equals(typeof(Fooer[]).TypeHandle)) + // return 13; + + if ((int)typeof(IFoo).GetMethod("DefaultMethod").Invoke(new ValueFooer(), new object[] { 1 }) != 51) + return 22; + + if ((int)typeof(IFoo).GetMethod("InstanceMethod").Invoke(new ValueFooer(), new object[] { 1 }) != 21) + return 23; + + if (!((RuntimeTypeHandle)typeof(IFoo).GetMethod("DefaultMethod").Invoke(new ValueFooer(), new object[] { })).Equals(typeof(Fooer).TypeHandle)) + return 32; + + // Likely failing due to https://github.com/dotnet/coreclr/issues/15241 + //if (!((RuntimeTypeHandle)typeof(IFoo).GetMethod("InstanceMethod").Invoke(new ValueFooer(), new object[] { })).Equals(typeof(Fooer[]).TypeHandle)) + // return 33; + + return 100; + } +} diff --git a/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.csproj b/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.csproj new file mode 100644 index 0000000000..d42efa5cdc --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/InvokeConsumer.csproj @@ -0,0 +1,38 @@ + + + + + Debug + AnyCPU + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Exe + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + BuildAndRun + 0 + + + + + + + False + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.il b/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.il new file mode 100644 index 0000000000..d1f6e73af8 --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.il @@ -0,0 +1,74 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +.assembly extern mscorlib { } + +.assembly InvokeProvider { } + +.class interface public abstract auto ansi IFoo +{ + .method public hidebysig newslot virtual instance int32 DefaultMethod(int32) cil managed + { + ldarg.1 + ldc.i4 50 + add + ret + } + + .method public hidebysig instance int32 InstanceMethod(int32) cil managed + { + ldarg.1 + ldc.i4 20 + add + ret + } + + .method public hidebysig static int32 StaticMethod(int32) cil managed + { + ldarg.0 + ldc.i4 30 + add + ret + } +} + +.class interface public abstract auto ansi IFoo`1 +{ + .method public hidebysig newslot virtual instance valuetype [mscorlib]System.RuntimeTypeHandle DefaultMethod() cil managed + { + ldtoken !T + ret + } + + .method public hidebysig instance valuetype [mscorlib]System.RuntimeTypeHandle InstanceMethod() cil managed + { + ldtoken !T[] + ret + } + + .method public hidebysig static valuetype [mscorlib]System.RuntimeTypeHandle StaticMethod() cil managed + { + ldtoken !T[,] + ret + } +} + +.class public auto ansi beforefieldinit Fooer + extends [mscorlib]System.Object + implements IFoo, class IFoo`1 +{ + .method public hidebysig specialname rtspecialname + instance void .ctor() cil managed + { + ldarg.0 + call instance void [mscorlib]System.Object::.ctor() + ret + } +} + +.class public sequential ansi sealed beforefieldinit ValueFooer + extends [mscorlib]System.ValueType + implements IFoo, class IFoo`1 +{ +} diff --git a/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.ilproj b/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.ilproj new file mode 100644 index 0000000000..21573d81ac --- /dev/null +++ b/tests/src/reflection/DefaultInterfaceMethods/InvokeProvider.ilproj @@ -0,0 +1,36 @@ + + + + + Debug + AnyCPU + $(MSBuildProjectName) + 2.0 + {95DFC527-4DC1-495E-97D7-E94EE1F7140D} + Library + {786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + ..\..\ + + + + + + + + + False + + + + + + + + + + + + + + + -- cgit v1.2.3