Benutzer mit den meisten Antworten
Generic List of <T> where T has to implement a specific interface

Frage
-
The topic should say everything. I want a list
class MyList<T> : List<T> { }
where T has to implement a specific interface. Is there a way to solve this in the declaration or do I have to check this in my implementation?
[Edit] Here's an example what I want to do ...
interface INamable { DateTime Timestamp { get; } String Name { get; } } class Project : INamable { // ... } class Category : INamable { // ... } class Trackings { // ... } // I like to ensure that T cannot be anything else than 'INamable'. class NamableList<T> : List<T> { public T Remove( String name ) { INamable itemFound = null; foreach ( INamable item in this ) { if ( item.Name.ToLower( ) == name.ToLower( ) ) { this.Remove( item ); itemFound = item; break; } } return itemFound; } } static class Program { [STAThread] static void Main( ) { NamableList<Project> projects = new NabaleList<Project>( ); NamableList<Category> categories = new NabaleList<Category>( ); // The code as follows shouldn't work, because 'Tracking' isn't an instance of 'INamable'. NambaleList<Tracking> trackings = new NamableList<Tracking>( ); } }
[/Edit]
Thanks alot.
Christian
Christian Ramelow
- Bearbeitet Christian Ramelow Dienstag, 14. August 2012 15:21 Example for better explanation inserted.
Antworten
-
Hallo Christian,
ggfs. hilft dir das hier:
http://stackoverflow.com/questions/5716799/multiple-interfaces-contained-in-one-listt
(Falls ich deine Frage richtig verstanden habe, sollte das das sein, was Du suchst)
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community- Als Antwort markiert Christian Ramelow Dienstag, 14. August 2012 15:51
-
Hallo Christian,
da wir hier in einem deutschen Forum sind in der angepassten Sprache.
Das lässt sich durch eine Einschränkung lösen:
class NamableList<T> : List<T> where T : class, INamable { public T Remove(String name) { int itemIndex = this.FindIndex( item => ((INamable)item).Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (itemIndex >= 0) { var itemFound = this[itemIndex]; this.RemoveAt(itemIndex); return itemFound; } return null; // default(T); } internal static void Test() { var projects = new NamableList<Project>(); var categories = new NamableList<Category>(); // The code as follows shouldn't work, because 'Tracking' isn't an instance of 'INamable'. // var trackings = new NamableList<Trackings>(); } }
Die zusätzliche Einschränkung auf class, damit definitiv ein Referenztyp verwendet wird,
bei default(T) als Standard zurückgeben.Wobei ich mir die Freiheit herausgenommen habe, Remove ein wenig zu optimieren;
durch FindIndex und RemoveAt entfällt der zweite Durchlauf.
List<T> implementiert weitere wie RemoveAll, die man in solchen Fällen gut nutzen kann.Gruß Elmar
- Als Antwort markiert Christian Ramelow Dienstag, 14. August 2012 15:51
Alle Antworten
-
Hallo Christian,
ggfs. hilft dir das hier:
http://stackoverflow.com/questions/5716799/multiple-interfaces-contained-in-one-listt
(Falls ich deine Frage richtig verstanden habe, sollte das das sein, was Du suchst)
Gruß, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community- Als Antwort markiert Christian Ramelow Dienstag, 14. August 2012 15:51
-
Hallo Christian,
da wir hier in einem deutschen Forum sind in der angepassten Sprache.
Das lässt sich durch eine Einschränkung lösen:
class NamableList<T> : List<T> where T : class, INamable { public T Remove(String name) { int itemIndex = this.FindIndex( item => ((INamable)item).Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (itemIndex >= 0) { var itemFound = this[itemIndex]; this.RemoveAt(itemIndex); return itemFound; } return null; // default(T); } internal static void Test() { var projects = new NamableList<Project>(); var categories = new NamableList<Category>(); // The code as follows shouldn't work, because 'Tracking' isn't an instance of 'INamable'. // var trackings = new NamableList<Trackings>(); } }
Die zusätzliche Einschränkung auf class, damit definitiv ein Referenztyp verwendet wird,
bei default(T) als Standard zurückgeben.Wobei ich mir die Freiheit herausgenommen habe, Remove ein wenig zu optimieren;
durch FindIndex und RemoveAt entfällt der zweite Durchlauf.
List<T> implementiert weitere wie RemoveAll, die man in solchen Fällen gut nutzen kann.Gruß Elmar
- Als Antwort markiert Christian Ramelow Dienstag, 14. August 2012 15:51