C# CodeDom - Create new field within CodeMemberMethod
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
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.
精彩评论