【问题标题】:How can I initialise a Dictionary with delegate elements?如何使用委托元素初始化字典?
【发布时间】:2021-05-02 15:31:41
【问题描述】:

我想创建一个静态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) {}

}

这给了我错误

集合初始值设定项的最佳重载 Add 方法 'Dictionary.Add(Statement.Types, Machine._processStatement)' 有一些无效参数

我不完全理解。

有没有可能,我想要实现的目标是什么?还是我必须在构造函数中初始化它?

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

已添加具有相同密钥的项目。键:增量

【问题讨论】:

  • Statement.Types.Increment 是如何成为私有委托 _processStatement 的委托的?
  • Statement.Types.Increment 是键,代理Increment() 是值,在字典中。
  • 字典初始化符号new Dictionary&lt;, &gt;() { [Statement.Types.Increment] = Increment } 工作吗?如果不是,您可能需要使用强制转换,即 (_processStatement)Increment 或可能是 lambda 表示法 (Statement x) =&gt; Increment(x),并查看 IDE 是否建议进行代码修复以使其更简洁
  • 不,我的意思是,您是否尝试过我建议的特定语法。有两种类型的集合初始化器可用于字典。一个像你用的,另一个像我建议的(仔细看,方括号)
  • @pinkfloydx33 是的,我明白了,这就是我删除评论的原因。当我尝试这样做时,我收到错误A field initializer cannot reference the non-static field, method, or property 'Machine.Increment(Statement)'

标签: c# dictionary delegates


【解决方案1】:

看看这段代码:

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) { }
}

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 2011-01-21
    相关资源
    最近更新 更多