This problem was originally found in Community For more information, see.
Public class testclass: itestinterface
{
Private string name;
Public string name
{
Get {return name ;}
Set {name = value ;}
}
}
Public interface itestinterface
{
String name {Get; set ;}
}
Why can't I write it like this?
Ilist <itestinterface> List = (ilist <itestinterface>) new list <testclass> ();
Assert. istrue (list! = NULL );
I want to use different types of set types that implement the same interface. These types do not implement the public base classes of this interface.
I have thought of a solution. Let's take a look. The idea is similar to that of klesh Wong.
List <classa> lista = new list <classa> ();
For (INT I = 0; I <10; I ++)
{
Classa A = new classa ();
Lista. Add ();
}
List <classb> listb = new list <classb> ();
For (int K = 0; k <10; k ++)
{
Classb B = new classb ();
Listb. Add (B );
}
List <imyinterface> listinterfacea = lista. convertall (New
Converter <classa, imyinterface> (contermygenericclassa ));
Assert. istrue (listinterfacea. Count> 5 );
List <imyinterface> listinterfaceb = listb. convertall (New
Converter <classb, imyinterface> (contermygenericclassb ));
Assert. istrue (listinterfaceb. Count> 5 );
}
Public static imyinterface contermygenericclassa (classa OBJ)
{
Return (imyinterface) OBJ;
}
Public static imyinterface contermygenericclassb (classb OBJ)
{
Return (imyinterface) OBJ;
}
Is
What is this requirement? In fact, we usually make some controls to expose some data source attributes, such as ilist <classa>,
The data here is sometimes retrieved from the view,
If you map a view to an object, you cannot directly give the source to the control. Of course, this is not the case. For example, if you do not use a wildcard, you can get an arraylist, however, they are not pursuing perfection.
Well ~~ In this case, we naturally want to abstract an interface so that the data source of this control does not depend on a specific type. This saves us a lot of trouble when accessing data, especially when using the ORM Component.
Hou.
Let's get another generic and anonymous proxy version ;)
List <imyinterface> liste
= Listb. convertall <imyinterface> (New converter <classb,
Imyinterface> (delegate (classb B) {return (imyinterface) B ;}));
Assert. istrue (liste. Count> 0 );
This should be the case for more reliable writing: Ilist <itestinterface> List = new list <itestinterface> (); During type conversion, the interface conversion only applies to the types that implement the interface. The list on the Right of (ilist <itestinterface>) is only [Serializableattribute] Public class list <t>: ilist <t>, icollection <t>, Ienumerable <t>, ilist, icollection, ienumerable Here, t is strongly typed. At this time, T has been specified as testclass during compilation, so there is no type conversion process here. Therefore, at most (ilist <testclass>) new list <testclass> (); Instead of (ilist <itestinterface>) new list <testclass> (); |
|
1 hour agoKlesh Wong: Theoretically, Ilist <itestinterface> List = (ilist <itestinterface>) new list <testclass> (); This method should be true, but it cannot be compiled. The requirements of the landlord should also have some rationality. For the time being, they can only come up with the method of rebuilding the list for conversion. Do you know if there are any better solutions? Ilist <itestinterface> List = cast <testclass, itestinterface> (new list <testclass> ()); Response. Write (list! = NULL ); Public static ilist <t> cast <S, T> (ilist <S> E) where S: T { Ilist <t> List = new list <t> (); Foreach (S in E) List. Add (s ); Return list; } |