首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Reflection.PropertyInfo propertyInfo返回NULL?

System.Reflection.PropertyInfo propertyInfo返回NULL?
EN

Stack Overflow用户
提问于 2012-09-19 15:11:30
回答 1查看 397关注 0票数 0

嗨,我正在将一个数据表传递给一个方法"AsJqGridResult",但是"GetPropertyValue“方法由于"System.Reflection.PropertyInfo propertyInfo”返回NULL而返回错误。

是否有任何问题要传递数据表或其他问题,请帮助.

控制器

代码语言:javascript
复制
[HttpPost]
    public ActionResult DynamicGridData(string sidx, string sord, int page, int rows)
    {
        var context = GetTable().AsEnumerable();
        return (context.AsQueryable().AsJqGridResult(sidx, sord, page, rows));
    }


    public DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Number", typeof(int));
        table.Columns.Add("Abr", typeof(string));
        table.Columns.Add("Country", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        table.Rows.Add(1, "AF", "AFGHANISTAN", DateTime.Now);
        table.Rows.Add(2, "AX", "ÅLAND ISLANDS", DateTime.Now);

        return table;
    }

Helper方法

代码语言:javascript
复制
public static JsonResult AsJqGridResult<T>(this IQueryable<T> source, string column, string sortOrder, int page, int pageSize)
{
  int pageIndex = Convert.ToInt32(page) - 1;
  int totalRecords = source.Count();
  int totalPages = (int) Math.Ceiling((float) totalRecords/(float) pageSize);

  var list = (sortOrder.ToLower()=="asc") ? 
      source.OrderBy(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize) :
      source.OrderByDescending(new Func<T, IComparable>(item => (IComparable) GetPropertyValue(item, column))).Skip(pageIndex*pageSize).Take(pageSize);


  var jsonData = new
  {
    total = totalPages,
    page,
    records = totalRecords,
    rows = (
            from item in list
            select new
                      {
                        i = Guid.NewGuid(),
                        cell = GetGridCells(item)
                      }).ToArray()
  };

  return new JsonResult {Data = jsonData};
}

/// <summary>
/// Gets the property value.
/// </summary>
/// <param name="obj">The obj.</param>
/// <param name="property">The property.</param>
/// <returns></returns>
private static object GetPropertyValue(object obj, string property) {
  System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);
  return propertyInfo.GetValue(obj, null);
}
EN

回答 1

Stack Overflow用户

发布于 2012-09-19 15:14:06

DataTables没有它们的列的属性。

相反,您需要在表上调用TypeDescriptor.GetProperties(),它将使用ITypedList实现获取实际列的PropertyDescriptor

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12497585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档