有沒有一種自動處理數組和強類型集合之間轉換的好方法? (Is there a good way to handle conversions between arrays and strongly typed collections automatically?)


問題描述

有沒有一種自動處理數組和強類型集合之間轉換的好方法? (Is there a good way to handle conversions between arrays and strongly typed collections automatically?)

Background:

I have a big solution where hundreds of functions take strongly typed collections as inparameters and using them as return values.

The solution references a generated proxy wich converts calls to a webservice that always returns collection in the format int[] or Order[] or wathever type it is. The proxy wraps them up as IntCollection or OrderCollection.

Now I want to reference the assembly directly. When I do that I get the interface against the Arrays instead of the proxy generated strongly typed collections. This of course breaks all the code.

I am looking for a smart way to handle this and avoid rewriting thousands lines of code.

Any ideas?

‑‑‑‑‑

參考解法

方法 1:

Does IntCollection etc belong to you? You could add an implicit conversion operator:

class IntCollection : Collection<int> {
    public IntCollection() : base() { }
    public IntCollection(IList<int> data) : base(data) { }
    public static implicit operator int[](IntCollection items) {
        return items.ToArray(); // LINQ, but can do manually
    }
    public static implicit operator IntCollection (int[] items){
         return new IntCollection(items);
    }
}

Normally, this would be one of the times that interfaces (IList<Foo> etc) help ‑ but interfaces don't often work very well in web services. Depending on what the code currently does, you might be able to do a "replace all" fix ‑ a bit drastic, though; other than that... you're going to have to change odd bits of code.

a few thoughts:

  • switching to var (in C# 3.0) might minimize the change ‑ i.e. var orders = svc.GetOrders(); in most cases, the different implementations will have "similar enough" APIs
    • resharper might be able to help with this
  • using LINQ (.ToList().ToArray()) might serve as a shim

方法 2:

Create the strongly typed collection from the array. For example you can create a generic list from an array.

int[] oIntArray;
List<int> = new List<int>(oIntArray);

(by JonMarc Gravellstevehipwell)

參考文件

  1. Is there a good way to handle conversions between arrays and strongly typed collections automatically? (CC BY‑SA 3.0/4.0)

#collections #C#






相關問題

C ++中集合/容器的接口/超類 (Interface/Superclass for Collections/Containers in c++)

如何將對象列表轉換為兩級字典? (How to convert a list of objects to two level dictionary?)

如何在java中限制哈希集的默認排序 (how to restrict default ordering of Hash Set in java)

將兩個單獨的 Set 轉換為二維數組 (Convert two separate Sets to a 2D array)

事件調度線程從列表異常 SWING 中移除 (Event dispatching thread remove from List exception SWING)

將集合項複製到 .NET 中的另一個集合 (Copy collection items to another collection in .NET)

java - 如何在java中使用某些特定索引將值存儲到arraylist中 (how to store value into arraylist with some specific index in java)

將對象添加到具有主幹的第一個位置的集合中? (Adding object to a Collection in the first position with backbone?)

在VB中過濾一個wpf collectionviewsource? (Filter a wpf collectionviewsource in VB?)

將流式數據讀入排序列表 (Reading streamed data into a sorted list)

有沒有一種自動處理數組和強類型集合之間轉換的好方法? (Is there a good way to handle conversions between arrays and strongly typed collections automatically?)

將數據行轉換為 1 個對象 (Convert data rows to 1 object)







留言討論