未找到声明为 public 的方法

method declared as public not found

在我的 common.cs class 中,我有以下基于 class 的列表声明:

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

我有一个 public 方法从 xml 数据文件中填充列表,在同一个 class (common.cs):

中这样声明
#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;
}

但问题是,在我调用 class 时,当我尝试从该方法返回一个列表时,找不到它 - 我得到一个编译错误,需要一个对象引用。

我试着这样称呼它:

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

我收到以下错误:

An object reference is required for the non-static field, method, or property 'EvryCardManagement.Common.populateEDBService(string)'

我做错了什么?

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

您需要创建 class 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; 将导致错误。您可以将其设为 return 缺失值的空字符串,例如,改为使用 (string)srv.Element("ServiceID");

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

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

现在您可以使用 common.populateEDBService();

从所有其他 类 调用此方法