CodeDom和集合初始值设定项

有没有办法使用C#CodeDom生成字典初始化程序? 那些支持吗?

我想拥有:

private IDictionary map = new Dictionary { { "Name", "Value" }, ... }; 

使用CodeDom构造无法做到这一点。 它们未针对集合初始化程序进行更新。

LukeH有一篇关于3.5特性和CodeDom主题的优秀博客文章

你可以做到但它可能是有史以来最糟糕的噩梦。这就是我现在正在做的事情。

 var lgoDictionary = new CodeMemberField("Dictionary> _setValue", @" = new Dictionary>() { { NetMsg.Bool, (objectId, classId, index, packet)=> Classes[objectId][classId].SetBool[index].Invoke(packet.PullBool()) }, { NetMsg.Int, (objectId, classId, index, packet)=> Classes[objectId][classId].SetInt[index].Invoke(packet.PullInt()) }, { NetMsg.UInt, (objectId, classId, index, packet)=> Classes[objectId][classId].SetUInt[index].Invoke(packet.PullUInt()) }, { NetMsg.Long, (objectId, classId, index, packet)=> Classes[objectId][classId].SetLong[index].Invoke(packet.PullLong()) }, { NetMsg.ULong, (objectId, classId, index, packet)=> Classes[objectId][classId].SetULong[index].Invoke(packet.PullULong()) }, { NetMsg.Byte, (objectId, classId, index, packet)=> Classes[objectId][classId].SetByte[index].Invoke(packet.PullByte()) }, { NetMsg.Short, (objectId, classId, index, packet)=> Classes[objectId][classId].SetShort[index].Invoke(packet.PullShort()) }, { NetMsg.UShort, (objectId, classId, index, packet)=> Classes[objectId][classId].SetUShort[index].Invoke(packet.PullUShort()) }, { NetMsg.Char, (objectId, classId, index, packet)=> Classes[objectId][classId].SetChar[index].Invoke(packet.PullChar()) }, { NetMsg.Float, (objectId, classId, index, packet)=> Classes[objectId][classId].SetFloat[index].Invoke(packet.PullFloat()) }, { NetMsg.Double, (objectId, classId, index, packet)=> Classes[objectId][classId].SetDouble[index].Invoke(packet.PullDouble()) }, { NetMsg.String, (objectId, classId, index, packet)=> Classes[objectId][classId].SetString[index].Invoke(packet.PullString()) }, }");