c#错误(使用接口方法):非静态字段、方法或属性需要对象引用

本文关键字:方法 字段 属性 对象引用 静态 错误 接口 | 更新日期: 2023-09-27 18:06:26

我在使用具有过时文档的第三方API时遇到了麻烦,所以我试图弄清楚为什么这段@#$!不是工作。@#$!我的意思是"代码",当然:)

因此,据我所知,WAPISoap是我通过在visual studio中添加web引用而获得的公共接口。

我还知道description()方法接受两个参数,一个字符串和一个凭据类型的对象,它返回一个字符串。任何帮助都将是非常感激的:)

这是我到目前为止得到的:

using WAPIClient;
using System;
using Project1.WsWWDAPI;
namespace WAPIClient
{
    class ResellerAPI
    {
        public void CallDescribe()
        {
            String sReturnXml;
            Credential m_Crededential = new Project1.WsWWDAPI.Credential();
            m_Crededential.Account = "account";
            m_Crededential.Password = "password";
            String sCLTRID = System.Guid.NewGuid().ToString();
            sReturnXml = WAPISoap.Describe(sCLTRID, m_Crededential);
            Console.WriteLine(sReturnXml);
        }
        static void Main(string[] args)
        {
            ResellerAPI reseller = new ResellerAPI();
            reseller.CallDescribe();
        }
    }
}

c#错误(使用接口方法):非静态字段、方法或属性需要对象引用

Describe方法不是静态的,这意味着您需要在WAPI类的实例上调用它:

WsWWDAPI.WAPI m_WAPIObj = null;
WsWWDAPI.Credential m_Crededential = null;
public void Init()
{
    m_WAPIObj = new WsWWDAPI.WAPI();
    m_Crededential = new WsWWDAPI.Credential();
    m_Crededential.Account  = "account";
    m_Crededential.Password = "password";
}
public void CallDescribe()
{
    String sReturnXml;
    String sCLTRID = System.Guid.NewGuid().ToString();
    sReturnXml = m_WAPIObj.Describe(sCLTRID, m_Crededential);
    Console.WriteLine( sReturnXml );
}
static void Main(string[] args)
{
    ResellerAPI reseller = new ResellerAPI();
    reseller.Init();
    reseller.CallDescribe();
}

见:http://products.secureserver.net/guides/wsapiquickstart.pdf

这个错误是因为您在静态上下文中使用了非静态方法—您应该有WAPISoap的实例,以便调用非静态的成员函数

听起来您需要创建一个WAPISoap实例,然后在该实例上调用description