我有一个类提供对 Web 服务的动态调用。
private System.Xml.XmlTextReader _XmlReader;
private System.Web.Services.Description.ServiceDescription _ServiceDescription;
private System.Web.Services.Description.ServiceDescriptionImporter _DescriptionImporter;
private System.CodeDom.CodeNamespace _CodeNamespace;
private System.CodeDom.CodeCompileUnit _CodeUnit;
private System.Web.Services.Description.ServiceDescriptionImportWarnings _ImportWarnings;
private System.CodeDom.Compiler.CodeDomProvider _Compiler;
private System.CodeDom.Compiler.CompilerParameters _Parameters;
private System.CodeDom.Compiler.CompilerResults _Results;
private System.Reflection.Assembly _WebServiceAssembly;
private string _Url;
public WebServiceInvoker(string url)
{
_Url = url;
System.Net.ServicePoint servicePoint = System.Net.ServicePointManager.FindServicePoint(new Uri(_Url));
servicePoint.Expect100Continue = false;
_XmlReader = new System.Xml.XmlTextReader(_Url);
_ServiceDescription = System.Web.Services.Description.ServiceDescription.Read(_XmlReader);
_DescriptionImporter = new System.Web.Services.Description.ServiceDescriptionImporter();
_DescriptionImporter.ProtocolName = "Soap";
_DescriptionImporter.AddServiceDescription(_ServiceDescription, null, null);
_DescriptionImporter.Style = System.Web.Services.Description.ServiceDescriptionImportStyle.Client;
_DescriptionImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
_CodeNamespace = new System.CodeDom.CodeNamespace();
_CodeUnit = new System.CodeDom.CodeCompileUnit();
_CodeUnit.Namespaces.Add(_CodeNamespace);
_ImportWarnings = _DescriptionImporter.Import(_CodeNamespace, _CodeUnit);
if (_ImportWarnings != 0) throw new Exception("Can not resolve registration service.");
_Compiler = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("CSharp");
string[] references = new string[] { "System.Web.Services.dll", "System.Xml.dll" };
_Parameters = new System.CodeDom.Compiler.CompilerParameters(references);
_Results = _Compiler.CompileAssemblyFromDom(_Parameters, _CodeUnit);
foreach (System.CodeDom.Compiler.CompilerError oops in _Results.Errors) { throw new Exception("Compile Error Creating Assembly"); }
_WebServiceAssembly = _Results.CompiledAssembly;
}
public LYSProducts.ProductObject GetProduct(string productCode)
{
LYSProducts.ProductObject p;
object obj = _WebServiceAssembly.CreateInstance("RegisteryService");
Type typ = obj.GetType();
p = (LYSProducts.ProductObject)typ.InvokeMember("GetProduct", System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] { productCode });
return p;
}
GetProduct 方法返回一个自定义类。
namespace LYSProducts
{
[Serializable()]
class ProductObject
{
public string ProductCode
{
get { return _ProductCode; }
set { _ProductCode = value; }
}
public string ProductName
{
get { return _ProductName; }
set { _ProductName = value; }
}
public string MajorVer
{
get { return _MajorVer; }
set { _MajorVer = value; }
}
public string MinorVer
{
get { return _MinorVer; }
set { _MinorVer = value; }
}
private string _ProductCode;
private string _ProductName;
private string _MajorVer;
private string _MinorVer;
public ProductObject(string productCode, string productName)
{
_ProductCode = productCode;
_ProductName = productName;
_MajorVer = "";
_MinorVer = "";
}
public ProductObject(string productCode, string productName, string majorVer, string minorVer)
{
_ProductCode = productCode;
_ProductName = productName;
_MajorVer = majorVer;
_MinorVer = minorVer;
}
}
}
代码工作正常。但是“typ.InvokeMember”方法返回一个对象,我不知道如何将其转换为 LYSProducts.ProdutcObject 类。我在客户端部署了相同的类,但是当我尝试强制转换时,我收到“无法将'ProductObject'类型的对象转换为'LYSProducts.ProductObject'”错误。我知道该服务必须为“ProductObject”类型提供一些架构,我可以使用反射从该信息创建一个 ProductObject,但我不知道该怎么做。