C# CodeDom - 在 CodeMemberMethod 中创建新字段
为了“填充”CodeDom 类,我可以创建一个新的字段
,它允许我为字符串、字节等生成随机名称。然后我在其中创建了一个新方法我通过 CodeDom 的类,但我在填充此方法时遇到很多麻烦。我发现我可以使用 CodeSnippetStatement
方法将直接字符串添加到 CodeDom 方法中,但我不想使用直接字符串。是否有其他方法来填充 CodeDom 方法?
这是我现在使用的:
CodeMemberMethod method = new CodeMemberMethod();
method.name = "mainMethod";
method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
// Here is where the code is added as a direct string:
method.Statements.Add(new CodeSnippetStatement("string myString = path.getTempPath();"));
myClass.Members.Add(method);
Namespaces.Types.Add(myClass);
我再次想知道是否有一种新方法可以用来将数据添加到 CodeDom 方法中。
谢谢你, 埃文
In order to "populate" a CodeDom class I can create a new field
which allows me to generate random names for strings, bytes, etc. I then created a new method within my class via CodeDom but I am having a lot of trouble populating this method. I have found that I can use the CodeSnippetStatement
method to add direct strings into the CodeDom method but I do not want to have to use direct strings. Is there some other way to populate a CodeDom method?
Here is what I am using now:
CodeMemberMethod method = new CodeMemberMethod();
method.name = "mainMethod";
method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
// Here is where the code is added as a direct string:
method.Statements.Add(new CodeSnippetStatement("string myString = path.getTempPath();"));
myClass.Members.Add(method);
Namespaces.Types.Add(myClass);
Once again, I would like to know if there is a new method I could use to add data into a CodeDom method.
Thank you,
Evan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法将字段(属于类型成员)添加到方法中。不过,您可以使用
CodeVariableDeclarationStatement
添加局部变量。有关可在方法中使用的各种类型的语句,请参阅 CodeStatement 的继承层次结构,网址为 http://msdn.microsoft.com/en-us/library/system.codedom.codestatement#inheritanceContinued。You can't add a field (which is a type member) to a method. You can, however, add a local variable by using a
CodeVariableDeclarationStatement
. For the various types of statements available for use in methods, see the inheritance hierarchy of CodeStatement at http://msdn.microsoft.com/en-us/library/system.codedom.codestatement#inheritanceContinued.