gpt4 book ai didi

c# - 如何使用 CodeDom 创建接口(interface)方法

转载 作者:行者123 更新时间:2023-12-02 08:37:16 26 4
gpt4 key购买 nike

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

这是我开发的用于生成接口(interface)文件的代码:

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"));
}

最佳答案

据我所知,您不必为接口(interface)方法指定任何特殊属性。以下示例

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);
}
}

关于c# - 如何使用 CodeDom 创建接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22698937/

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