[英]C# Using CodeDom to add variables as part of a class
我正在嘗試通過使用 CodeDom 創建以下代碼:
public partial class mainClass
{
public byte[] bytes = null;
}
我創建 class 沒有問題,並且我找到了通過 CodeDom 使用CodeVariableDeclarationStatement
方法來聲明變量的方法,但我不確定如何添加變量聲明作為我的 class 的一部分。
這是我迄今為止嘗試過的:
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");
我願意接受任何建議和想法。 謝謝你的幫助,埃文。
嘗試使用這個
CodeMemberField field = new CodeMemberField(typeof(byte[]), "bytes");
field.Attributes = MemberAttributes.Public;
mainClass.Members.Add(field);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.