PropertyInfo.GetValue 布尔值始终为真

PropertyInfo.GetValue on Boolean is always True

我正在对 SharePoint 字段进行交互以检查隐藏的 属性 (Sytem.Boolean),以便可以切换它。我注意到 GetValue(f,null) 始终为 True,即使我知道字段 Hidden 属性 为 False 也是如此。我不明白为什么它一直返回 true。谢谢

var list = ctx.Web.Lists.GetById(libGuid);
var fields = list.Fields;
ctx.Load(list);
ctx.Load(fields);
ctx.ExecuteQuery();

List<object> fieldPropList = new List<object>();

foreach (Field f in fields)
 {
    List<PropertyInfo> props = f.GetType().GetProperties().ToList();
    foreach (var prop in props)
    {
        if (prop.Name == "Hidden")
        {
            fieldPropList.Add(new
            {
                PropertyName = prop.Name,
                PropertyType = prop.PropertyType.ToString(),
                CanRead = prop.CanRead,
                CanWrite = prop.CanWrite,
                Value = prop.GetValue(f, null).ToString()  // Always TRUE why?
            });
        }
    }

很确定您还需要加载字段...

var list = ctx.Web.Lists.GetById(libGuid);
var fields = list.Fields;
ctx.Load(list);
ctx.Load(fields);
ctx.ExecuteQuery();

List<object> fieldPropList = new List<object>();

foreach (Field f in fields)
 {
    ctx.Load(f);   //  <<== *** LOAD THE FIELD ***
    List<PropertyInfo> props = f.GetType().GetProperties().ToList();
    foreach (var prop in props)
    {
    if (prop.Name == "Hidden")
    {
        fieldPropList.Add(new
        {
        PropertyName = prop.Name,
        PropertyType = prop.PropertyType.ToString(),
        CanRead = prop.CanRead,
        CanWrite = prop.CanWrite,
        Value = prop.GetValue(f, null).ToString()  // Always TRUE why?
        });
    }
    }

这是一个示例,如果您要根据您的请求

使用 Class 下面的代码
public static void ConvertNullToStringEmpty<T>(this T clsObject) where T : class
{
    PropertyInfo[] properties = clsObject.GetType().GetProperties();
    foreach (var info in properties)
    {
        // if a string and null, set to String.Empty
        if (info.PropertyType == typeof(string) && info.GetValue(clsObject, null) == null)
        {
            info.SetValue(clsObject, String.Empty, null);
        }
    }
}