runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Improve error reporting for dynamic method invocation via reflection

Open Nefcanto opened this issue 2 years ago • 15 comments

When we invoke a method via reflection, in case something goes wrong with type matching we get this error:

Object does not match target type.

This is a very poor message for debugging. Please give us more information. Something like:

You wanted to invoke SomeMethod and you passed X of type XType with value XValue and Y with type YType. These types do not match with what SomeMethod expects as its parameters A of type AType and B of type BType.

Nefcanto avatar Jan 16 '24 10:01 Nefcanto

Tagging subscribers to this area: @dotnet/area-system-reflection See info in area-owners.md if you want to be subscribed.

Issue Details

When we invoke a method via reflection, in case something goes wrong with type matching we get this error:

Object does not match target type.

This is a very poor message for debugging. Please give us more information. Something like:

You wanted to invoke SomeMethod and you passed X of type XType with value XValue and Y with type YType. These types do not match with what SomeMethod expects as its parameters A of type AType and B of type BType.

Author: Nefcanto
Assignees: -
Labels:

area-System.Reflection, untriaged

Milestone: -

ghost avatar Jan 16 '24 10:01 ghost

The request sounds reasonable; the proposed wording seems a little verbose. Maybe shorten to something like:

SomeMethod exists on type X but an object of type Y was passed.

It looks like the existing resource string "RFLCT_Targ_ITargMismatch" is used by the managed code in System.Reflection.MethodInvokerCommon and by runtime code in interoputil.cpp. To make this easier, we would likely create a new resource string for the managed code and leave the runtime\native code as-is.

steveharter avatar Jan 17 '24 21:01 steveharter

@steveharter, thank you so much.

Nefcanto avatar Jan 18 '24 06:01 Nefcanto

@steveharter, I would like to take on this issue.

GustavoAdami avatar Jan 24 '24 20:01 GustavoAdami

@Nefcanto or @steveharter Can you please suggest how Can I debug runtime > Reflection code so I can fix this issue. I tried lot but I didn't found the way.

RutulPatel8 avatar Feb 03 '24 15:02 RutulPatel8

@RutulPatel8, create a simple class with a single method that takes one parameter which is a string.

Now reflect it and call that method, but pass it a list of integers. That's how.

Nefcanto avatar Feb 04 '24 05:02 Nefcanto

@RutulPatel8, create a simple class with a single method that takes one parameter which is a string.

Now reflect it and call that method, but pass it a list of integers. That's how.

@Nefcanto Yes I know. how to reproduce this issue. I'm talking about dotnet > Runtime > libraries > System.Reflection debugging. Currently I clone runtime repository but I don't know which solution file needs to execute(debug) in runtime project. In libraries, I found System.Reflection csProj but I think it is not right way to technique.

RutulPatel8 avatar Feb 04 '24 06:02 RutulPatel8

@RutulPatel8, I apologize for my misunderstanding. I don't know about that part.

Nefcanto avatar Feb 04 '24 06:02 Nefcanto

@RutulPatel8, I apologize for my misunderstanding. I don't know about that part.

Oh okay no issue Thanks for the help!

RutulPatel8 avatar Feb 04 '24 07:02 RutulPatel8

@RutulPatel8, create a simple class with a single method that takes one parameter which is a string. Now reflect it and call that method, but pass it a list of integers. That's how.

@Nefcanto Yes I know. how to reproduce this issue. I'm talking about dotnet > Runtime > libraries > System.Reflection debugging. Currently I clone runtime repository but I don't know which solution file needs to execute(debug) in runtime project. In libraries, I found System.Reflection csProj but I think it is not right way to technique.

I believe the solution file you're looking for is this one: https://github.com/dotnet/runtime/blob/main/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.sln

wcsanders1 avatar Feb 04 '24 15:02 wcsanders1

I was taking a look at this and the error message gets thrown within class MethodInvokerCommon method ValidateInvokeTarget I was unable to find any pre-existing tests for this class - am I missing them? I grepped the repo for the class name and did not find it used in anything test related.

I do find tests within \src\libraries\System.Private.CoreLib\tests\IntrinsicsInSystemPrivatecoreLibAnalyzer.Tests so would assume that somewhere under \tests\ would need a new test class adding for MethodInvokerCommon.

I tried to run the existing tests following the instructions from here - https://github.com/dotnet/runtime/blob/main/docs/workflow/testing/libraries/testing.md#running-tests-for-a-single-library

But this doesn't appear to work for src\libraries\System.Private.CoreLib\tests\ because there is no solution like there is for something like System.Collections.Immutable which has a solution within the libraries folder.

@steveharter - are you able to give some pointers on what I might be missing in terms of how we can run the tests within visual studio for System.Private.CoreLib?

AndyBevan avatar Feb 05 '24 05:02 AndyBevan

I was taking a look at this and the error message gets thrown within class MethodInvokerCommon method ValidateInvokeTarget I was unable to find any pre-existing tests for this class - am I missing them? I grepped the repo for the class name and did not find it used in anything test related.

I do find tests within \src\libraries\System.Private.CoreLib\tests\IntrinsicsInSystemPrivatecoreLibAnalyzer.Tests so would assume that somewhere under \tests\ would need a new test class adding for MethodInvokerCommon.

I tried to run the existing tests following the instructions from here - https://github.com/dotnet/runtime/blob/main/docs/workflow/testing/libraries/testing.md#running-tests-for-a-single-library

But this doesn't appear to work for src\libraries\System.Private.CoreLib\tests\ because there is no solution like there is for something like System.Collections.Immutable which has a solution within the libraries folder.

@steveharter - are you able to give some pointers on what I might be missing in terms of how we can run the tests within visual studio for System.Private.CoreLib?

Which solution did you execute for debugging?

RutulPatel8 avatar Feb 05 '24 09:02 RutulPatel8

Which solution did you execute for debugging?

At this point I have not found any tests to execute. I'm hoping that @steveharter will give us some pointers on how to approach this because there is a solution file that contains libraries\System.Private.CoreLib but it's in coreclr (/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.sln) and does not contain the tests (Maybe it should?)

Rest of my questions are in this post - https://github.com/dotnet/runtime/issues/97022#issuecomment-1926268048

AndyBevan avatar Feb 05 '24 18:02 AndyBevan

I think I figured it out - It looks like you need to use the System.Runtime solution which includes the public methods that allow access to System.Private.CoreLib.

AndyBevan avatar Feb 05 '24 19:02 AndyBevan

I've been looking at this and have submitted a PR - https://github.com/dotnet/runtime/pull/98129

@steveharter - I do have a few questions that I'm hoping you can help me with.

  1. I added a test to DefaultBinderTests to get things working because there was a good class in the abstract to re-use but this really isn't testing DefaultBinding behavior. The other location I considered was MethodInvokerTests, those tests all relate to static method testing but maybe that is a better place. Thoughts?
  2. RFLCT_Targ_ITargMismatch is the current resource string, I added a new one in \src\libraries\System.Private.CoreLib\src\Resources\Strings.resx - is that the correct location?
  3. String Name RFLCT_Targ_ITargMismatch_WithType - Does this resource string name correct? I wasn't sure how to differentiate this new string from the original one.
  4. I used message Object of type {0} does not match target type {1}. This is different from the proposed message of SomeMethod exists on type X but an object of type Y was passed.. Because I'm not sure we know easily that the target type has SomeMethod. The problem here relates to the incorrect type being passed in so I think the error message I used adds the additional clarity we are looking for.
  5. I also have a general question about building the changes. Some of the changes are to MethodInvokerCommon which is not included in the solution System.Runtime solution where the managed code reflection tests are, it's class is located in the library folder but the class is not included in the csProj (\System.Private.CoreLib\src\System\Reflection\MethodInvokerCommon.cs). To modify MethodInvokerCommon I needed to load System.Private.CoreLib.sln, what's the easiest way to rebuild this library so that it gets pulled into for use within projects contained in the System.Runtime.sln solution. I ended up rerunning build.cmd clr+libs -rc Release but that's a big hammer for a small nut (hopefully). Is there a more efficient way to work to rebuild Private.CoreLib and then have the managed code libraries pull the new version? I hope this question makes sense. I tried just building System.Private.CoreLib.sln and that wasn't sufficient to for the projects contained in System.Runtime solution to have the new assembly.

AndyBevan avatar Feb 07 '24 21:02 AndyBevan