我试图找出一种方法来使用 CodeDom 构建这样的东西
public System.Collections.Generic.Dictionary<string, string> Attributes
{
get
{
return new System.Collections.Generic.Dictionary<string, string>()
{
{"firstkey", "first value"},
{"second", "second value"},
{"third", "third value"}
};
}
}
我确实读过这个,但它并没有真正让我到达我想要的地方, http: //msdn.microsoft.com/en-us/library/system.codedom.codetypeparameter.aspx
我做了这个
Type myType = typeof (System.Collections.Generic.Dictionary<string, string>);
string dictionaryTypeName = myType.FullName;
CodeTypeReference dictionaryType = new CodeTypeReference(dictionaryTypeName);
var abc = new CodeVariableDeclarationStatement(
dictionaryType, "dict2",
new CodeObjectCreateExpression(
dictionaryType, new CodeSnippetExpression(@"{""firstkey"", ""first value""}")
)
);
property.GetStatements.Add(abc);
它生成这个
public Dictionary<object, object> Attributes
{
get
{
Dictionary<string, string> dict2 = new Dictionary<string, string>({"firstkey", "first value"});
}
}
有人建造过类似的东西吗?