diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/MethodBaseInvoker.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/MethodBaseInvoker.cs index 78fc4d99d2792..a14c0007dc8f1 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/MethodBaseInvoker.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/MethodBaseInvoker.cs @@ -57,7 +57,7 @@ protected static void ValidateThis(object thisObject, RuntimeTypeHandle declarin throw new TargetException(SR.RFLCT_Targ_StatMethReqTarg); if (!RuntimeAugments.IsAssignable(thisObject, declaringTypeHandle)) - throw new TargetException(SR.RFLCT_Targ_ITargMismatch); + throw new TargetException(SR.Format(SR.RFLCT_Targ_ITargMismatch_WithType, declaringTypeHandle.GetRuntimeTypeInfoForRuntimeTypeHandle().Name, thisObject.GetType().Name)); } } } diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index 09348ec9d3ce4..b6d8fd00aa404 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3344,6 +3344,9 @@ Object does not match target type. + + Object type {0} does not match target type {1}. + Non-static field requires a target. @@ -4307,4 +4310,4 @@ Blocking wait is not supported on the JS interop threads. - \ No newline at end of file + diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvokerCommon.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvokerCommon.cs index 0096a9f397c39..4eda76d9364b7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvokerCommon.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvokerCommon.cs @@ -96,7 +96,7 @@ internal static void ValidateInvokeTarget(object? target, MethodBase method) if (!method.DeclaringType!.IsInstanceOfType(target)) { - throw new TargetException(SR.RFLCT_Targ_ITargMismatch); + throw new TargetException(SR.Format(SR.RFLCT_Targ_ITargMismatch_WithType, method.DeclaringType.Name, target.GetType().Name)); } } diff --git a/src/libraries/System.Runtime/tests/System.Reflection.Tests/DefaultBinderTests.cs b/src/libraries/System.Runtime/tests/System.Reflection.Tests/DefaultBinderTests.cs index 874e1f3d5864c..d1c1f63d830e0 100644 --- a/src/libraries/System.Runtime/tests/System.Reflection.Tests/DefaultBinderTests.cs +++ b/src/libraries/System.Runtime/tests/System.Reflection.Tests/DefaultBinderTests.cs @@ -107,6 +107,18 @@ public static void DefaultBinderNamedParametersSkippedAndOutOfOrderTest() Assert.Equal("MethodMoreParameters", method.Name); } + [Fact] + public void InvokeWithIncorrectTargetTypeThrowsCorrectException() + { + Type t = typeof(Sample); + object incorrectInstance = Activator.CreateInstance(t); + MethodInvoker invoker = MethodInvoker.Create(typeof(Test).GetMethod(nameof(Test.TestMethod))); + + TargetException ex = Assert.Throws(() => invoker.Invoke(obj: incorrectInstance, "NotAnInt")); + Assert.Contains(nameof(Test), ex.Message); + Assert.Contains(nameof(Sample), ex.Message); + } + [Fact] public static void InvokeWithNamedParameters1st2ndTest() {