正确区分布尔?和C#中的bool

spa*_*223 16 c# reflection

我试图找出一个变量是简单的bool还是一个Nullable<bool>.

看起来

if(val is Nullable<bool>)
Run Code Online (Sandbox Code Playgroud)

两个返回true boolNullable<bool>变量

if(val is bool)
Run Code Online (Sandbox Code Playgroud)

也返回了两个真实的boolNullable<bool>.

基本上,我在发现如果一个简单有趣的bool变量是 OR如果一个Nullable<bool>变量是不是空.

这样做的方法是什么?

这是完整的代码:

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 to check if val is bool and true or if bool? and not null
                          .Select(val => val.ToString())
                          .Where(str => str.Length > 0)
                          .ToList();
Run Code Online (Sandbox Code Playgroud)

InstViewModel对象:

 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; }
}
Run Code Online (Sandbox Code Playgroud)

这里我的代码的目的是找出是否所有对象的值null(更具体地说,找出任何非空的值并将它们保存在a中List<string>).然而,这在lambda表达式中提出了一个复杂性,当我试图区分我的对象boolbool?类型时(第二个Where语句).

另外,由于对象也包含一些字符串类型,我试图在我的第一个.Where语句中排除那些(我目前可能做得不好,因为它似乎不起作用).但我的主要目标是区分boolbool?类型.

Dmi*_*try 11

有一个简单的方法来检查一个变量是否被声明为TT?:

private static bool IsNullable<T>(T val)
{
    return false;
}

private static bool IsNullable<T>(T? val)
    where T : struct
{
    return true;
}
Run Code Online (Sandbox Code Playgroud)

用法:

bool? val = false;

if (IsNullable(val))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

编辑
针对编辑过的问题,请尝试以下代码:

var boolProps = typeof (InstViewModel).GetProperties()
    .Where(prop => prop.PropertyType == typeof(bool))
    .Select(prop => (bool)prop.GetValue(ivm, null))
    .Select(v => v ? v.ToString() : String.Empty);

var nullableBoolProps = typeof(InstViewModel).GetProperties()
    .Where(prop => prop.PropertyType == typeof(bool?))
    .Select(prop => (bool?)prop.GetValue(ivm, null))
    .Select(v => v.HasValue ? v.ToString() : String.Empty);

List<string> values = boolProps.Concat(nullableBoolProps)
              .Where(str => str.Length != 0)
              .ToList();
Run Code Online (Sandbox Code Playgroud)


Gen*_*mer 4

获取类实例值的代码:

// create class instance
InstViewModel model = new InstViewModel()
{
    Uk = true,
    UkNrs = false,
};

// check all boolean fields are false or null
bool isAllNullOrFalse = (from property in typeof(InstViewModel).GetProperties()
                         let type = property.PropertyType
                         let isBool = type == typeof(bool)
                         where isBool || type == typeof(bool?)
                         let value = property.GetValue(model)
                         select value == null || (isBool && bool.Equals(value, false))).All(e => e);

Console.WriteLine("All values are null or false = {0}", isAllNullOrFalse);
Run Code Online (Sandbox Code Playgroud)