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

标签 c# asp.net .net code-generation codedom

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/

相关文章:

c# - ServiceStack JsonSerializer DeserializeFromString 正在删除双引号

c# - 使用当前线程区域性与两位数的月份和日期格式化日期?

c# - 带附加参数的 WCF REST 文件上传

c# - 不同系统配置的最佳IO线程数

c# - 从 Vista 上的 NT 服务运行时,从 Excel COM 对象访问文件失败

c# - 要在自定义 HqlGenerator 中使用的 HqlCase 实现

c# - 使用 SQLite 测试 ASP Net Core 应用程序抛出未知函数 : newid()

C# vuforia.image.pixels 覆盖到 iOS CIImage 或 CGImage

c# - 带触发器的 UpdateProgress 不起作用

c# - Fluent Validation,在 Asp.NET Core 中对列表中的每个项目进行不同的验证