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

The Archive Base Latest Questions

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

I realize that, generally speaking, there are performance implications of using reflection. (I myself

  • 0

I realize that, generally speaking, there are performance implications of using reflection. (I myself am not a fan of reflection at all, actually; this is a purely academic question.)

Suppose there exists some class that looks like this:

public class MyClass {
    public string GetName() {
        return "My Name";
    }
}

Bear with me here. I know that if I have an instance of MyClass called x, I can call x.GetName(). Furthermore, I could set a Func<string> variable to x.GetName.

Now here’s my question. Let’s say I don’t know the above class is called MyClass; I’ve got some object, x, but I have no idea what it is. I could check to see if that object has a GetName method by doing this:

MethodInfo getName = x.GetType().GetMethod("GetName");

Suppose getName is not null. Then couldn’t I furthermore check if getName.ReturnType == typeof(string) and getName.GetParameters().Length == 0, and at this point, wouldn’t I be quite certain that the method represented by my getName object could definitely be cast to a Func<string>, somehow?

I realize there’s a MethodInfo.Invoke, and I also realize I could always create a Func<string> like:

Func<string> getNameFunc = () => getName.Invoke(x, null);

I guess what I’m asking is if there’s any way to go from a MethodInfo object to the actual method it represents, incurring the performance cost of reflection in the process, but after that point being able to call the method directly (via, e.g., a Func<string> or something similar) without a performance penalty.

What I’m envisioning might look something like this:

// obviously this would throw an exception if GetActualInstanceMethod returned
// something that couldn't be cast to a Func<string>
Func<string> getNameFunc = (Func<string>)getName.GetActualInstanceMethod(x);

(I realize that doesn’t exist; I’m wondering if there’s anything like it.)

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

    This kind of replaces my previous answer because this, although it’s a slightly longer route – gives you a quick method call and, unlike some of the other answers, allows you to pass through different instances (in case you’re going to encounter multiple instances of the same type). IF you don’t want that, check my update at the bottom (or look at Ben M’s answer).

    Here’s a test method that does what you want:

    public class TestType
    {
      public string GetName() { return "hello world!"; }
    }
    
    [TestMethod]
    public void TestMethod2()
    {
      object o = new TestType();
    
      var input = Expression.Parameter(typeof(object), "input");
      var method = o.GetType().GetMethod("GetName", 
        System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
      //you should check for null *and* make sure the return type is string here.
      Assert.IsFalse(method == null && !method.ReturnType.Equals(typeof(string)));
    
      //now build a dynamic bit of code that does this:
      //(object o) => ((TestType)o).GetName();
      Func<object, string> result = Expression.Lambda<Func<object, string>>(
        Expression.Call(Expression.Convert(input, o.GetType()), method), input).Compile();
    
      string str = result(o);
      Assert.AreEqual("hello world!", str);
    }
    

    Once you build the delegate once – you can cache it in a Dictionary:

    Dictionary<Type, Func<object, string>> _methods;
    

    All you do then is add it to the dictionary, using the incoming object’s Type (from GetType()) as the key. In the future, you first check to see if you have a ready-baked delegate in the dictionary (and invoke it if so), otherwise you build it first, add it in, and then invoke it.

    Incidentally, this is a very highly simplified version of the kind of thing that the DLR does for it’s dynamic dispatch mechanism (in C# terms, that’s when you use the ‘dynamic’ keyword).

    And finally

    If, as a few people have mentioned, you simply want to bake a Func bound directly to the object you receive then you do this:

    [TestMethod]
    public void TestMethod3()
    {
      object o = new TestType();
    
      var method = o.GetType().GetMethod("GetName", 
        System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    
      Assert.IsFalse(method == null && !method.ReturnType.Equals(typeof(string)));
    
      //this time, we bake Expression.Constant(o) in.
      Func<string> result = Expression.Lambda<Func<string>>(
       Expression.Call(Expression.Constant(o), method)).Compile();
    
      string str = result(); //no parameter this time.
      Assert.AreEqual("hello world!", str);
    }
    

    Note, though, that once the expression tree gets thrown away, you need to make sure that the o stays in scope, otherwise you could get some nasty results. The easiest way would be to hold on to a local reference (in a class instance, perhaps) for the lifetime of your delegate. (Removed as a result of Ben M’s comments)

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

Sidebar

Related Questions

I realize that virtual template functions are not allowed in c++. Because of my
I realize that a SO user has formerly asked this question but it was
I realize that the answer to this question is likely quite obvious (if somewhat
I realize that this question has been asked 100times but none that I have
Edit: Warning - I now realize that the following technique is generally regarded as
I've done numerous searches and I realize that I can just download this file
I realize this is partially subjective, but I'm generally curious as to community opinion
I have a separate class library that I'm using for all of my unit
I'm often confused by CSS override rules: in general, I realize that more specific
I realize that tinyint is a single byte integer (by the way, is it

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.