2

假设我有这个通用方法

void ActivateView<T>(ViewCommand command) where T : IPresenter
{
    //code
}

我有一个动作:

Action<ViewCommand> action = this.ActivateView<DiagnosticPresenter>;

现在给出action,我怎么知道泛型方法的类型参数ActivateView?在这种情况下,它应该是DiagnosticPresenter。所以我期待一个Type等于的实例typeof(DiagnosticPresenter)

Type type = Magic(action); //what should Magic do?
if ( type == typeof(DiagnosticPresenter))
{
    //I want to do something here!
}

那可能吗?我应该如何实施Magic()

4

1 回答 1

3

这似乎对我有用:

public Type Magic(Action action)
{
    return action.Method.GetGenericArguments().First();
}
于 2012-07-25T13:07:21.510 回答