c# - Get MethodInfo from expression without knowing method signature -
i'm trying pass expression describes method want argument typed , don't want have know method signature or pass arguments in expression, this:
getmethod<myclass>(c => c.dosomething);
where dosomething
have method signature this... string dosomething(int id, int count)
i know can this:
memberinfo getmethod<t>(expression<func<t, delegate>> expression); //implementation getmethod<myclass>(c => new func<int, int, string>(c.dosomething))
but frankly, quite ugly.
is possible?
just have overload each possible action/func. won't cover all possibilities (have overload you've shown there cover edge cases) it'll handle of them.
the body of each of action/func overloads can call overload you've shown above actual implementation.
public memberinfo getmethod<t1, t2>(expression<func<t1, func<t2>>> expression) { return getmethodimpl(expression); } public memberinfo getmethod<t1, t2, t3>(expression<func<t1, func<t2, t3>>> expression) { return getmethodimpl(expression); } public memberinfo getmethod<t1, t2>(expression<func<t1, action<t2>>> expression) { return getmethodimpl(expression); } //...
getmethodimpl
can implemented so:
private memberinfo getmethodimpl<t1, t2>(expression<func<t1, t2>> expression) { }
that able slight modification of existing getmethod
implementation. t2
delegate; may need cast delegate
, depending on how use it.
Comments
Post a Comment