Hi I am trying to achive something like this;
Method<TObjectType>(m=>m.GetData); //m is the instance of TObjectType
if I can succeed on that, then I can visit this expression and get the GetData Method and use it for creating a dynamic sql query. I could do this by giving method name as a string however I don’t want to break the strongly type world of my developer friends.
I know I have to give the exact definition of the delegate but this still doesn’t helped me;
void Method<TObjectType>(Expression<Func<TObjectType, Delegate>> ex){/**/}
Do you have an idea?
Unfortunately, there’s barely such a thing as “a delegate”. For most purposes (and especially: for resolving a
MethodInfo), it must be a strongly-typed delegate. This is becauseGetDataisn’t a method, it is a method group. You really need to specify the method precisely, or have a known delegate-type (which ultimately does the same thing).You have two practical options; work to
object, or add a generic. For example:would work, as would:
The caller would use:
If you really want to use a delegate, the type must be predictable, for example:
allowing:
Alternatively, if you know (for example) that the method is always parameterless, but you don’t know the return type; maybe something like:
which allows: