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 7430827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:12:07+00:00 2026-05-29T09:12:07+00:00

I am trying to write a method that returns a methodinfo class from a

  • 0

I am trying to write a method that returns a methodinfo class from a given input, with a view to creating the generic version of that method…i.e.

var m = myClass.GetType().GetMethod("SomeMethod").MakeGenericMethod(...blahblah..);

This works and is all good, except that I have the string literal of my method name, so if in the course of re-factoring I happen to rename one of the methods I am using I don’t find out until run time.

What I would like to do is create a helper method that I can pass a lamba to that specifies the methodgroup, that way I would get compile time checking of the method name, not to mention intellisense etc…ie.

MethodInfo mi = myClass.GetMethodInfo( o => o.SomeMethod );
m = mi.MakeGenericMethod(..blah...);

But I haven’t been able to figure out the method signature of the helper…

public MethodInfo GetMethodInfo(Func<MyClass,XXXX> lambda){ //What is my XXXX ? }
  • 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-29T09:12:08+00:00Added an answer on May 29, 2026 at 9:12 am

    You can do it like this, but I am stumped as to why you need the to specify the delegate type. Without that it doesn’t work, but since the below works for BarMethod, it doesn’t seem to matter what the delegate type is:

    public static void Main(String[] args)
    {
        Foo f = new Foo();
        Console.WriteLine(ForMethod(() => f.FooMethod()).Name);
        Console.WriteLine(ForMethod(() => f.Foo2Method<String>()).Name);
        Console.WriteLine(ForMethod(() => f.BarMethod("foo")).Name);
        Console.ReadKey();
    }
    
    public static MethodInfo ForMethod(Expression<Action> e)
    {
        var mi = ((MethodCallExpression) e.Body).Method;
        if (mi.IsGenericMethod)
            mi = mi.GetGenericMethodDefinition();
        return mi;
    }
    
    class Foo
    {
        public void FooMethod() { }
        public void Foo2Method<T>() { }
        public void BarMethod(String foo) { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a generic Parse method that converts and returns a
I'm trying to write some C# code that calls a method from an unmanaged
I am trying to write public instance method createParther() that returns an instance of
I'm trying to write a generic method that can be used to deserialize xml
I'm trying to write a method named isLowerThanFreezing that returns true if the Temperature
I'm trying to write a controller method that returns an ActionResult. In this method,
I'm trying to write a method that does the following: public class GridCounting {
I'm trying write a test for a method that returns a java.sql.Connection to connect
I'm trying to write a convenience method that returns a UIButton and takes a
Good evening all, I'm trying to write a method that creates and returns a

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.