Kev*_*vin 1 c# variables dictionary
可能的重复:
具有值“变量”的字典
我有一些高度耦合的代码,我正在尝试解耦。如果我能制作一本这样的字典:
Dictionary<string, var> dict = new Dictionary<string, var>
可以修复很多耦合。另一个线程中的一位发帖人似乎找到了使用变量类的解决方案,但他/她没有发布该类的任何详细信息,我不知道如何做到这一点。
编辑:
基本上,而不是:
string transit = <string value>;
我希望能够写:
string dict["Transit"] = <string value>;
我已经完成的地方:
Dictionary<string, var> dict = new Dictionary<string, var>()
{
    {"Transit", transit}
};
这可能是不可能的,但欢迎任何建议。
问候。
这可以解决问题:
public class Variable
{
    public object Value { get; set; }
}
然后他就可以做他所要求的事情,即:
Dictionary<string, Variable> dict = new Dictionary<string, Variable>()
{
    {"Transit", new Variable()}
}
dict["Transit"].Value = "transit"