Reputation: 2620
i am declaring a class with the help of CodeTypeDeclaration like this :
CodeTypeDeclaration targetClass = new CodeTypeDeclaration(sType);
I can add a constructor :
CodeConstructor constructor = new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
Or a member field :
CodeMemberField myField = new CodeMemberField();
myField.Name = fieldName;
myField.Type = new CodeTypeReference(fieldType);
targetClass.Members.Add(myField);
But i am trying to add any kind of line , for example a constant declaration :
const addressFilteresErrorCounters: UInt32 = 0x0000AE77;
Can i do this without using CodeMemberField ? Maybe somehow i can add to the class a CodeSnippetStatement, so let's simply say , add some line to the class by the using the force and not filtering the declaration line with the CodeMemberField ?
Maybe smth like this :
targetClass.Members.Add(new CodeSnippetStatement("var n = 2"));
Thanks.
Upvotes: 1
Views: 2617
Reputation: 604
You can't add a CodeSnippetStatement
directly to a class. You can, however, add them to a CodeMemberMethod
for example:
CodeMemberMethod method = new CodeMemberMethod();
method.Name = "DoSomething";
method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
method.Statements.Add(new CodeSnippetStatement("var n = 2;"));
Although you don't need to resort to a CodeSnippetStatement
to add a constant. You can use:
CodeTypeDeclaration exampleClass = new CodeTypeDeclaration("GeneratedClass");
CodeMemberField constant = new CodeMemberField(new CodeTypeReference(typeof(System.UInt32)), "addressFilteresErrorCounters");
constant.Attributes = MemberAttributes.Const;
constant.InitExpression = new CodePrimitiveExpression(0x0000AE77);
exampleClass.Members.Add(constant);
Upvotes: 2