开发者

Generic type in C#: restricting the type parameter to be a collection

开发者 https://www.devze.com 2022-12-10 13:04 出处:网络
I need to write a generic class where the type parameter must be something i开发者_如何学运维mplementing ICollection<T>. Inside of MyClass I need the collection\'s item type (in the code snippet

I need to write a generic class where the type parameter must be something i开发者_如何学运维mplementing ICollection<T>. Inside of MyClass I need the collection's item type (in the code snippet marked as ???).

class MyClass<TCollection> where TCollection : ICollection<???>
{
  // ...

  public void store(??? obj) { /* put obj into collection */ }

  // ...
}

Often the collection will actually be a dictionary. Sometimes, it will be something simple as a list.

I know exactly how to do this in C++. How would I do this is C#?


Well, you'd normally do this using another type parameter:

class MyClass<TCollection, TElement> where TCollection : ICollection<TElement>
{
  // ...

  public void store(TElement obj) { }

  // ...
}

Do you actually need TCollection in this case? Could you just make do with TElement?


The simplest thing to do is just specify the element type only and hard-code ICollection<T> wherever you need it, e.g.

class MyClass<T> {

    private ICollection<T> _items;

    public MyClass(ICollection<T> items) {
        _items = items;
    }

    public void Store(T obj) {
        _items.Add(obj);
    }

    public ICollection<T> Items {
        get {
            return _items;
        }
    }
}

I recommend that you pass in a collection instance to the constructor rather than create one internally. It makes the class simpler and more "generic" (excuse the pun), and allows you to construct collection instances with non-default constructors, e.g. a dictionary with a non-default comparator.

RE-EDIT (3rd attempt): Using class inheritance and namespace aliasing to simulate typedef are both OK up to a point, but both abstractions break down under certain circumstances. This code is the simplest I have found that actually compiles.

Step 1 - Define these classes:

// This KeyValuePair was being used to simulate a tuple. We don't need to simulate a tuple when we have a concrete class.
class BazAndListOfWrgl {
    Baz Baz { get; set; }
    List<Wrgl> Wrgls { get; set; }
}

// Simple typedef.
class BazAndListOfWrglDictionary : Dictionary<Bar, BazAndListOfWrgl> { }

Step 2 - Define these namespace aliases. All identifiers must be fully qualified. All types referenced must already be defined in a different physical file (since namespace aliases have to come before all code in a file).

using OuterDictionary = System.Collections.Generic.Dictionary<MyNamespace.Foo, MyNamespace.BazAndListOfWrglDictionary>;
using OuterDictionaryItem = System.Collections.Generic.KeyValuePair<MyNamespace.Foo, MyNamespace.BazAndListOfWrglDictionary>;

Step 3 - Use them like this:

class Program {

    static void Main() {

        // List-based example.
        var listWrapper = new MyClass<BazAndListOfWrgl>(new List<BazAndListOfWrgl>());
        listWrapper.Store(new BazAndListOfWrgl());
        Console.WriteLine(listWrapper.Items.Count);

        // Dictionary-based example.
        var dictionaryWrapper = new MyClass<OuterDictionaryItem>(new OuterDictionary());
        dictionaryWrapper.Store(new OuterDictionaryItem(new Foo(), new BazAndListOfWrglDictionary()));
        Console.WriteLine(dictionaryWrapper.Items.Count);

    }
}

The reasoning being: BazAndListOfWrglDictionary cannot be a namespace alias because namespace aliases cannot depend on each other. OuterDictionary and OuterDictionaryItem cannot be derived classes because otherwise the compiler does not recognise one as being the element of the other.


This works for me:

class MyClass<TCollection,T> where TCollection : ICollection<T> , new()
{
    private TCollection collection;           

    public MyClass()
    {
        collection = new TCollection();
    }

    public void Store(T obj)
    {
        collection.Add(obj);
    }

    public TCollection Items
    {
        get { return collection; }
    }
}

Usage:

MyClass<List<string>, string> myclass = new MyClass<List<string>, string>();
myclass.Store("First element");
myclass.Store("Second element");
myclass.Items.ForEach(s => Console.WriteLine(s));

EDIT: When T is getting more complicated, you might want to take a look at the using directive, you can (mis)use it as a typedef.

using HugeType = System.Collections.Generic.Dictionary<int, string>; // Example 'typedef'
...

//Create class, note the 'typedef' HugeType
MyClass<List<HugeType>, HugeType> myclass = new MyClass<List<HugeType>, HugeType>();
//Fill it
HugeType hugeType1 = new HugeType();
hugeType1.Add(1, "First");
hugeType1.Add(2, "Second");
HugeType hugeType2 = new HugeType();
hugeType1.Add(3, "Third");
hugeType1.Add(4, "Fourth");
myclass.Store(hugeType1);
myclass.Store(hugeType2);
//Show it's values.
myclass.Items.ForEach(element => element.Values.ToList().ForEach(val => Console.WriteLine(val)));


Can't you just do this?:

class MyClass<T, TCollection>  where T: YourTypeOrInterface
                               where TCollection : ICollection<T>
{
    public void store(T obj) { }
}

And, not tested, but the only other way I could think to do it would be to specify the type when storing the item:

class MyClass<TCollection> where TCollection : System.Collections.ICollection
{

    TCollection Collection;

    public void Store<T>(T obj) 
    {
        ((ICollection<T>)this.Collection).Add(obj);
    }

}


Are there any public members which expose the type TCollection?

If there are any such members, do you gain any value exposing the direct type TCollection where you could expose ICollection<T>?

Your clients are expected to implement the ICollection<T> interface, so it doesn't really affect your object; you will only ever be making calls to that interface, not their specific type. As such, your object only needs to know the type of element being stored in the collection.

Under this premise, the class can be written like this:

public class MyClass<T>
{
    public MyClass(ICollection<T> collection) 
    { 
        /* store reference to collection */ 
    }

    // ...

    public void store(T obj) 
    { 
        /* put obj into collection */ 
    }

    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号