gpt4 book ai didi

c# - CodeDom 如何创建具有 void 返回类型的方法?

转载 作者:行者123 更新时间:2023-12-02 17:40:55 24 4
gpt4 key购买 nike

public void CreateMethod()
{
CodeMemberMethod mymethod = new CodeMemberMethod();
mymethod.Name = testMethod;
CodeTypeReference ctr = new CodeTypeReference();
//Assign the return type to the method.
mymethod.ReturnType = ctr;

CodeSnippetExpression snippet1 = new CodeSnippetExpression("AutomationBase obj = new AutomationBase()");
CodeSnippetExpression snippet2 = new CodeSnippetExpression("obj.Execute(testCases[1])");
CodeExpressionStatement stmt1 = new CodeExpressionStatement(snippet1);
CodeExpressionStatement stmt2 = new CodeExpressionStatement(snippet2);
mymethod.Statements.Add(stmt1);
mymethod.Statements.Add(stmt2);
mymethod.Attributes = MemberAttributes.Public;
myclass.Members.Add(mymethod);
}

输出

 public virtual void TestCaseId002() {
AutomationBase obj = new AutomationBase();
obj.Execute(testCases[1]);
}

获得虚拟虚空

我只需要void。

最佳答案

您需要将该方法标记为Final,以表明它不可继承:

mymethod.Attributes = MemberAttributes.Public | MemberAttributes.Final;

关于c# - CodeDom 如何创建具有 void 返回类型的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38562581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com