我们有一个新的会计系统,为外部客户提供Web服务接口.我要访问的一个接口,但没有WSDL所以我创建通过使用HttpWebRequest的请求,它工作正常.
然而,为了简化请求的创建和解析响应,我想创建某种自动化功能.我在XSD文件中有请求和响应结构.有任何想法吗?
请求创建:
public void SendRequest()
{
HttpWebRequest request = CreateWebRequest();
XmlDocument soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
<soap:Body xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
++ structure type inserted here ++
</soap:Body>
</soap:Envelope>");
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
using (WebResponse response = request.GetResponse())
{
using (StreamReader rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
Console.WriteLine(soapResult);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有2个基本接口,IViewBase(所有视图都将实现)和IPresenterBase(所有演示者都将实现):
public interface IViewBase { }
public interface IPresenterBase
{
IViewBase View { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个新的接口ILogPresenter,它派生自IPresenterBase和ILogView派生自IViewBase:
public interface ILogPresenter : IPresenterBase { }
public interface ILogView : IViewBase{ }
Run Code Online (Sandbox Code Playgroud)
当我创建一个实现ILogPresenter的类时,
public class LogPresenter: ILogPresenter
{
public ILogView View { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
'LogPresenter'没有实现接口成员'IPresenterBase.View'.'LogPresenter.View'无法实现'IPresenterBase.View',因为它没有匹配的返回类型'Views.IViewBase'.
我不能将LogPresenter.View的返回类型设置为从IViewBase派生的ILogView?我想用不同的IView实现ILogPresenter,它来自IViewBase.
如果我有对象类型属性或泛型对象的对象,我该如何序列化?
例如.
public class MyClass
{
public Object Item { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
要么
public class MyClass<T>
{
public T Item { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑
我的Generic类现在看起来像这样:
public class MyClass<T>
{
public MySubClass<T> SubClass { get; set; }
}
public class MySubClass<T>
{
public T Item { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
附加问题:如何在运行时将Item的元素名称更改为typeof(T).Name?