c# - how to exclude some list properties to make data table -
in below utility function (list data table), how can exclude member of list.
like, don't want send 2 properties "uniquekey" & "pointtopointdata" utility method below,
public class pointdataclone { public int dataid { get; set; } public string uniquekey { get; set; } public int count { get; set; } public list<pointtopointdata> pointtopointdata { get; set; } }
utility function,
public static datatable todatatable<t>(this list<t> ilist) { datatable datatable = new datatable(); propertydescriptorcollection propertydescriptorcollection = typedescriptor.getproperties(typeof(t)); (int = 0; < propertydescriptorcollection.count; i++) { propertydescriptor propertydescriptor = propertydescriptorcollection[i]; type type = propertydescriptor.propertytype; if (type.isgenerictype && type.getgenerictypedefinition() == typeof(nullable<>)) type = nullable.getunderlyingtype(type); datatable.columns.add(propertydescriptor.name, type); } object[] values = new object[propertydescriptorcollection.count]; foreach (t ilistitem in ilist) { (int = 0; < values.length; i++) { values[i] = propertydescriptorcollection[i].getvalue(ilistitem); } datatable.rows.add(values); } return datatable; }
create attribute
[attributeusage(attributetargets.method | attributetargets.property | attributetargets.field | attributetargets.parameter, allowmultiple = false)] public class dontshowme : attribute { }
you can use attribute annotate class
public class pointdataclone { public int dataid { get; set; } [dontshowme] public string uniquekey { get; set; } public int count { get; set; } [dontshowme] public list<pointtopointdata> pointtopointdata { get; set; } }
and modify function query attributes. need additional using statement
using system.componentmodel;
add line loop
if (type.isgenerictype && type.getgenerictypedefinition() == typeof(nullable<>)) type = nullable.getunderlyingtype(type); // test attribute see if shown if (propertydescriptor.attributes.contains(new dontshowme())) continue; datatable.columns.add(propertydescriptor.name, type);
you have deal fact object have more properties datatable has columns. leave manage little detail.
hope helps,
marc
Comments
Post a Comment