PropertyInfo.GetValue on Boolean始终为True

时间:2015-02-06 19:45:33

标签: c# csom sharepoint-online

我正在考虑使用SharePoint字段来检查隐藏属性(Sytem.Boolean),以便可以切换它。我注意到,即使我知道字段Hidden属性为False,GetValue(f,null)也始终为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?
            });
        }
    }

2 个答案:

答案 0 :(得分:0)

非常确定您还需要加载该字段......

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?
        });
    }
    }

答案 1 :(得分:0)

下面是一个示例,如果您根据您的请求

处理以下代码
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);
        }
    }
}