46

They could be used as follows:

FieldInfo field = fieldof(string.Empty);
MethodInfo method1 = methodof(int.ToString);
MethodInfo method2 = methodof(int.ToString(IFormatProvider));

fieldof could be compiled to IL as:

ldtoken <field>
call FieldInfo.GetFieldFromHandle

methodof could be compiled to IL as:

ldtoken <method>
call MethodBase.GetMethodFromHandle

Whenever the typeof operator is used, you get perfect Find All References results. Unfortunately, as soon as you go to fields or methods, you end up with nasty hacks. I think you could do the following... or you can go back to getting a field by name.

public static FieldInfo fieldof<T>(Expression<Func<T>> expression)
{
    MemberExpression body = (MemberExpression)expression.Body;
    return (FieldInfo)body.Member;
}

public static MethodInfo methodof<T>(Expression<Func<T>> expression)
{
    MethodCallExpression body = (MethodCallExpression)expression.Body;
    return body.Method;
}

public static MethodInfo methodof(Expression<Action> expression)
{
    MethodCallExpression body = (MethodCallExpression)expression.Body;
    return body.Method;
}

public static void Test()
{
    FieldInfo field = fieldof(() => string.Empty);
    MethodInfo method1 = methodof(() => default(string).ToString());
    MethodInfo method2 = methodof(() => default(string).ToString(default(IFormatProvider)));
    MethodInfo method3 = methodof(() => default(List<int>).Add(default(int)));
}
12
  • 4
    Can you imagine how many times a year this feature would be used? Commented Jul 31, 2009 at 17:36
  • 2
    I'm writing a CLI implementation in C# - it would be extremely helpful for identifying special references in the IL in the JIT so I can treat them differently. For example, I want to replace any IL instance of call RuntimeHelpers.get_OffsetToStringData with ldc.i4 <int32 offset> inline (before native code generation). Commented Jul 31, 2009 at 17:40
  • 21
    It's a quite frequently requested feature, so apparently there are reasonably common usage cases. That said, obviously would not achieve the popularity of, say, auto props, or where clauses. Commented Jul 31, 2009 at 19:43
  • 17
    It would be an extremely used feature for anyone dealing with WPF (think dependency properties), and, more generally, anyone implementing INotifyPropertyChanged. Commented Aug 4, 2009 at 7:46
  • 2
    Even people who don't use WPF still throw ArgumentException Commented Jul 5, 2012 at 13:04

4 Answers 4

38

You can actually avoid using both reflection and lambdas (.NET Framework 2.0). Consider the following class:

public class methodof<T>
{
    private MethodInfo method;

    public methodof(T func)
    {
        Delegate del = (Delegate)(object)func;
        this.method = del.Method;
    }

    public static implicit operator methodof<T>(T methodof)
    {
        return new methodof<T>(methodof);
    }

    public static implicit operator MethodInfo(methodof<T> methodof)
    {
        return methodof.method;
    }
}

and it's usage:

MethodInfo writeln = (methodof<Action>)Console.WriteLine;
MethodInfo parse = (methodof<Func<string, int>>)int.Parse;
Sign up to request clarification or add additional context in comments.

5 Comments

+1 For a very elegant solution, plus style points for overloading the cast operators to make it look more built-in. Now if only C# would let us explicitly reference the get/set accessors on properties then this would work on those as well.
Unfortunately does not work
@hazzik It works for me. Please specify the error.
I agree it's nice, but unfortunately this requires you to explicitly specify the method signature, so it's basically the same as MethodInfo parse = ((Func<string, int>)int.Parse).Method; which is fully built-in and thus doesn't require any helper class. Seems to me, the whole point here is to avoid that cast, which would enable you to actually obtain the signature at runtime when all you need to provide at compile-time is the method name token.
I have problem with void (non returned) method.
23

Eric Lippert (on the C# design team) has an excellent overview/discussion on this topic here. To quote:

It’s an awesome feature that pretty much everyone involved in the design process wishes we could do, but there are good practical reasons why we choose not to. If there comes a day when designing it and implementing it is the best way we could spend our limited budget, we’ll do it. Until then, use Reflection.

3 Comments

'Foof' would've been a more appropriate expletive :)
I added a comment under my question with a use case that would be extremely helpful. Of course it could be used - it would just be extremely helpful if I could write C# code to fully leverage the ldtoken IL instruction. Or cpblk and initblk for that matter.
I know, I'll implement the hack, then add those helper methods to the list of calls I treat in a special way before the JIT! The only downside is it results in a small JIT-time performance hit, clumsy syntax, and a slightly larger assembly.
7

@280Z28 - We were just sitting down to figure out how to do this when I found your question & code. We needed a PropertyOf method so I added it. Here it is in case anybody else needs it. Thx for the great question.

     public static PropertyInfo PropertyOf<T>(Expression<Func<T>> expression)
    {
        MemberExpression body = (MemberExpression)expression.Body;
        PropertyInfo pi = body.Member as PropertyInfo;
        if (pi != null)
        {
            return pi;
        }
        else throw new ArgumentException("Lambda must be a Property.");
    } 

      [TestMethod()]
    public void MethodofPropertyOfTest<T>()
    {

        string foo = "Jamming";
        MethodInfo method1 = ReflectionHelper.Methodof(() => default(string).ToString());
        PropertyInfo prop = ReflectionHelper.PropertyOf(() => default(string).Length);
        Assert.AreEqual(method1.Invoke(foo, null), "Jamming");
        Assert.AreEqual(prop.GetGetMethod().Invoke(foo, null), foo.Length);
    }

3 Comments

Nicely done. I did something similar here: stackoverflow.com/questions/269578/… but I like yours as a solid general solution:
Something to note if you need this type of operation in production code - the value returned by a call to methodof, fieldof, or propertyof is constant for a given input. Whenever I need them in a class, I either have a static readonly field that I initialize to the FieldInfo/MethodInfo/PropertyInfo, or I use an appropriate Dictionary<Type,MemberInfo> map. If you look at the IL generated by the expression tree sugar, you can see you don't want to be running that unless you have to. The #1 advantage I see of having them as language operators is the IL could/would be very succinct.
Corner case: stackoverflow.com/questions/6658669/…. I think this is not the right approach for this kind of work. Also i think its better to check for unary expressions as in cases of boxed conversions like <object> () => IntProperty().
0

this is a generic type version, that can use F2 to rename and not need modify existed code in the example, the method name is GetPKValue

var typFactory = typeof(FormFieldFactory<>).MakeGenericType(entity.GetType());
var methodName = new Func<object, object>(FormFieldFactory<object>.GetPKValue).Method.Name;

var theMethod =
    (Func<object, object>)Delegate.CreateDelegate(
        typeof(Func<object, object>)
        , typFactory.GetMethod(methodName)
    );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.