我要訪問外部類的父類的方法(Web服務是在子類實例化)的方法(網絡服務)本地接口,可外接SOAP的Web服務(C#)
的情況是這樣的: 我有一個名爲ConvertCurrency的母類,它接收美元並給出盧比。例如, int ConvertCurrency(int dollars);
現在市場上有一個標準的供應商,它提供了一個軟件,任何人都可以在他的服務器上託管並創建一個web服務。因此,可以說它在兩個地方託管:www.link-A.com/service.asmx和www.link-B.com/service.asmx。
我有一個父類:CurrencyConvertor,它有兩個子類:Convertor-A和Convertor-B。
Class CurrencyConvertor
{
protected Object Service;
public Convert(int dollars)
{
Service.ConvertCurrency(dollars);
}
}
Class Convertor-A : CurrencyConvertor
{
public Convertor-A()
{
Service = link-A.service;
}
}
Class Convertor-B : CurrencyConvertor
{
public Convertor-B()
{
Service = link-B.service;
}
}
// based on current response-time of the two servers, we decide
// that B is faster and hence we make the decision at run-time that we should use
// B's service
CurrencyConvertor cc = new Convertor-B();
cc.Convert(5);
但是,這不能編譯,因爲父類不知道所有的方法服務有它提供了以下錯誤:
「對象」不包含一個定義爲「GetSources」和沒有擴展方法'GetSources'接受類型'對象'的第一個參數可以找到(你是否缺少使用指令或程序集引用?)
我想知道什麼可以是這個問題的可能解決方案。當然,不需要爲兩個提供者中的每一個創建單獨的轉換函數。
如果我能以某種方式使「服務」對象知道它的接口(即函數簽名),它將工作得很好。
有沒有辦法解決這個問題?
做轉換器A和B CurrencyConverter繼承? – Stephan
是@Stephan。感謝您指出:)有更新代碼反映相同。 – texens