reflection - Correctly distinguish between bool? and bool in C# -


i trying find out if variable either simple bool or nullable<bool>.

it seems that

if(val nullable<bool>) 

returns true both bool , nullable<bool> variables and

if(val bool) 

also returns true both bool , nullable<bool>.

basically, interesting in finding out if simple bool variable true or if nullable<bool> variable not null.

what's way this?

here full code:

list<string> values = typeof(instviewmodel).getproperties()                           .where(prop => prop != "subcollection" && prop != "id" && prop != "name" && prop != "level")                           .select(prop => prop.getvalue(ivm, null))                           .where(val => val != null && (val.gettype() != typeof(bool) || (bool)val == true))      //here i'm trying check if val bool , true or if bool? , not null                           .select(val => val.tostring())                           .where(str => str.length > 0)                           .tolist(); 

the instviewmodel object:

 public class instviewmodel     {         public string subcollection { get; set; }         public string id { get; set; }         public string name { get; set; }         public string level { get; set; }         public bool uk { get; set; }         public bool eu { get; set; }         public bool os { get; set; }         public nullable<bool> mobiles { get; set; }         public nullable<bool> landlines { get; set; }         public nullable<bool> uknrs { get; set; }         public nullable<bool> intnrs { get; set; } } 

the point of code here find out if of object's values null (more specifically, find out values not null , save them in list<string>). presents complication in lambda expression, however, when trying distinguish between bool , bool? types in object (second where statement).

additionally, since object contains string types well, trying exclude in first .where statement (which not doing right @ present doesn't seem working). main goal distinguish between bool , bool? types.

code getting class instance values:

// create class instance instviewmodel model = new instviewmodel() {     uk = true,     uknrs = false, };  // check boolean fields false or null bool isallnullorfalse = (from property in typeof(instviewmodel).getproperties()                          let type = property.propertytype                          let isbool = type == typeof(bool)                          isbool || type == typeof(bool?)                          let value = property.getvalue(model)                          select value == null || (isbool && bool.equals(value, false))).all(e => e);  console.writeline("all values null or false = {0}", isallnullorfalse); 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -