这是一个让我感到困惑的简单问题。
我有两个类和一个字典(为了示例而简化):
class Result {       
    public string Description;
}
class Error {
    public int ErrorCode;
}
Dictionary<int, string> errorCodeToMessage = new Dictionary<int, string> {
    { 0, "Item not found" },
    { 1, "Connection error" }
}
在我继承的代码库中,我经常看到这一行:
Result result = new Result {
    Description = errorCodeToMessage[error.ErrorCode];
}
我不希望字典被全部使用,我希望将此逻辑封装在Result对象或Error对象中。
我考虑在Result对象中创建一个新的构造函数,该构造函数将接受 ErrorCode 并在那里执行逻辑。但我不确定这是最好的方法。
你会怎么做?