开发者

C# Using CodeDom to add variables as part of a class

开发者 https://www.devze.com 2023-03-19 04:42 出处:网络
I am attempting to create the following code through the use of CodeDom: public partial class mainClass

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 开发者_JAVA百科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.


Try to use this

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

mainClass.Members.Add(field);
0

精彩评论

暂无评论...
验证码 换一张
取 消