【问题标题】:error when populating list based on class in generic method在泛型方法中基于类填充列表时出错
【发布时间】:2015-01-28 09:22:20
【问题描述】:

我在 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&lt;EvryCardManagement.Common.edbService&gt;' is required

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

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

但我收到一个错误Cannot implicitly convert type 'System.Collections.Generic.List&lt;EvryCardManagement.Common.edbService&gt;' to 'System.Collections.Generic.List&lt;EvryCardManagement.CustomerUpdate.edbService&gt;'

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

【问题讨论】:

    标签: c# .net list class


    【解决方案1】:

    听起来您的类 edbService 定义在两个命名空间中,

    EvryCardManagement.Common 和 EvryCardManagement.CustomerUpdate

    我建议仅在 EvryCardManagement.Common 中定义它,并让所有内容都从那里引用它。

    【讨论】:

    • 谢谢(这行得通!),但是我应该如何编写它以便该方法返回一个填充列表?
    • 您只需要添加一个 List 类型的局部变量的声明。然后在循环中将 edbService 对象添加到此列表中,然后在函数末尾返回该列表。
    • 我尝试edb_service = Common.populateSoapBody&lt;edbService&gt;("CardUpdate.xml"); 调用该过程,但出现错误
    • 什么是populateSoapBody?错误是什么?值得关闭这个线程并开始一个新线程吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    相关资源
    最近更新 更多