运算符 '==' 不能应用于操作数 System.Reflection.PropertyInfo 和 &

本文关键字:Reflection PropertyInfo System 不能 运算符 应用于 操作数 | 更新日期: 2023-09-27 18:36:07

这是我卡在 c# 中的一段代码 [编辑]:

foreach (var property in this.allProperties)
        {
            var propertyItself = element.GetType().GetProperty(property.GetType().Name);
            if (propertyItself.PropertyType != typeof(Int32))
            { continue; }
            if (propertyType == 0)
            { return false; }
        }

如果有人对此不满意,可以帮助提供一些信息吗?提前感谢!

运算符 '==' 不能应用于操作数 System.Reflection.PropertyInfo 和 &

在计算属性的值之前,您需要调用 PropertyInfo.GetValue 方法,如下所示

if (propertyItself == typeof (Int32))
{ 
    if((int) propertyItself.GetValue(element) == 0)
    { 
        return false; 
    }
}

您也可以考虑提高表达式的可读性,就像我上面所做的那样,仅通过计算类型等于整数。