本文介绍了我可以停止我的 WCF 生成 ArrayOfString 而不是 string[] 或 List<string> 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 WCF 服务代理时遇到了一个小问题,其中消息包含 List 作为参数.

I am having a minor problem with WCF service proxies where the message contains List<string> as a parameter.

我在 Visual Studio 中使用添加服务引用"来生成对我的服务的引用.

I am using the 'Add Service reference' in Visual Studio to generate a reference to my service.

   // portion of my web service message
   public List<SubscribeInfo> Subscribe { get; set; }
   public List<string> Unsubscribe { get; set; }

这些是在我的 MsgIn 上为我的一种网络方法生成的属性.当我使用 List 时,您可以看到它使用了 ArrayOfString,而另一个使用 List - 与我原来的 C# 匹配上面的对象.

These are the generated properties on my MsgIn for one of my web methods.You can see it used ArrayOfString when I am using List<string>, and the other takes List<SubscribeInfo> - which matches my original C# object above.

[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
public System.Collections.Generic.List<DataAccess.MailingListWSReference.SubscribeInfo> Subscribe {
    get {
        return this.SubscribeField;
    }
    set {
        if ((object.ReferenceEquals(this.SubscribeField, value) != true)) {
            this.SubscribeField = value;
            this.RaisePropertyChanged("Subscribe");
        }
    }
}

[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false)]
publicDataAccess.MailingListWSReference.ArrayOfString Unsubscribe {
    get {
        return this.UnsubscribeField;
    }
    set {
        if ((object.ReferenceEquals(this.UnsubscribeField, value) != true)) {
            this.UnsubscribeField = value;
            this.RaisePropertyChanged("Unsubscribe");
        }
    }
}

生成的 ArrayOfString 类如下所示.这是在我的代码中生成的类 - 它不是 .NET 类.它实际上为我生成了一个继承自 List 的类,但没有为我创建任何构造函数的体面".

The ArrayOfString class generated looks like this. This is a class generated in my code - its not a .NET class. It actually generated me a class that inherits from List, but didn't have the 'decency' to create me any constructors.

    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfString", Namespace="http://www.example.com/", ItemName="string")]
    [System.SerializableAttribute()]
    public class ArrayOfString : System.Collections.Generic.List<string> {
    }

问题是我经常像这样创建我的消息:

The problem is that I often create my message like this :

client.UpdateMailingList(new UpdateMailingListMsgIn()
{
    Email = model.Email,
    Name = model.Name,
    Source = Request.Url.ToString(),
    Subscribe = subscribeTo.ToList(),
    Unsubscribe = unsubscribeFrom.ToList()
});

我真的很喜欢这给我带来的干净外观.

I really like the clean look this gives me.

现在解决实际问题:

我不能将 List 分配给 Unsubscribe 属性,它是一个 ArrayOfString - 即使它继承自 List.事实上,如果没有额外的语句,我似乎无法找到任何分配它的方法.

I cant assign a List<string> to the Unsubscribe property which is an ArrayOfString - even though it inherits from List. In fact I cant seem to find ANY way to assign it without extra statements.

我尝试了以下方法:

  • new ArrayOfString(unsubscribeFrom.ToList()) - 这个构造函数不存在:-(
  • 更改代码生成器使用的数组类型 - 不起作用 - 它总是给我 ArrayOfString (!?)
  • 尝试将 List 转换为 ArrayOfString - 失败并显示无法转换",即使它编译得很好
  • create new ArrayOfString() 然后 AddRange(unsubscribeFrom.ToList()) - 有效,但我不能在一个语句中完成所有操作
  • 创建一个转换函数ToArrayOfString(List),它可以工作,但没有我想要的那么干净.
  • new ArrayOfString(unsubscribeFrom.ToList()) - this constructor doesn't exist :-(
  • changing the type of the array used by the code generator - doesn't work - it always gives me ArrayOfString (!?)
  • try to cast List<string> to ArrayOfString - fails with 'unable to cast', even though it compiles just fine
  • create new ArrayOfString() and then AddRange(unsubscribeFrom.ToList()) - works, but I cant do it all in one statement
  • create a conversion function ToArrayOfString(List<string>), which works but isn't as clean as I want.

它只对字符串执行此操作,这很烦人.

Its only doing this for string, which is annoying.

我错过了什么吗?有没有办法告诉它不要生成 ArrayOfString - 或者其他一些分配它的技巧?

Am i missing something? Is there a way to tell it not to generate ArrayOfString - or some other trick to assign it ?

推荐答案

任何实现名为Add"的方法的 .NET 对象都可以像数组或字典一样进行初始化.

Any .NET object that implements a method named "Add" can be initialized just like arrays or dictionaries.

由于 ArrayOfString 确实实现了Add"方法,您可以像这样初始化它:

As ArrayOfString does implement an "Add" method, you can initialize it like this:

var a = new ArrayOfString { "string one", "string two" };

但是,如果你真的想基于另一个集合来初始化它,你可以为此编写一个扩展方法:

But, if you really want to initialize it based on another collection, you can write a extension method for that:

public static class U
{
    public static T To<T>(this IEnumerable<string> strings)
        where T : IList<string>, new()
    {
        var newList = new T();
        foreach (var s in strings)
            newList.Add(s);
        return newList;
    }
}

用法:

client.UpdateMailingList(new UpdateMailingListMsgIn()
{
    Email = model.Email,
    Name = model.Name,
    Source = Request.Url.ToString(),
    Subscribe = subscribeTo.ToList(),
    Unsubscribe = unsubscribeFrom.To<ArrayOfString>()
});

这篇关于我可以停止我的 WCF 生成 ArrayOfString 而不是 string[] 或 List<string> 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 00:14