3

I am attempting to create the following code through the use of CodeDom:

public partial class mainClass
{
    public byte[] bytes = null;
}

I have no problem creating the class, and I found ways to declare variables through the use of CodeDom using the CodeVariableDeclarationStatement method, but I am unsure of how to add a variable declaration as part of my class.

Here is what I have tried thus far:

CodeTypeDeclaration mainClass = new CodeTypeDeclaration("mainClass");
mainClass.IsPartial = true;
mainClass.IsClass = true;
mainClass.Attributes = MemberAttributes.Public;
Namespaces.Types.Add(mainClass);

CodeVariableDeclarationStatement variableDeclaration = new(CodeVariableDeclarationStatement(typeof(byte[]), "bytes", new CodePrimitiveExpression("String.Empty");

I am open to any suggestions and ideas. Thank you for any help, Evan.

1 Answer 1

1

Try to use this

CodeMemberField field = new CodeMemberField(typeof(byte[]), "bytes");
field.Attributes = MemberAttributes.Public;

mainClass.Members.Add(field);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. What if my declaration was to be part of a method, though. Would I use my original code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.