问题
I am using CodeDom to generate my WCF service interfaces and implementation files. I cannot find any options on CodeMemberMethod that will allow me to generate an interface method. If I use MemberAttributes.Abstract I am almost there but it includes the abstract keyword which is invalid for an interface. Does anyone know the correct syntax to generate the method?
Here is the code I have developed to generate the interface file: 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:
AFAIK, you do not have to specify any special attributes for an interface method. The following sample
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();
}
Generates the following, valid output:
namespace TestNamespace {
public interface ITest {
string DoSomething(int x);
}
}
来源:https://stackoverflow.com/questions/22698937/how-to-create-an-interface-method-with-codedom