在泛型方法中基于类填充列表时出错

时间:2015-01-28 09:22:20

标签: c# .net list class

我在11个不同的类(处理Web服务)

中有一个如下定义的列表
private List<edbService> genEdbService;

internal class edbService
{
    public string ServiceID { get; set; }
    public string ServiceName { get; set; }
    public string ServiceDescr { get; set; }
    public string ServiceInterval { get; set; }
    public string ServiceStatus { get; set; }
    public string ServiceUrl { get; set; }
    public string SourceApplication { get; set; }
    public string DestinationApplication { get; set; }
    public string Function { get; set; }
    public string Version { get; set; }
    public string userid { get; set; }
    public string credentials { get; set; }
    public string orgid { get; set; }
    public string orgunit { get; set; }
    public string customerid { get; set; }
    public string channel { get; set; }
    public string ip { get; set; }
}

通过从每个类中的xml文件中读取Web服务配置数据,在每个类中填充列表:

public DCSSCustomerCreate_V3_0()
{
try
{
    XElement x = XElement.Load(global::EvryCardManagement.Properties.Settings.Default.DataPath + "CustomerCreate.xml");
    // Get global settings        
    IEnumerable<XElement> services = from el in x.Descendants("Service")
         select el;
    if (services != null)
    {
        edb_service = new List<edbService>();

        // edb_service= Common.populateEDBService("CustomerCreate.xml");

        foreach (XElement srv in services)
        {
        edbService edbSrv = new edbService();

        edbSrv.ServiceID = srv.Element("ServiceID").Value;
        edbSrv.ServiceName = srv.Element("ServiceName").Value;
        edbSrv.ServiceDescr = srv.Element("ServiceDescr").Value;
        edbSrv.ServiceInterval = srv.Element("ServiceInterval").Value;
        edbSrv.ServiceStatus = srv.Element("ServiceStatus").Value;
        edbSrv.ServiceUrl = srv.Element("ServiceUrl").Value;
        foreach (XElement ServiceHeader in srv.Elements("ServiceHeader"))
        {
        ... 

现在我要做的是将这段代码放在Common.cs类的一个地方,所以我试过了:

public static List<edbService> populateEDBService(string xmlDataFile)
{
    try
    {
    XElement x = XElement.Load(global::EvryCardManagement.Properties.Settings.Default.DataPath + xmlDataFile);

    // Get global settings
    IEnumerable<XElement> services = from el in x.Descendants("Service")
         select el;
    if (services != null)
    {
      //edb_Service = new List<edbService>();
      foreach (XElement srv in services)
      {
      edbService edbSrv = new edbService();

      edbSrv.ServiceID = srv.Element("ServiceID").Value;
      edbSrv.ServiceName = srv.Element("ServiceName").Value;
      edbSrv.ServiceDescr = srv.Element("ServiceDescr").Value;
      edbSrv.ServiceInterval = srv.Element("ServiceInterval").Value;
      edbSrv.ServiceStatus = srv.Element("ServiceStatus").Value;
      edbSrv.ServiceUrl = srv.Element("ServiceUrl").Value;

      foreach (XElement ServiceHeader in srv.Elements("ServiceHeader"))
        {
        edbSrv.SourceApplication = ServiceHeader.Element("SourceApplication").Value;
        edbSrv.DestinationApplication = ServiceHeader.Element("DestinationApplication").Value;
        edbSrv.Function = ServiceHeader.Element("Function").Value;
        edbSrv.Version = ServiceHeader.Element("Version").Value;

        foreach (XElement ClientContext in ServiceHeader.Elements("ClientContext"))
          {
          edbSrv.userid = ClientContext.Element("userid").Value;
          edbSrv.credentials = ClientContext.Element("credentials").Value;
          edbSrv.orgid = ClientContext.Element("orgid").Value;
          edbSrv.orgunit = ClientContext.Element("orgunit").Value;
          edbSrv.customerid = ClientContext.Element("customerid").Value;
          edbSrv.channel = ClientContext.Element("channel").Value;
          edbSrv.ip = ClientContext.Element("ip").Value;
          }
        }

     // populateEDBService.Add(edbSrv);
     }
}
}
catch (Exception ex)
{
    /* Write to log */
    Common.logBuilder("CustomerCreate : Form --> CustomerCreate <--", "Exception", Common.ActiveMQ,
       ex.Message, "Exception");
    /* Send email to support */
    emailer.exceptionEmail(ex);
    }
return;
}

现在我在return;上遇到了An object of a type convertible to 'System.Collections.Generic.List<EvryCardManagement.Common.edbService>' is required

的编译错误

并且在应该调用此方法的类中,我想做类似的事情:

edb_service = Common.populateEDBService("CustomerUpdate.xml");

但我收到错误Cannot implicitly convert type 'System.Collections.Generic.List<EvryCardManagement.Common.edbService>' to 'System.Collections.Generic.List<EvryCardManagement.CustomerUpdate.edbService>'

首先,我应该如何从通用方法返回列表,如何调用它来返回填充了配置数据的列表?

1 个答案:

答案 0 :(得分:3)

听起来你在两个名称空间中定义了你的类edbService,

EvryCardManagement.Common和 EvryCardManagement.CustomerUpdate

我建议只在EvryCardManagement.Common中定义它,并从那里引用它。