c# cannot cast object to interface in a method with multiple generic parameters -


I'm playing with generics and try to put one method in the interface with two common parameters , Which does not work. The code below shows the problem:

  Private void GenericMethod & lt; T, U & gt; () Where T: Block where U: IBlock {var list = new list & lt; T & gt; (); // This Casting works var items = new blocks (); Var iItem = (IBlock) item; Foreach (var l) in the list {// it does not compile var variables = (U) L; }}  

The block here is a square and IBlock has the interface implemented by this class.

Why Casting (U) Fails? How can I do this?

If T is obtained from the block, and from IBBlock, u does not mean it if block with ULblock For example:

  Public category TBlock: Block {Public Zero NewMethod () {}} Public Interface UIBlock: IBlock {void AnotherNewMethod (); }  

To work this example, you must change

  where T: block where U: iBlock  
From P>

  where U: iBlock where T: block, u  

make sure that you have inherited Yu.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -