I'm trying to write an extension method that will give me the MemberInfo
representing a member of a given type, using lambda expressions. Ideally, I'd like to be able to write
var info = MyType.GetMember(m => m.MyProperty);
or, also acceptable
var info = typeof(MyType).GetMember(m => m.MyProperty);
or even
var info = typeof(MyType).GetMember((MyType m) => m.MyProperty);
I have a generic method signature that works, but requires me to specify all the type parameters, and I'd very much like C# to infer them. As far as I can see, if I just find the right way to specify the extension method signature, there should be enough information in (at least the last one of) the code snippets to infer everything - but according to the compiler, there isn't.
I've read an old blog post on static extension methods but I haven't been able to find anything on it more recent than that. If that came true, I'd be able to write
public static MemberInfo GetMember<TType, TReturnType>(static TType, Expression<Func<TType, TReturnType>> member)
which would solve my problem. But as I said, I seem to be stuck with instance extensions, in which case
public static MemberInfo GetMember<TType, TReturnType>(this Type t, Expression<Func<TType, TReturnType>> member)
just isn't good enough for the compiler to infer type members.