I have several classes that all IWidget & lt; T & gt; Implementing the interface. I would like to add them to the list, so I define something like this:
list & lt; IWidget & lt; T & gt; & Gt; _widgetList; There is no complaint about the compiler T:
Error 1 type or namespace name 'T' could not be found (whether you use the Director Are not the assemblies missing the context?)
Is there a way to define a list of IWidget and still have a generic interface?
I am using .NET 3.5 SP1.
There are a few ways to do this:
1) You need a non-common route:
Public interface IWidget {...} Public Interface IWidget & lt; T & gt; : IWidget {...} So that you list & lt; IWidget & gt; _widgetList;
2) You can use the dynamic s or polymorphism with a coat / co-vibration, but your To play in accordance with the rules of the rules, & lt; T & gt; should not be a value type and you have to enter after entering, but not when it is added, like this:
IWidget & lt; String & gt; Xxx = ...; Var widgetListDyn = New list & lt; IWidget & lt; Dynamic & gt; & Gt; (); Var widgetListObj = new list & lt; IWidget & lt; Object & gt; & Gt; (); WidgetListDyn.Add (xxx); WidgetListObj.Add (xxx);
3) or you can match two methods:
Public Interface: IWidget: IWidget & lt; Object & gt; {} and you list & lt; IWidget & gt; You can use. _Vidit list; with this technique you .add () :
list
4) You can go about this in the final way:
Public class IWidgetList & lt; T & gt; : List & lt; IWidget & lt; T & gt; & Gt; {} Which would be useful, if you plan to declare several lists written in, then you should: list & lt; IWidget & lt; TYPE & gt; & Gt; and instead you can use it: IWidgetList & lt; TYPE & gt; and it's similar
No comments:
Post a Comment