简体   繁体   English

如何使用Codedom创建匿名类型

[英]How to create an anonymous type using codedom

Essentially I am aiming to get something like the following code as output from my codedom: 从本质上讲,我的目标是从我的代码仓库中获取类似以下代码的内容:

var foo = new { variable1 = "value", variable2 = 5 };

I won't just be using it for a variable assignment but I wanted a simple example of how to create an anonymous type. 我不仅将它用于变量赋值,还想要一个有关如何创建匿名类型的简单示例。 I'm not sure that this exact code is possible to achieve but if there's some kind of work around you could point me in the direction of I would be much obliged. 我不确定是否可以实现这种精确的代码,但是如果您周围有某种工作可以使我朝我的方向努力,那我将非常有义务。

I've had a look at the CodeObjectCreateExpression and I don't think that's going to do what I'm looking for and I can't find anything else that comes close. 我看过CodeObjectCreateExpression ,但我认为这不会做我要寻找的东西,而且我找不到其他可以接近的东西。

Thanks in advance for all and any help. 在此先感谢您提供的所有帮助。

CodeDOM attempts to be language-neutral and hasn't been updated for a very long time, so I don't think you can use any of its built-in types to do this. CodeDOM试图保持语言中立,并且很长时间没有更新,因此我认为您不能使用其任何内置类型来做到这一点。 But you can use CodeSnippetExpression to inject this code as string: 但是您可以使用CodeSnippetExpression将此代码作为字符串注入:

var expression = new CodeSnippetExpression("new { variable1 = \"value\", variable2 = 5 }");
var statement = new CodeVariableDeclarationStatement("var", "foo", expression);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM