Skip to content

Improve error reporting for dynamic method invocation via reflection #97022

Closed

Description

@Nefcanto

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.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 16, 2024
  2. added
    help wanted[up-for-grabs] Good issue for external contributors
    good first issueIssue should be easy to implement, good for first-time contributors
    and removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 17, 2024
  3. added this to the 9.0.0 milestone on Jan 17, 2024
  4. steveharter commented on Jan 17, 2024

    @steveharter
    Contributor

    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.

  5. changed the title [-]Improve error reporting for dynamic method invokation via reflection[/-] [+]Improve error reporting for dynamic method invocation via reflection[/+] on Jan 17, 2024
  6. Nefcanto commented on Jan 18, 2024

    @Nefcanto
    Author

    @steveharter, thank you so much.

  7. GustavoAdami commented on Jan 24, 2024

    @GustavoAdami

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

  8. RutulPatel8 commented on Feb 3, 2024

    @RutulPatel8
    Contributor

    @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.

  9. Nefcanto commented on Feb 4, 2024

    @Nefcanto
    Author

    @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.

  10. RutulPatel8 commented on Feb 4, 2024

    @RutulPatel8
    Contributor

    @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.

  11. Nefcanto commented on Feb 4, 2024

    @Nefcanto
    Author

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

  12. RutulPatel8 commented on Feb 4, 2024

    @RutulPatel8
    Contributor

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

    Oh okay no issue Thanks for the help!

  13. wcsanders1 commented on Feb 4, 2024

    @wcsanders1
    Contributor

    @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

  14. AndyBevan commented on Feb 5, 2024

    @AndyBevan
    Contributor

    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?

  15. RutulPatel8 commented on Feb 5, 2024

    @RutulPatel8
    Contributor

    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?

  16. AndyBevan commented on Feb 5, 2024

    @AndyBevan
    Contributor

    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 - #97022 (comment)

  17. AndyBevan commented on Feb 5, 2024

    @AndyBevan
    Contributor

    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.

  18. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 7, 2024
  19. AndyBevan commented on Feb 7, 2024

    @AndyBevan
    Contributor

    I've been looking at this and have submitted a PR - #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.
  20. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 7, 2024
  21. added a commit that references this issue on Feb 7, 2024
    1832ee9
  22. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 7, 2024
  23. added a commit that references this issue on Feb 16, 2024
    50c0153
  24. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Feb 16, 2024
  25. locked and limited conversation to collaborators on Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflectiongood first issueIssue should be easy to implement, good for first-time contributorshelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions