静态字典包含代表实例的方法 [英] Static dictionary containing delegates to instance methods

查看:110
本文介绍了静态字典包含代表实例的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public bool ExecuteCommand(string command,string args)
{
bool result = false;
switch(command)
{
casecommand1:result = Method1(args);打破;
casecommand2:result = Method2(args);打破;
//等
}
返回结果;
}
private bool Method1(string args){...}

现在我考虑用 Func<> 代码的字典替换它,以便我可以消除switch语句:

 私人字典< string,Func< string,bool>> _commands = new ...; 
public MyClass()
{
_commands.Add(command1,Method1);
// etc:
}
public bool ExecuteCommand(string command,string args)
{
return _commands [command](args);
}

我看到的问题是,一个新的Dictionary被实例化和填充每个新的MyClass实例。



有可能以某种方式使该Dictionary(包含代理到实例方法)成为一个静态成员,它将被初始化一次,在静态构造函数?



例如这样的东西(不行):

 私人静态字典< string,Func< string,bool>> _commands = new ...; 
static MyClass()
{
//以下行将导致编译器错误:
//错误CS0120:非静态字段需要对象引用,
//方法,或属性'MyClass.Method1(string,string)'
_commands.Add(command1,MyClass.Method1);
}


解决方案

/ em>在静态构造函数中初始化它 - 但是您需要创建 MyClass 的实例,这可能不是你想要的,因为我假设你希望命令执行在执行已被调用的实例的上下文中。



或者,您可以填充与 $ p> class MyClass
{
static Dictionary< string,Func< MyClass,string,bool>>命令
=新字典< string,Func< MyClass,string,bool>>
{
{Foo,(@this,x)=> @ this.Foo(x)},
{Bar,(@this,y)=> @ this.Bar(y)}
};

public bool Execute(string command,string value)
{
return commands [command](this,value);
}

public bool Foo(string x)
{
return x.Length> 3;
}

public bool Bar(string x)
{
return x ==;
}
}

理论上我认为没有lambda通过创建开放代表来表达,但它需要使用反射更多的工作。如果您不介意额外间接的丑恶和性能损失,我认为这种方法应该很好。


I have this method with a huge switch statement like this:

public bool ExecuteCommand(string command, string args)
{
    bool result = false;
    switch (command)
    {
        case "command1": result = Method1(args); break;
        case "command2": result = Method2(args); break;
        // etc.
    }
    return result;
}
private bool Method1(string args) {...}

Now I thought about replacing this with a dictionary of Func<> delegates so that I can eliminate the switch statement:

private Dictionary<string, Func<string, bool>> _commands = new ...;
public MyClass()
{
    _commands.Add("command1", Method1);
    // etc:
}
public bool ExecuteCommand(string command, string args)
{
    return _commands[command](args);
}

The problem I see with this, is that a new Dictionary is instantiated and populated with each new instance of MyClass.

Is it possible to somehow make that Dictionary (containing delegates to instance methods) a static member, which would be initialized only once, in the static constructor?

E.g. something like this (does not work):

private static Dictionary<string, Func<string, bool>> _commands = new ...;
static MyClass()
{
    // the following line will result in a compiler error:
    // error CS0120: An object reference is required for the non-static field,
    // method, or property 'MyClass.Method1(string, string)'
    _commands.Add("command1", MyClass.Method1);
}

解决方案

You can initialize it in the static constructor - but you'll need to create instances of MyClass, which may not be what you want, because I assume you want the command to execute "in the context of" the instance which Execute has been called on.

Alternatively, you can populate the dictionary with delegates which take an instance of MyClass as well, like this:

class MyClass
{
    static Dictionary<string, Func<MyClass, string, bool>> commands
        = new Dictionary<string, Func<MyClass, string, bool>>
    {
        { "Foo", (@this, x) => @this.Foo(x) },
        { "Bar", (@this, y) => @this.Bar(y) }
    };

    public bool Execute(string command, string value)
    {
        return commands[command](this, value);
    }

    public bool Foo(string x)
    {
        return x.Length > 3;
    }

    public bool Bar(string x)
    {
        return x == "";
    }
}

In theory I believe it should be doable without the lambda expression by creating an "open delegate", but it would need a bit more work using reflection. If you don't mind the ugliness and tiny performance penalty of the extra indirection, I think this approach should work quite well.

这篇关于静态字典包含代表实例的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆