Aloha
I have a method with (pseudo) signature:
public static T Parse<T>(string datadictionary) where T : List<U>
This doesn’t build. How can I restrict the in the method to accept only generic List<> objects (which should of cource not contain T’s but something else 🙂
I need to restrict the type of T because I need to call a method on it in this code. The type passed in is a custom collection (based on List).
public class MyCollection<T> : List<T> where T: MyClass, new() { public void Foo(); } public static T Parse<T>(string datadictionary) where T : MyCollection<U> { T.Foo(); }
-Edoode
Well, you can have two type parameters:
That way you’ll also actually know what U is (in a compile-time manner)…
EDIT: Alternatively (and better), just specify the element type and change the return type:
e.g.
Note that you may wish to change from
List<T>toIList<T>or even a broader interface.