使用对象属性动态填充 DataTable
Populate DataTable dynamically using Object Properties
我正在尝试动态地将对象属性添加到数据表。我试图避免明确说明对象的属性,因为我希望此代码适用于我的应用程序中的每个对象。
我读过 Adding object to DataTable and create a dynamic GridView 但这对我来说不够动态,因为他明确指定了属性。
private static void Main(string[] args)
{
//Read in JSON from text file
StreamReader fileStream = new StreamReader(@"C:\Users\Aid\Desktop\test.txt");
string json = fileStream.ReadToEnd();
//Deserialise and map to class
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = new DataTable();
List<string> CliProperties = new List<string>();
//Loop through Object properties and add to Datatable columns
CliProperties = objProps(u);
DataRow newRow = dt.NewRow();
foreach (var prop in CliProperties)
{
dt.Columns.Add(prop);
newRow[prop] = u.prop;
}
Console.ReadKey();
}
/// <summary>
/// Get list of object properties
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static List<string> objProps(Object obj)
{
List<string> Props = new List<string>();
Type objType = obj.GetType();
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
if (propertyInfo.CanRead)
{
Props.Add(propertyInfo.Name);
}
}
return Props;
}
}
public static class DataColumnCollectionExtensions
{
public static IEnumerable<DataColumn> AsEnumerable(this DataColumnCollection source)
{
return source.Cast<DataColumn>();
}
}
我的问题区域是:
foreach (var prop in CliProperties)
{
dt.Columns.Add(prop);
newRow[prop] = u.prop;
}
我收到错误:'Client' does not contain a definition for 'prop' and no extension method 'prop' accepting first argument of type 'Client'
我明白为什么会收到错误消息,但是我不确定如何让它工作。
我正在尝试使用对象 属性 值 = u.prop
.
的 PropertyName newRow[prop]
添加一个新行
编辑:数据表列名称将与对象 属性 名称相同。
foreach 的第一次迭代应如下所示:
foreach (var prop in CliProperties)
{
dt.Columns.Add("RefID");
newRow["RefID"] = u.RefID;
}
使用这个用于 List 的扩展方法,将您的 User
对象设为 List<User>
,然后简单地在其实例上调用 ToDataTable()
。
这里是扩展方法:
public static class ListExtensions
{
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < 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)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
你必须简单地这样称呼它:
List<User> users = new List<User>();
DataTable dt = users.ToDataTable();
更新:
您的操作方式必须像这样遍历 User
class 属性:
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(User));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dt.Columns.Add(propertyDescriptor.Name, type);
}
刚刚为 Class
的单个对象编写了另一个扩展方法:
public static DataTable ToDataTable<T>(this T Item) where T: class
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < 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];
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(Item);
}
dataTable.Rows.Add(values);
return dataTable;
}
并这样称呼它:
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = user.ToDataTable();
我正在尝试动态地将对象属性添加到数据表。我试图避免明确说明对象的属性,因为我希望此代码适用于我的应用程序中的每个对象。 我读过 Adding object to DataTable and create a dynamic GridView 但这对我来说不够动态,因为他明确指定了属性。
private static void Main(string[] args)
{
//Read in JSON from text file
StreamReader fileStream = new StreamReader(@"C:\Users\Aid\Desktop\test.txt");
string json = fileStream.ReadToEnd();
//Deserialise and map to class
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = new DataTable();
List<string> CliProperties = new List<string>();
//Loop through Object properties and add to Datatable columns
CliProperties = objProps(u);
DataRow newRow = dt.NewRow();
foreach (var prop in CliProperties)
{
dt.Columns.Add(prop);
newRow[prop] = u.prop;
}
Console.ReadKey();
}
/// <summary>
/// Get list of object properties
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static List<string> objProps(Object obj)
{
List<string> Props = new List<string>();
Type objType = obj.GetType();
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
if (propertyInfo.CanRead)
{
Props.Add(propertyInfo.Name);
}
}
return Props;
}
}
public static class DataColumnCollectionExtensions
{
public static IEnumerable<DataColumn> AsEnumerable(this DataColumnCollection source)
{
return source.Cast<DataColumn>();
}
}
我的问题区域是:
foreach (var prop in CliProperties)
{
dt.Columns.Add(prop);
newRow[prop] = u.prop;
}
我收到错误:'Client' does not contain a definition for 'prop' and no extension method 'prop' accepting first argument of type 'Client'
我明白为什么会收到错误消息,但是我不确定如何让它工作。
我正在尝试使用对象 属性 值 = u.prop
.
newRow[prop]
添加一个新行
编辑:数据表列名称将与对象 属性 名称相同。
foreach 的第一次迭代应如下所示:
foreach (var prop in CliProperties)
{
dt.Columns.Add("RefID");
newRow["RefID"] = u.RefID;
}
使用这个用于 List 的扩展方法,将您的 User
对象设为 List<User>
,然后简单地在其实例上调用 ToDataTable()
。
这里是扩展方法:
public static class ListExtensions
{
public static DataTable ToDataTable<T>(this List<T> iList)
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < 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)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
你必须简单地这样称呼它:
List<User> users = new List<User>();
DataTable dt = users.ToDataTable();
更新:
您的操作方式必须像这样遍历 User
class 属性:
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(User));
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type type = propertyDescriptor.PropertyType;
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
type = Nullable.GetUnderlyingType(type);
dt.Columns.Add(propertyDescriptor.Name, type);
}
刚刚为 Class
的单个对象编写了另一个扩展方法:
public static DataTable ToDataTable<T>(this T Item) where T: class
{
DataTable dataTable = new DataTable();
PropertyDescriptorCollection propertyDescriptorCollection =
TypeDescriptor.GetProperties(typeof(T));
for (int i = 0; i < 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];
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(Item);
}
dataTable.Rows.Add(values);
return dataTable;
}
并这样称呼它:
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = user.ToDataTable();