CodeDom and collection initializers

家住魔仙堡 提交于 2019-12-03 20:12:48

This is not possible using the CodeDom constructs. They were not updated for collection initializers.

LukeH has an excellent blog post on the subject of 3.5 features and the CodeDom

You can do it but its possibly the worst nightmare ever.. Here is how I am doing it currently. (Updated my answer to reflect the question)

var constructDictionary = new CodeMemberField("Dictionary<string, string> map", @" = new Dictionary<string, string>()
{
   { Name, Value },
}");

This can also be populated once again its hackish.

string builder += " = new Dictionary<string, string>(){";
for(int i = 0; i < 10; i++)
{
   builder+="{ Name"+i+", Value"+i+" }";
}
var constructDictionary = new CodeMemberField("Dictionary<string, string> map", builder+" }");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!