c# - Check the type of a class -


i have following c# classes:

public class reply<t> { }  public class ok<t> : reply<t> { }  public class badrequest<t> : reply<t> { } 

and on method receives reply need check type ok or badrequest or ... like:

public static string evaluate(reply<t> reply) {    switch (typeof(reply)) {     case typeof(ok<t>):       //       break;     // other cases   }  } 

but error

 type or namespace name 'reply' not found (are missing using directive or assembly reference?) 

any idea how test type of reply?

well, typeof() works on types (as in typeof(int)), not variables, need

reply.gettype()  

instead.

but you'll find case expressions require literal values, you'll need convert if-else block:

public static string evaluate<t>(reply<t> reply) {     if(reply.gettype() == typeof(ok<t>)) {         //     }     else {      // other cases       } }   

or

  if(reply ok<t>) {       //   }   else {       // other cases   }   

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -