如何使用CodeDom创建一个接口方法

时间:2014-03-27 20:51:32

标签: c# wcf interface codedom

我正在使用CodeDom生成我的WCF服务接口和实现文件。我在CodeMemberMethod上找不到任何可以生成接口方法的选项。如果我使用MemberAttributes.Abstract,我几乎就在那里,但它包含对接口无效的abstract关键字。有谁知道生成方法的正确语法?

以下是我为生成界面文件而开发的代码:     private void CreateServiceContractFile(Type entity,BoElement bo,DirectoryInfo serviceOutputDir,AppParameters appPrams)         {             CodeCompileUnit serviceFileUnit = new CodeCompileUnit();

        CodeNamespace importNameSpace = new CodeNamespace("");
        importNameSpace.Imports.Add(new CodeNamespaceImport("System"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Linq"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Runtime.Serialization"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.ServiceModel"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.ServiceModel.Web"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Text"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("ALVIN.BusinessObjects"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("DAOObjects = ALVIN.DAO"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("ALIAS.BusinessObjects"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Data.Entity.Validation"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.ComponentModel.DataAnnotations"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("System.Data.Objects"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("ALIAS.Common.Utility"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("ALIAS.EventMessages"));
        importNameSpace.Imports.Add(new CodeNamespaceImport("ALIAS.ServiceCommon"));


        importNameSpace.Imports.Add(new CodeNamespaceImport(appPrams.BoNameSpace));

        serviceFileUnit.Namespaces.Add(importNameSpace);

        string serviceName = string.Format("{0}Service", entity.Name);

        CodeTypeDeclaration serviceClass = new CodeTypeDeclaration(serviceName);
        serviceClass.IsClass = true;
        serviceClass.BaseTypes.Add(new CodeTypeReference("I" + serviceName));
        serviceClass.TypeAttributes = TypeAttributes.Public;
        serviceClass.Comments.AddRange(Helper.CreateComment("The Service Contract class"));

        CodeParameterDeclarationExpression headerParm = new CodeParameterDeclarationExpression(
            typeof(ALIASTransactionHeader), "header");

        CodeParameterDeclarationExpression requestParm = new CodeParameterDeclarationExpression(
            bo.BoName, "requestBO");

        CodeMemberMethod enterMethod = new CodeMemberMethod();

        MethodCommentInfo methodComments = new MethodCommentInfo();
        methodComments.CommentSummaryLines.Add(string.Format("Enters a {0} into the system.", bo.BoName));
        methodComments.CommentParameters.Add(new ParameterInfo("header", "Security and Logging information for the web method"));
        methodComments.CommentParameters.Add(new ParameterInfo("requestBO", string.Format("{0} to be entered into the system", bo.BoName)));

        enterMethod.Comments.AddRange(Helper.CreateMethodComment(methodComments));
        enterMethod.Parameters.Add(headerParm);
        enterMethod.Parameters.Add(requestParm);
        enterMethod.PrivateImplementationType = new CodeTypeReference("I" + serviceName);
        //enterMethod.Attributes = MemberAttributes.Public | MemberAttributes.Abstract;
        enterMethod.Name = string.Format("Enter{0}", bo.BoName);


        serviceClass.Members.Add(enterMethod);

        CodeNamespace serviceNameSpace = new CodeNamespace(bo.ServiceNameSpace);

        serviceNameSpace.Types.Add(serviceClass);
        serviceFileUnit.Namespaces.Add(serviceNameSpace);

        Helper.CreateFile(serviceFileUnit, new FileInfo(serviceOutputDir + @"\" + serviceName + @".svc.cs"));
    }

1 个答案:

答案 0 :(得分:6)

AFAIK,您不必为接口方法指定任何特殊属性。以下示例

var compUnit = new CodeCompileUnit();
var ns = new CodeNamespace("TestNamespace");
var iface = new CodeTypeDeclaration("ITest");
iface.IsInterface = true;
var mth = new CodeMemberMethod();
mth.Name = "DoSomething";
mth.ReturnType = new CodeTypeReference(typeof(string));
mth.Parameters.Add(new CodeParameterDeclarationExpression(typeof(int), "x"));
iface.Members.Add(mth);
ns.Types.Add(iface);
compUnit.Namespaces.Add(ns);
var csProv = new CSharpCodeProvider();
using (var tw = File.CreateText(@"test.cs"))
{
    var options = new CodeGeneratorOptions();
    csProv.GenerateCodeFromCompileUnit(compUnit, tw, options);
    tw.Flush();
}

生成以下有效输出:

namespace TestNamespace {


    public interface ITest {

        string DoSomething(int x);
    }
}