对C#对象数组和隐式类型转换感到困惑 [英] Confused on C# Array of objects and implicit type conversion

查看:53
本文介绍了对C#对象数组和隐式类型转换感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个简单对象的数组传递给Web服务,并且在编译我的Web客户端项目的过程中确实卡在此错误上:

I am trying to pass an array of a simple object to a web service and I'm really stuck on this error during compile of my web client project:

无法将类型'TRIMBrokerUtil.MetaData []'隐式转换为'TRIMBrokerASMXProxy.ASMXProxy.MetaData []'

Cannot implicitly convert type 'TRIMBrokerUtil.MetaData[]' to 'TRIMBrokerASMXProxy.ASMXProxy.MetaData[]'

这是我的实用程序项目,已编译为TRIMBrokerUtil.dll:

Here is my "utility" project compiled into 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(Namespace = http://tempuri.org/ )]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
公共类FileService:System.Web.Services.WebService
{

Here is a snippet of the web service which also compiles fine: using TRIMBrokerUtil; namespace TRIMBrokerService { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class FileService : System.Web.Services.WebService {

    [WebMethod]
    public string UploadFile(byte[] incomingArray
        , string FileName
        , long FileLengthInBytes
        , MetaData[] metaDataArray)
    {

。以及以后的用法如下:

...and usage later as this:

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文件中,我发现这似乎不错:

Next, (via "Add Web Reference") I have my proxy class for the ASMX webservice in a separate project and it compiles without problem. Inside the generated reference.cs file I find this which seems OK:

    [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客户端项目中的编译错误(默认值。 aspx.cs):

Now for the error that occurs in compilation in the web client project (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 );

我还尝试将上面的最后两行更改为以下一行:

I have also tried changing the last 2 lines above to simply this one line:

strURI = client.UploadFile(binData, fileNameOnly, binData.Length, metaDataArray);

...但是该更改引入了编译器的第二个错误,内容为:

...but that change introduces a 2nd error from the compiler which reads as:

'TRIMBrokerASMXProxy.ASMXProxy.FileService.UploadFile(byte [],string,long,TRIMBrokerASMXProxy.ASMXProxy.MetaData [])的最佳重载方法匹配具有一些无效参数

The best overloaded method match for 'TRIMBrokerASMXProxy.ASMXProxy.FileService.UploadFile(byte[], string, long, TRIMBrokerASMXProxy.ASMXProxy.MetaData[])' has some invalid arguments

(请注意,有关无法转换的原始错误是第二个错误)。

(note the original error about "cannot convert" is the 2nd error).

很抱歉上面这么冗长。希望您能帮助您弄清这种混乱。

Sorry about being so verbose above. Hope you can help shed light on this confusion.

推荐答案

您正试图将TRIMBrokerUtil.MetaData数组分配给TRIMBrokerASMXProxy.ASMXProxy.MetaData。请记住,asp.net代理声明了自己的类型。

You are trying to assign an array of TRIMBrokerUtil.MetaData to an array of TRIMBrokerASMXProxy.ASMXProxy.MetaData. Remember that the asp.net proxy declares its own type.

只需将数据复制到具有代理类型的新数组中即可。

Just copy the data into a new array with the proxy type.

这篇关于对C#对象数组和隐式类型转换感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆