c# - Casting down from generic type within function type parameters (error) -


the problem getting that, in below example, can cast mytype<t> imytype, fine. cannot cast func<mytype<t>, bool> func<imytype, bool>.

that doesn't make sense , i'm wondering if there way round through perhaps different architecture?

i not want use reflection.

i understand why failing.

code failing in .net 4.5.2. online version here - http://goo.gl/13z4xg - fails in same way.

using system; using system.collections.generic;  public interface imytype {     string text { get; }     int number { get; } }  public class mytype<t> : imytype t : someothertype {     public mytype()     {         text = "hello";         number = 99;     }      public string text { get; private set; }     public int number { get; private set; } }  public abstract class someothertype {     public int id { get; set; }     public string title { get; set; } }  public class concretetype : someothertype {     public string description { get; set; } }  class program {     static void main(string[] args)     {         var ok = (imytype) new mytype<concretetype>();         console.write("i can cast mytype<t> imytype here");         var list = new list<func<imytype, bool>>();         func<mytype<concretetype>, bool> func = type => false;         // list.add(func); // compiler error         list.add((func<imytype,bool>)func); // runtime cast error         console.write("got here ok"); // (it doesn't here...)     } } 

sorry...i thought various answers explained pretty why you don't want this. though think (a common enough mistake, no reason feel badly it). didn't occur me after reading of questions , answers, wouldn't feel had adequate answer own question. i'm not sure can add them, i'll try…


short version: if allowed add instance of func<mytype<concretetype>, bool> list of func<imytype, bool>, other code pull instance out func<imytype, bool> , try call it, passing delegate instance of imytype other mytype<concretetype>.

but, method (presumably, given delegate declaration) expects instances of mytype<concretetype>! if passed other implementation of imytype?


compiler tried prevent making mistake, when gave compile-time error @ list.add(func). hid error compiler cast, doesn't change fundamental hazard compiler trying save from, , run-time has step in when attempt cast , stop there.

it have been better block code @ compile-time, that's not possible when use cast claim know type compiler doesn't. it's still better run-time error there @ cast, rather later when try pass other implementation of imytype method delegate represents.


now, code example you've given doesn't make clear why think advantageous do. it's impossible know alternative best in case.

if method delegate reference indeed need know type mytype<concretetype>, you're stuck. not safe put delegate referencing method in context imytype passed it. painted corner somehow, , need rethink design, because has fundamental flaw in it.

note, however, if really use imytype-ness of object in method, solution declare way. example, lambda expression show compile fine if assigned instead variable of type func<imytype, bool>, could add list.

alternatively, if 100% certain no code ever attempt invoke delegate implementation of imytype other mytype<concretetype>, wrap wrong delegate type in instance of right delegate type. example:

list.add(o => func((mytype<concretetype>)o)); 

of course, doing remove bulk of type-safety features of using generic list<t> object. has "bad code smell" it. compile, , run long never break rules , try invoke delegate different implementation of imytype.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -