Skip to content

Commit 50c0153

Browse files
authored
Added more verbose error message when calling method using reflection and target type does not match passed type (#98129)
* Added more verbose error message when calling method using reflection and target type does not match passed type (fixes #97022) * Fixed resource string as per PR comment * Fixed the test related to a resource string change * Modified test to have a contains check rather than validating the English string. * Modifications to AOT implementation for validating target type when a method is invoked using reflection, inline with other PR changes
1 parent efe8b87 commit 50c0153

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/Execution/MethodBaseInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected static void ValidateThis(object thisObject, RuntimeTypeHandle declarin
5757
throw new TargetException(SR.RFLCT_Targ_StatMethReqTarg);
5858

5959
if (!RuntimeAugments.IsAssignable(thisObject, declaringTypeHandle))
60-
throw new TargetException(SR.RFLCT_Targ_ITargMismatch);
60+
throw new TargetException(SR.Format(SR.RFLCT_Targ_ITargMismatch_WithType, declaringTypeHandle.GetRuntimeTypeInfoForRuntimeTypeHandle().Name, thisObject.GetType().Name));
6161
}
6262
}
6363
}

src/libraries/System.Private.CoreLib/src/Resources/Strings.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3344,6 +3344,9 @@
33443344
<data name="RFLCT_Targ_ITargMismatch" xml:space="preserve">
33453345
<value>Object does not match target type.</value>
33463346
</data>
3347+
<data name="RFLCT_Targ_ITargMismatch_WithType" xml:space="preserve">
3348+
<value>Object type {0} does not match target type {1}.</value>
3349+
</data>
33473350
<data name="RFLCT_Targ_StatFldReqTarg" xml:space="preserve">
33483351
<value>Non-static field requires a target.</value>
33493352
</data>
@@ -4307,4 +4310,4 @@
43074310
<data name="WasmThreads_BlockingWaitNotSupportedOnJSInterop" xml:space="preserve">
43084311
<value>Blocking wait is not supported on the JS interop threads.</value>
43094312
</data>
4310-
</root>
4313+
</root>

src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvokerCommon.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ internal static void ValidateInvokeTarget(object? target, MethodBase method)
9696

9797
if (!method.DeclaringType!.IsInstanceOfType(target))
9898
{
99-
throw new TargetException(SR.RFLCT_Targ_ITargMismatch);
99+
throw new TargetException(SR.Format(SR.RFLCT_Targ_ITargMismatch_WithType, method.DeclaringType.Name, target.GetType().Name));
100100
}
101101
}
102102

src/libraries/System.Runtime/tests/System.Reflection.Tests/DefaultBinderTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ public static void DefaultBinderNamedParametersSkippedAndOutOfOrderTest()
107107
Assert.Equal("MethodMoreParameters", method.Name);
108108
}
109109

110+
[Fact]
111+
public void InvokeWithIncorrectTargetTypeThrowsCorrectException()
112+
{
113+
Type t = typeof(Sample);
114+
object incorrectInstance = Activator.CreateInstance(t);
115+
MethodInvoker invoker = MethodInvoker.Create(typeof(Test).GetMethod(nameof(Test.TestMethod)));
116+
117+
TargetException ex = Assert.Throws<TargetException>(() => invoker.Invoke(obj: incorrectInstance, "NotAnInt"));
118+
Assert.Contains(nameof(Test), ex.Message);
119+
Assert.Contains(nameof(Sample), ex.Message);
120+
}
121+
110122
[Fact]
111123
public static void InvokeWithNamedParameters1st2ndTest()
112124
{

0 commit comments

Comments
 (0)