Unfortunately, there is hardly such a thing as a “delegate”. For most purposes (and especially: to allow MethodInfo
), it should be a strongly typed delegate. This is due to the fact that GetData
not a method, it is a group of methods. You really need to specify the method exactly or have a known delegate type (which ultimately does the same thing).
You have two practical options; work with object
or add a shared one. For instance:
void Method<TObjectType>(Expression<Func<TObjectType,object>> ex) {...}
will work like:
void Method<TObjectType, TValue>(Expression<Func<TObjectType,TValue>> ex) {...}
The caller will use:
Method<Foo>(x => x.GetData());
If you really want to use a delegate, the type should be predictable, for example:
void Method<TObjectType>(Expression<Func<TObjectType,Func<int>>> ex)
allows you to:
Method<Foo>(x => x.GetData);
Alternatively, if you know (for example) that the method is always without parameters, but you do not know the return type; maybe something like:
void Method<TObjectType, TValue>(Expression<Func<TObjectType,Func<TValue>>> ex)
which allows:
Method<Foo, int>(x => x.GetData);
source share