未找到声明为公共的方法



在我的普通类中.cs我有以下基于类的列表声明:

public static List<edbService> edb_service;
public 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; }
}

我有一个公共方法可以从在同一类(common.cs)中声明的xml数据文件中填充列表:

#region PublicMethods
public 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;
        }
      }
      edb_service.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 edb_service;
}

但问题是,在我的调用类中,当我尝试从此方法返回列表时,找不到它 - 我收到一个编译错误,指出需要对象引用。

我试图这样称呼它:

Common.edbService edb_service = Common.populateEDBService("CardUpdate.xml");

我收到以下错误:

非静态字段、方法或属性 'EvryCardManagement.Common.populateEDBService(string)' 需要对象引用

我做错了什么?

我想要一个可以从多个类调用的通用方法(这些类在我的表单上的后台工作人员实例化后异步运行)

您可以尝试将方法设置为静态。

public static List<edbService> populateEDBService(string xmlDataFile)
{
   //Your code here
   ....
}

现在,您可以使用common.populateEDBService();从所有其他类调用此方法

您需要创建类static,或者创建一个对象来调用它。

class edbService { }
public static void Main() {
  //this is error
  edbService.populateEDBService("");
  //this is correct
  edbService s = new edbService();
  s.populateEDBService("");
}

示例的最后一行显示了编译器所需的对象引用。此处s变量是对象引用。

您的 XML 中是否有任何缺失值?如果缺少值,则 .Value 属性将不起作用。因此,如果缺少ServiceID,则srv.Element("ServiceID").Value;将导致错误。您可以让它返回缺失值的空字符串,例如,改用 (string)srv.Element("ServiceID");

最新更新