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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:38:56+00:00 2026-05-15T03:38:56+00:00

I hope i’m missing something obvious, but I’m having some troubles defining a method

  • 0

I hope i’m missing something obvious, but I’m having some troubles defining a method that takes a parameter of a method to fetch the method information for the passed method. I do NOT want actually execute the method.

I want to be able to do:

  busObject.SetResolverMethod<ISomeInterface>(x=>x.GetNameById);

Where GetNameById is a method defined on the interface ISomeInterface. In this case, an example of the method being passed in’s signature would be:

 MyVarA GetNameById(int id){ .... }

In the above example, the SetResolverMethod‘s body should be able to return / store the string “GetNameById”.

There is no standard signature the method being passed in will conform to (except that it will always return an object of some kind).

Currently I’m setting the method as a string (i.e. “GetNameById”), but I want it to be compile time checked, hence this question.

  • 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-15T03:38:58+00:00Added an answer on May 15, 2026 at 3:38 am

    It’s not particularly pretty/fluent but if you really want to avoid having to pass dummy parameter values then you can use an expression that returns a delegate.

    SetResolverMethod<ISomeInterface>(x => new Func<int, MyVarA>(x.GetNameById));
    

    The SetResolverMethod implementation would look something like this:

    public void SetResolverMethod<T>(Expression<Func<T, Delegate>> expr)
    {
        var unary = (UnaryExpression) expr.Body;
        var methodCall = (MethodCallExpression) unary.Operand;
        var constant = (ConstantExpression) methodCall.Arguments[2];
        var method = (MethodInfo) constant.Value;
        Console.WriteLine(method.Name);
    }
    

    Edit: If you’re willing to create as set of overloads for each Func<> delegate, you can improve the fluency by including the method parameter types in the generic parameter types of your method.

    p.SetResolverMethod<ISomeInterface, int, MyVarA>(x => x.GetNameById);
    

    As you can see, the caller no longer needs to specify a delegate type, thus saving around 8 characters.

    I’ve implemented three overloads for 0, 1 and 2 parameters:

    public void SetResolverMethod<T, TResult>(Expression<Func<T, Func<TResult>>> expr)
    {
        SetResolverMethod((LambdaExpression) expr);
    }
    
    public void SetResolverMethod<T, T1, TResult>(Expression<Func<T, Func<T1, TResult>>> expr)
    {
        SetResolverMethod((LambdaExpression) expr);
    }
    
    public void SetResolverMethod<T, T1, T2, TResult>(Expression<Func<T, Func<T1, T2, TResult>>> expr)
    {
        SetResolverMethod((LambdaExpression) expr);
    }
    
    private void SetResolverMethod(LambdaExpression expr)
    {
        var unary = (UnaryExpression) expr.Body;
        var methodCall = (MethodCallExpression) unary.Operand;
        var constant = (ConstantExpression) methodCall.Arguments[2];
        var method = (MethodInfo) constant.Value;
        Console.WriteLine(method.Name);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hope this is something simple and I am just missing it, but consider this
Hope that someone can share their experience with some of the latest emerging backbone.js
hope everybody is cool.I've been trying to do something with spring-hibernate but it's still
Hope there is some out there that can help!, While debugging a WCF service
hope someone can answer this. Here is some sample code. namespace std { #ifdef
hope all is swell! I was hoping I had cracked this problem but it
Hope you're having a good Friday and stuff... okay, so here's my question: All
Hope you can advise I would like to add some simple fade in out
Hope someone can shed some light on this... I, and many other developers in
Hope someone can help, I'm not a programmer, but have been interested in exploring

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.