c# - Generic cast type to primitive -


is there way below? imagine generic result wrapper class. have type , associated error list. when there no result return user use boolean indicate success failure. want create constructor takes in error list, , if list null or count 0, and type bool/boolean want set true....

seemingly simple, amazingly not possible.

public class result<t>{     private t valueobject { get;set;}     private list<error> errors{ get;set;}      public result(t valueobj, list<error> errorlist){         this.valueobject = valueobj;         this.errors = errorlist;      }      public result(list<error> errors)     {         this.valueobject = default(returntype);          if (valueobject boolean)         {            //wont work compile            //(valueobject boolean) = ((errors == null) || errors.count == 0);               //compiles detaches reference               //bool temp = ((bool)(valueobject object)) ;              //temp = ((errors == null) || errors.count == 0);          }         this.errors = errors;     }  } 

}

am missing simple? , in general prefer without reflection.

casting object before cast generic t, should work well:

    if (valueobject boolean)     {          this.valueobject = (t)(object)((errors == null) || errors.count == 0);     } 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -