如何使用委托元素初始化字典?

How can I initialise a Dictionary with delegate elements?

我想创建一个将值映射到委托的静态 Dictionary。使用 Add 时效果很好:

class MyClass {
    private delegate void _processStatement(Statement statement);

    private static Dictionary<Statement.Types, _processStatement> _statementProcessors = 
           new Dictionary<Statement.Types, _processStatement>();

    public MyClass() {
           _statementProcessors.Add(Statement.Types.Increment, Increment);
    }

    private void Increment(Statement s) {}
}

但是,我想在实例化时初始化它们,像这样:

class MyClass {
    private delegate void _processStatement(Statement statement);

    private static Dictionary<Statement.Types, _processStatement> _statementProcessors = 
           new Dictionary<Statement.Types, _processStatement>() {
               { Statement.Types.Increment, Increment }
          }
    }
    private void Increment(Statement s) {}

}

这给了我错误

The best overloaded Add method 'Dictionary<Statement.Types, Machine._processStatement>.Add(Statement.Types, Machine._processStatement)' for the collection initializer has some invalid arguments

我不是很懂

我想要达到的目标有可能吗?还是我必须在构造函数中初始化它?

Update 在构造函数中添加它时出现此运行时错误:

An item with the same key has already been added. Key: Increment

看看这段代码:

class MyClass {
    private delegate void _processStatement(Statement statement);

    private static Dictionary<Statement.Types, _processStatement> _statementProcessors = 
           new Dictionary<Statement.Types, _processStatement>() {
               { Statement.Types.Increment, Increment }
          }
    }
    private void Increment(Statement s) {}
}

您想将 Dictionary 设为静态,但 IncrementMyClass 的实例方法。想一想——这将如何运作?为了调用 Increment,需要实例化 MyClass,但您想在此之前访问它。

你的第一个例子是正确的...

或者,您可以使 Increment 静态化:

class MyClass {
    private delegate void _processStatement(Statement statement);

    private static Dictionary<Statement.Types, _processStatement> _statementProcessors = 
           new Dictionary<Statement.Types, _processStatement>() {
               { Statement.Types.Increment, Increment }
          }
    }
    private static void Increment(Statement s) {}

}

或者,您可以实例化 MyClass 并引用方法:

class MyClass
{
    private delegate void _processStatement(Statement statement);

    private static Dictionary<Statement.Types, _processStatement> _statementProcessors;

    static MyClass()
    {
        var myClass = new MyClass();
        _statementProcessors = new Dictionary<Statement.Types, _processStatement>
        {
            { Statement.Types.Increment, myClass.Increment }
        };
    }

    private void Increment(Statement s) { }
}

或者,您可以创建 MyClass:

的静态实例
class MyClass
{
    private delegate void _processStatement(Statement statement);

    private static MyClass Instance { get; } = new MyClass();
    private static Dictionary<Statement.Types, _processStatement> _statementProcessors = new Dictionary<Statement.Types, _processStatement>
    {
        { Statement.Types.Increment, Instance.Increment }
    };

    private void Increment(Statement s) { }
}