Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 然而,当试图区分布尔和布尔的时候,是什么样的错误呢?在lambda表达式中键入我的对象。可以使用typeof(InstViewModel),但它是一个类定义,而不是一个对象(类实例)。和您的val如果类型为PropertyInfo,而不是bool或bool_C#_Reflection - Fatal编程技术网

C# 然而,当试图区分布尔和布尔的时候,是什么样的错误呢?在lambda表达式中键入我的对象。可以使用typeof(InstViewModel),但它是一个类定义,而不是一个对象(类实例)。和您的val如果类型为PropertyInfo,而不是bool或bool

C# 然而,当试图区分布尔和布尔的时候,是什么样的错误呢?在lambda表达式中键入我的对象。可以使用typeof(InstViewModel),但它是一个类定义,而不是一个对象(类实例)。和您的val如果类型为PropertyInfo,而不是bool或bool,c#,reflection,C#,Reflection,然而,当试图区分布尔和布尔的时候,是什么样的错误呢?在lambda表达式中键入我的对象。可以使用typeof(InstViewModel),但它是一个类定义,而不是一个对象(类实例)。和您的val如果类型为PropertyInfo,而不是bool或bool?。我已经编辑了我的问题,希望它能提供更多解释。可能我最初的方法是错误的,但希望进一步的信息能帮助您理解我的意图。@sparta223,尝试更新的代码,您是在寻找它吗?几乎,您能更新代码以便将结果保存在列表中吗?这样我就可以检查列表是否为空,也


然而,当试图区分布尔和布尔的时候,是什么样的错误呢?在lambda表达式中键入我的对象。可以使用
typeof(InstViewModel)
,但它是一个类定义,而不是一个对象(类实例)。和您的
val
如果类型为
PropertyInfo
,而不是
bool
bool?
。我已经编辑了我的问题,希望它能提供更多解释。可能我最初的方法是错误的,但希望进一步的信息能帮助您理解我的意图。@sparta223,尝试更新的代码,您是在寻找它吗?几乎,您能更新代码以便将结果保存在列表中吗?这样我就可以检查列表是否为空,也就是说所有的bool?属性为null,所有bool属性均为false。@sparta223您在属性或值上尝试的
PropertyType
是什么?你需要在
Select(prop=>prop.GetValue(ivm,null))
之前完成它。我怀疑底部部分应该是
prop=>prop.PropertyType==typeof(bool)
等。
prop.GetType()
将只是
typeof(PropertyInfo)
或某个实现子类…@JonHanna是的,我把它放在Select之前,但是我仍然没有看到getPropertyType()。我看到你编辑了你的问题,现在它显示GetType()?它是由其他人编辑的,他们用一个更严重的错误替换了我的错误。它应该是
PropertyType
(这是
PropertyInfo
的一个属性,不是一个方法,我的错误是基于内存,
PropertyInfo
有很多以
Get
开始的方法,但是
PropertyType
是一个更正常的属性)。不过,你的答案也很有效,有人在你之前发布了相同的答案,所以我接受了他们的答案。不过,我还是赞成你的答案。
if(val is Nullable<bool>)
if(val is 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();
 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; }
}
bool? value = true;
object value2 = value; 
private static bool IsNullable<T>(T val)
{
    return false;
}

private static bool IsNullable<T>(T? val)
    where T : struct
{
    return true;
}
bool? val = false;

if (IsNullable(val))
{
    ...
}
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();
// 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);
typeof(InstViewModel).GetProperties()
  .Select(prop => prop.GetValue(ivm, null))
isNullableProperty = property.PropertyType.IsGenericType
  && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
typeof(InstViewModel).GetProperties()
  .Where(
     prop => prop.PropertyType == typeof(bool)
     || prop.PropertyType == typeof(bool?))
((bool?)val).HasValue
!((bool?)val).HasValue
 if(boolVariable){
      //bool variable, not nullable
 }
 if(nullableVariable.HasValue){
     //a nullable variable is not null
 }
if(variable??false){
   //then I'm sure that this is not null and has value=true
}
     if(variables!=null &&variables!=false){/*This may give a warning message but it works*/}
   if(((bool?)variable)??false){
      /*variable is not null and is true*/
    }
List<string> values = typeof(InstViewModel).GetProperties()
    .Select(prop => new { N = prop.Name, T = prop.PropertyType, V = prop.GetValue(ivm, null) })
    .Where(prop => prop.N != "SubCollection" && prop.N != "ID" && prop.N != "Name" && prop.N != "Level")
    .Where(val => (val.V != null && val.T.IsAssignableFrom(typeof(Nullable<bool>))) || Convert.ToBoolean(val.V))                    
    .Select(val => val.V.ToString())
    .Where(str => str.Length > 0)
    .ToList();
var nullableBooleanProperties = typeof(InstViewModel).Where(prop => prop.PropertyType == typeof(bool?));
var values = nullableBooleanProperties.Select(prop => prop.GetValue(ivm)).Where(val => val != null).Select(val => val.ToString());
var values = typeof(InstViewModel).Where(prop => prop.PropertyType == typeof(bool?))
    .Select(prop => prop.GetValue(ivm)).Where(val => val != null)
    .Select(val => val.ToString())
    .ToList();