我正试图找出一种使用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"});
}
}
任何建造类似东西的人?
答案 0 :(得分:1)
我必须使用.add而不是将值作为参数添加到构造函数中。
if (type == typeof(Dictionary<string, string>))
{
string variableName = "dict";
CodeVariableDeclarationStatement codeVariableDeclarationStatement = new CodeVariableDeclarationStatement(new CodeTypeReference(type.FullName), variableName,
new CodeObjectCreateExpression(type)
);
property.GetStatements.Add(codeVariableDeclarationStatement);
property.GetStatements.Add(AddPropertyValues(new CodeSnippetExpression(string.Format(@"""{0}"", ""{1}""", "a", "xx xx1")), variableName));
property.GetStatements.Add(AddPropertyValues(new CodeSnippetExpression(string.Format(@"""{0}"", ""{1}""", "b", "xx xx2")), variableName));
property.GetStatements.Add(AddPropertyValues(new CodeSnippetExpression(string.Format(@"""{0}"", ""{1}""", "c", "xx xx3")), variableName));
property.GetStatements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(variableName)));
}
static CodeStatement AddPropertyValues(CodeExpression exp, string variableReference)
{
return new CodeExpressionStatement(
new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeTypeReferenceExpression(new CodeTypeReference(variableReference)),
"Add"),
new CodeExpression[]{
exp,
}));
}
生成此
public System.Collections.Generic.Dictionary<string, string> Attributes
{
get
{
System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>();
dict.Add("a", "xx xx1");
dict.Add("b", "xx xx2");
dict.Add("c", "xx xx3");
return dict;
}
}
足够公平!
答案 1 :(得分:1)
如果要使用初始化而不是使用Add方法,则必须使用Snippet方式进行构造函数的调用。如果您不想在常量字符串中写入类型名称,这可能会更加困难。在这种情况下,您可以创建对无参数构造函数的调用,使用CSharpCodeProvider获取表示构造函数调用的字符串,丢弃最后的括号并连接初始化代码段。代码将是这样的,并生成所需的代码:
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.IndentString = " ";
options.BracingStyle = "C";
Type myType = typeof(System.Collections.Generic.Dictionary<string, string>);
string dictionaryTypeName = myType.FullName;
CodeTypeReference dictionaryType = new CodeTypeReference(dictionaryTypeName);
// here you create the CodeobjectCreateExpression in order to obtain the string with the name of the type
StringWriter sw = new StringWriter();
CodeObjectCreateExpression createExpr = new CodeObjectCreateExpression(dictionaryType);
codeProvider.GenerateCodeFromExpression(createExpr, sw, options);
string creationCode = sw.ToString();
// throw away the final ()
creationCode = creationCode.Substring(0, creationCode.Length - 2);
// add initialization
creationCode = creationCode + @"{{""firstkey"", ""first value""}, {""secondkey"", ""second value""}, {""thirdkey"", ""third value""}}";
CodeMethodReturnStatement retVal = new CodeMethodReturnStatement(new CodeSnippetExpression(creationCode));
property.GetStatements.Add(retVal);