Skip to content

Commit

Permalink
Added more verbose error message when calling method using reflection…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
AndyBevan authored Feb 16, 2024
1 parent efe8b87 commit 50c0153
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,9 @@
<data name="RFLCT_Targ_ITargMismatch" xml:space="preserve">
<value>Object does not match target type.</value>
</data>
<data name="RFLCT_Targ_ITargMismatch_WithType" xml:space="preserve">
<value>Object type {0} does not match target type {1}.</value>
</data>
<data name="RFLCT_Targ_StatFldReqTarg" xml:space="preserve">
<value>Non-static field requires a target.</value>
</data>
Expand Down Expand Up @@ -4307,4 +4310,4 @@
<data name="WasmThreads_BlockingWaitNotSupportedOnJSInterop" xml:space="preserve">
<value>Blocking wait is not supported on the JS interop threads.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TargetException>(() => invoker.Invoke(obj: incorrectInstance, "NotAnInt"));
Assert.Contains(nameof(Test), ex.Message);
Assert.Contains(nameof(Sample), ex.Message);
}

[Fact]
public static void InvokeWithNamedParameters1st2ndTest()
{
Expand Down

0 comments on commit 50c0153

Please sign in to comment.