你可以使用代表.例如,如果您有这些方法:
public void Login() {
// ...
}
public void Logout() {
// ...
}
Run Code Online (Sandbox Code Playgroud)
你可以用这个Dictionary:
Dictionary<string, Action> actions = new Dictionary<string, Action>() {
{"Login", Login},
{"Logout", Logout}
};
Run Code Online (Sandbox Code Playgroud)
然后称之为:
actions[myAction]();
Run Code Online (Sandbox Code Playgroud)
当然,您需要确保密钥存在.您可以像调用常规方法一样调用代理.如果他们有参数或返回值,只需使用适当的Action<T1, T2...>或Func<T1, T2... TOut>.
| 归档时间: |
|
| 查看次数: |
954 次 |
| 最近记录: |