Retrieving the name of the invoked method executed in a Func
Asked Answered
F

3

2

I would like to get the name of the method that is being delegated as a Func.

Func<MyObject, object> func = x => x.DoSomeMethod();
string name = ExtractMethodName(func); // should equal "DoSomeMethod"

How can I achieve this?

-- For bragging rights --

Make ExtractMethodName also work with a property invocation, having it return the property name in that instance.

eg.

Func<MyObject, object> func = x => x.Property;
string name = ExtractMethodName(func); // should equal "Property"
Fleabag answered 4/8, 2009 at 3:43 Comment(0)
U
11

Look Ma! No expression trees!

Here's a quick, dirty and implementation-specific version that grabs the metadata token from the IL stream of the underlying lambda and resolves it.

private static string ExtractMethodName(Func<MyObject, object> func)
{
    var il = func.Method.GetMethodBody().GetILAsByteArray();

    // first byte is ldarg.0
    // second byte is callvirt
    // next four bytes are the MethodDef token
    var mdToken = (il[5] << 24) | (il[4] << 16) | (il[3] << 8) | il[2];
    var innerMethod = func.Method.Module.ResolveMethod(mdToken);

    // Check to see if this is a property getter and grab property if it is...
    if (innerMethod.IsSpecialName && innerMethod.Name.StartsWith("get_"))
    {
        var prop = (from p in innerMethod.DeclaringType.GetProperties()
                    where p.GetGetMethod() == innerMethod
                    select p).FirstOrDefault();
        if (prop != null)
            return prop.Name;
    }

    return innerMethod.Name;
}
Unit answered 4/8, 2009 at 4:13 Comment(2)
Thanks! I didn't expect things to get that funky but it works as desired.Fleabag
What if the method is in a different module? Shouldn't you get the module of the function's parameter?Delegation
M
0

I don't think this is possible in the general case. What if you had:

Func<MyObject, object> func = x => x.DoSomeMethod(x.DoSomeOtherMethod());

What would you expect?

That being said, you could use reflection to open up the Func object and see what it does inside, but you'll only be able to solve it for certain cases.

Megasporophyll answered 4/8, 2009 at 3:47 Comment(1)
Given what you have I would want the answer to be "DoSomeMethod". Is it possible that the property example I provided is in fact easier because that would limit the potential complexity of the Func interrogation?Fleabag
B
0

Check out my hack answer here:

Why is there not a `fieldof` or `methodof` operator in C#?

In the past I did it another way that used Func instead of Expression<Func<...>>, but I was much less pleased with the result. The MemberExpression used to detect the field in my fieldof method will return a PropertyInfo when a property is used.

Edit #1: This works for a subset of the problem:

Func<object> func = x.DoSomething;
string name = func.Method.Name;

Edit #2: Whoever marked me down should take a second to realize what's going on here. The expression trees can be implicitly used with lambda expressions and are the fastest, most reliable way to get the specific requested information here.

Basuto answered 4/8, 2009 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.