混淆上的对象C#阵列和隐式类型转换
-
21-08-2019 - |
题
我想一个简单的对象数组传递到Web服务,我真的坚持这个错误我的Web客户端项目的编译过程:
无法隐式转换类型 'TRIMBrokerUtil.MetaData []' 到 'TRIMBrokerASMXProxy.ASMXProxy.MetaData []'
下面是我的 “效用” 项目汇编成TRIMBrokerUtil.dll:
namespace TRIMBrokerUtil
{
public class MetaData
{
protected string _Name;
protected string _Value;
public MetaData(string keyword, string setting)
{
this.Name = keyword;
this.Value = setting;
}
public string Name
{
get
{
return this._Name;
}
set
{
Name = value;
}
}
public string Value
{
get
{
return this._Value;
}
set
{
_Value = value;
}
}
}
下面是web服务也编译罚款的一个片段: 使用TRIMBrokerUtil; 命名空间TRIMBrokerService { [WebService的(命名空间= “ http://tempuri.org/ ”)] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [的ToolboxItem(假)] 公共类的FileService:System.Web.Services.WebService {
[WebMethod]
public string UploadFile(byte[] incomingArray
, string FileName
, long FileLengthInBytes
, MetaData[] metaDataArray)
{
...和使用以后作为这样的:
Update update = BuildMetaData(metaDataArray);
...和这样的:
private Update BuildMetaData(MetaData[] nvPairs)
{
Update update = new Update();
InputProperty[] ip = new InputProperty[nvPairs.Length];
int i;
for (i=0; i < nvPairs.Length; i++)
{
ip[i].Name = "udf:" + nvPairs[i].Name;
ip[i].Val = nvPairs[i].Value;
}
update.Items = ip;
return update;
}
接下来,(通过“添加Web引用”),我有我的代理类在一个单独的项目ASMX web服务,它编译没有问题。内部生成的reference.cs文件,我觉得这似乎确定:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UploadFile", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public string UploadFile([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] byte[] incomingArray, string FileName, long FileLengthInBytes, MetaData[] metaDataArray) {
object[] results = this.Invoke("UploadFile", new object[] {
incomingArray,
FileName,
FileLengthInBytes,
metaDataArray});
return ((string)(results[0]));
}
现在对发生在编译在web客户端项目的误差(default.aspx.cs):
using TRIMBrokerUtil;
public partial class _Default : System.Web.UI.Page
{
private void UploadFile(HttpPostedFile postedFile
, string fileNameOnly
, MetaData[] metaDataArray)
{
string strURI = string.Empty;
TRIMBrokerASMXProxy.ASMXProxy.FileService client = new TRIMBrokerASMXProxy.ASMXProxy.FileService();
BinaryReader b = new BinaryReader(postedFile.InputStream);
byte[] binData = b.ReadBytes(numBytes);
TRIMBrokerASMXProxy.ASMXProxy.MetaData[] kvData = metaDataArray; // error complains about this line
strURI = client.UploadFile(binData, fileNameOnly, binData.Length, kvData );
我还试图改变上述简单这一行的最后2行:
strURI = client.UploadFile(binData, fileNameOnly, binData.Length, metaDataArray);
...但是,变化从读取,因为编译器引入了一个第二错误:
关于 'TRIMBrokerASMXProxy.ASMXProxy.FileService.UploadFile(字节[],字符串长,TRIMBrokerASMXProxy.ASMXProxy.MetaData [])' 具有一些无效参数
在最佳重载方法匹配(注意原始误差约“可以不转换”是第二错误)。
对不起如此冗长的上方。希望能帮到你在这混乱线索。
解决方案
您正在努力TRIMBrokerUtil.MetaData的阵列分配给TRIMBrokerASMXProxy.ASMXProxy.MetaData的阵列。请记住,asp.net代理声明了自己的类型。
只需将数据复制到与代理类型的新数组。
其他提示
当您添加到Web服务的引用,Visual Studio将自动生成被用作参数到web服务的功能的对象的代码。这是你的TRIMBrokerASMXProxy.ASMXProxy.MetaData
类是从。
这是不一样的你TRIMBrokerUtil.MetaData
类。您可以从TRIMBrokerUtil
命名空间中删除类,只是使用从不是Web服务代理的一个。