我正在尝试找出一种使用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"});
}
}有人做过类似的东西吗?
发布于 2012-09-19 17:47:12
我必须使用.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;
}
}当然可以!
发布于 2012-09-19 20:43:06
如果你想使用初始化而不是Add方法,你也必须使用代码片段的方式来调用构造函数。如果您不想在常量字符串中写入类型名称,这可能会更加困难。在这种情况下,您可以创建对无参数构造函数的调用,使用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);https://stackoverflow.com/questions/12491060
复制相似问题