Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:57:33+00:00 2026-06-12T21:57:33+00:00

I have a number of MethodBase instances referencing different open generic methods ( expected

  • 0

I have a number of MethodBase instances referencing different open generic methods (expected), e.g. representing the following methods:

T Foo<T>(T nevermind, T other);
T Foo<T>(string nevermind, T other);

And I have a single MethodBase instance referencing closed method that was actually called (actual), e.g.:

int Foo<int>(string nevermind, int other);

How can I programatically check if actual closed method could match any of given expected open methods, especially when considering all the generics pitfalls and complications?

Specifically, I would like to identify that the correct item from expected list for given actual closed method is T Foo<T>(string nevermind, T other); and not the second one.

Moreover, for MethodBase corresponding to double Foo<double>(double something, string other) I’d like to have no results matched.

Is iterating through candidate methods and checking if each parameter from expected is assignable from corresponding actual parameter a good way? If so, is it the simplest way? Do I need to consider any special cases to not match methods that will not be chosen according to method overloads resolution rules in .NET?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:57:34+00:00Added an answer on June 12, 2026 at 9:57 pm

    Tl;dr. The problem is not possible to solve using reflection, at least as I understand it, and without more specificity..

    Method resolution rules are extremely complicated, especially for generic methods. There are many pitfalls you will fall into. You will need to know not only the method, the type parameter, but also a lot of information about the target, along with its own type parameters. In some cases, where the method was called from.

    • Method has implementation in a base class but is hidden by the child.
    • Method is from an interface, and was implemented explicitly, and may have another method with the same name on the implementer.
    • A methods such as Foo<T>(T a, string other), Foo<T>(string a, T other), Foo<T>(string a, string other) and some other variations cannot be disambiguated for T = string unless you know where the call is coming from (these are legal methods, and the one that gets called depends on several things).
    • Generic constraints can be placed on methods.
    • Polymorphism on the argument types, including generic variance for interfaces and delegates.
    • Optional parameters.
    • This goes on and on.

    Basically, it can never work. Not using reflection. Not the way you’re proposing. Even if you have restrictions about what calls can be made, you’d have to decide which things to check and which not, and you will always miss a few. These aren’t the only pitfalls by the way, just a random sampling.

    However, you do have some options.

    The first, and best option in my opinion, is going a step back and thinking about the original problem. Post that if you can. It might have a different answer, and people will be able to advise you better. Hopefully it’s less complicated to understand.

    If you limited the scope of the matter greatly, such as no generic constraints, no interfaces, and so forth, this might be possible. It would be error prone, because there are lots of gotcha’s.

    You can try resolving it at runtime using dynamic binding, but the way dynamic binding resolves methods may be different from the way it normally happens. I don’t know much about this, though.

    You can hook the runtime and also investigate method calls as they are resolved. There are libraries for this. This will even allow you to understand how late binding is resolved.

    Finally, you can look into the IL, possibly with the aid of various tools and libraries such as Mono.Cecil. In the built library, method resolution has already been performed, so you will see exactly which methods are called from which locations. This doesn’t sound feasible however.

    Oh, there is Roslyn, and other compilers with interfaces. They already have the resolution logic implemented, so they may make the task easier. IF they are open source, you can try to understand how method resolution is performed there. I’m kind of out of my depth here, though. And I suspect it’s not feasible.

    I don’t like posting links to specific libraries because I’d rather you just research them. Also because there are many options.

    To summarize, at least in my opinion, and as I understand the problem, without great restrictions on the methods and more information, it is impossible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I have a number of classes and they are quite close to each other
I have number of sockets connected to different systems with different ip-address with different
I have number of tables in my database. There will be number of methods
I have number of line breaks in a string. But I only want it
I have a number input that should trigger jQuery on every change. To do
We have a number of data objects that realize INotifyPropertyChanged to allow for WPF
I have a number of classes (more than 40), each one has a number
I have a number of aliases defined in my .bashrc . E.g: alias ls='ls
I have a number of draggable images that I want to clone and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.