[英]C# Using CodeDom to add variables as part of a class
I am attempting to create the following code through the use of CodeDom:我正在尝试通过使用 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.我创建 class 没有问题,并且我找到了通过 CodeDom 使用
CodeVariableDeclarationStatement
方法来声明变量的方法,但我不确定如何添加变量声明作为我的 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.
谢谢你的帮助,埃文。
Try to use this尝试使用这个
CodeMemberField field = new CodeMemberField(typeof(byte[]), "bytes");
field.Attributes = MemberAttributes.Public;
mainClass.Members.Add(field);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.