简体   繁体   English

对 C# 对象数组和隐式类型转换感到困惑

[英]Confused on C# Array of objects and implicit type conversion

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:我正在尝试将一个简单的 object 数组传递给 web 服务,并且在编译我的 web 客户端项目期间我真的遇到了这个错误:

Cannot implicitly convert type 'TRIMBrokerUtil.MetaData[]' to 'TRIMBrokerASMXProxy.ASMXProxy.MetaData[]'无法将类型“TRIMBrokerUtil.MetaData[]”隐式转换为“TRIMBrokerASMXProxy.ASMXProxy.MetaData[]”

Here is my "utility" project compiled into TRIMBrokerUtil.dll:这是我编译成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;
        }
    }
    }

Here is a snippet of the web service which also compiles fine: using TRIMBrokerUtil;这是 web 服务的片段,它也可以很好地编译: namespace TRIMBrokerService { [WebService(Namespace = " http://tempuri.org/ ")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class FileService: System.Web.Services.WebService { 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);

...and this: ...和这个:

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;
}

Next, (via "Add Web Reference") I have my proxy class for the ASMX webservice in a separate project and it compiles without problem.接下来,(通过“添加 Web 参考”)我有我的代理 class 在一个单独的项目中,它编译没有问题。 Inside the generated reference.cs file I find this which seems OK:在生成的 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]));
    }

Now for the error that occurs in compilation in the web client project (default.aspx.cs):现在针对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 );

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: ...但是该更改引入了编译器的第二个错误,内容如下:

The best overloaded method match for 'TRIMBrokerASMXProxy.ASMXProxy.FileService.UploadFile(byte[], string, long, TRIMBrokerASMXProxy.ASMXProxy.MetaData[])' has some invalid arguments 'TRIMBrokerASMXProxy.ASMXProxy.FileService.UploadFile(byte[], string, long, TRIMBrokerASMXProxy.ASMXProxy.MetaData[])' 的最佳重载方法匹配有一些无效的 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.希望您能帮助阐明这种困惑。

You are trying to assign an array of TRIMBrokerUtil.MetaData to an array of TRIMBrokerASMXProxy.ASMXProxy.MetaData.您正在尝试将 TRIMBrokerUtil.MetaData 数组分配给 TRIMBrokerASMXProxy.ASMXProxy.MetaData 数组。 Remember that the asp.net proxy declares its own type.请记住,asp.net 代理声明了自己的类型。

Just copy the data into a new array with the proxy type.只需将数据复制到具有代理类型的新数组中即可。

When you add a reference to the Web Service, Visual Studio will automatically generate code for the objects that are used as parameters to the functions of the web service.当您添加对 Web 服务的引用时,Visual Studio 将自动为用作 web 服务函数参数的对象生成代码。 This is where your TRIMBrokerASMXProxy.ASMXProxy.MetaData class comes from.这就是您的TRIMBrokerASMXProxy.ASMXProxy.MetaData class 的来源。

This is not the same as your TRIMBrokerUtil.MetaData class.这与您的TRIMBrokerUtil.MetaData class 不同。 You can remove the class from your TRIMBrokerUtil namespace and just use the one from the web service proxy instead.您可以从您的TRIMBrokerUtil命名空间中删除 class ,而只使用 web 服务代理中的那个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM