使用初始化程序进行弱类型赋值

时间:2013-05-22 15:03:36

标签: c# syntax

我喜欢语法:

Something something = new Something
{
  Property1 = "property1",
  Property2 = "property2"
}

但在当前项目中,我看到以下内容:

Something something = new Something();
something["Property1"] = "property1";
something["Property2"] = "property2";

我想知道是否有办法重构后者以某种方式接受后者的语法。我试图看看intellisense会提出什么建议但是没有更聪明。建议?

1 个答案:

答案 0 :(得分:0)

如果某事是字典那么

Something something = new Something()
{
    {"Property1", "property1"},
    {"Property2", "property2"}
}