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

  • SEARCH
  • Home
  • 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 3661666
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:19:50+00:00 2026-05-19T01:19:50+00:00

I have this type that contains two overloads of a generic method. I like

  • 0

I have this type that contains two overloads of a generic method. I like to retrieve one of the overloads (with the Func<T> parameter) using reflection. The problem however is that I can’t find the correct parameter type to supply the Type.GetMethod(string, Type[]) method with.

Here is my class definition:

public class Foo
{
    public void Bar<T>(Func<T> f) { }
    public void Bar<T>(Action<T> a) { }
}

And this is what I’ve come up with, unfortunately without succes:

[TestMethod]
public void Test1()
{
    Type parameterType = typeof(Func<>);

    var method = typeof(Foo).GetMethod("Bar", new Type[] { parameterType });

    Assert.IsNotNull(method); // Fails
}

How can I get the MethodInfo of a generic method of which I know the parameters?

  • 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-05-19T01:19:51+00:00Added an answer on May 19, 2026 at 1:19 am

    Why don’t you use expression trees? This makes it much easier:

    public static MethodInfo GetMethod<T>(
        Expression<Action<T>> methodSelector)
    {
        var body = (MethodCallExpression)methodSelector.Body;
        return body.Method;      
    }
    
    [TestMethod]
    public void Test1()
    {
        var expectedMethod = typeof(Foo)
            .GetMethod("Bar", new Type[] { typeof(Func<>) });
    
        var actualMethod = 
            GetMethod<Foo>(foo => foo.Bar<object>((Func<object>)null)
            .GetGenericMethodDefinition();
    
        Assert.AreEqual(expectedMethod, actualMethod);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a method, Register that subscribes to a number of
I'm having some problems with encapsulation in C#. There are two specific scenarios that
I have a standard jQuery UI dialog. Inside that dialog, I have a custom
If I have few csv files as follows: a,1,2,3 type, max, min, avg b,4,5,6
The whole story; I have some KeyValuePairs that I need to store in a
I have been delving into C# recently, and I wonder if anyone would mind
I am creating a database of structures that can be of different types. Each
Has anyone done trimesh refining in the VCG library ? I would add that
I have rich-text file with a few images and text. In my Cocoa app
Is it possible to use a Model inside the layout page? For instance, we

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.