CodeDom如何创建一个void返回类型的方法?

时间:2016-07-25 08:08:05

标签: 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]);
        }

获取虚拟空白

我只需要无效。

1 个答案:

答案 0 :(得分:5)

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

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