C# 使用对象属性动态填充数据表
我正在尝试动态地将对象属性添加到数据表中。我试图避免显式地声明对象的属性,因为我希望此代码适用于应用程序中的每个对象。 我已经阅读了,但这对我来说不够动态,因为他明确指定了属性C# 使用对象属性动态填充数据表,c#,datatable,C#,Datatable,我正在尝试动态地将对象属性添加到数据表中。我试图避免显式地声明对象的属性,因为我希望此代码适用于应用程序中的每个对象。 我已经阅读了,但这对我来说不够动态,因为他明确指定了属性 private static void Main(string[] args) { //Read in JSON from text file StreamReader fileStream = new StreamReader(@"C:\Users\Aid
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>();
}
}
我收到错误:“Client”不包含“prop”的定义,并且没有扩展方法“prop”接受“Client”类型的第一个参数。
我理解为什么会出现错误,但我不确定如何使其工作
我正在尝试添加一个具有PropertyNamenewRow[prop]
的新行,其值为objects属性值=u.prop
编辑:DataTable列名将与对象属性名相同
foreach的第一次迭代应如下所示:
foreach (var prop in CliProperties)
{
dt.Columns.Add("RefID");
newRow["RefID"] = u.RefID;
}
使用这个用于List的扩展方法,将您的
User
对象设置为List
,只需在其实例上调用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;
}
}
刚刚为类的单个对象编写了另一个扩展方法:
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;
}
公共静态数据表ToDataTable(此T项),其中T:class
{
DataTable=新的DataTable();
属性描述集合属性描述集合=
GetProperties(typeof(T));
对于(int i=0;i
这样称呼它:
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);
}
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = user.ToDataTable();
Client u=JsonConvert.DeserializeObject(json);
DataTable dt=user.ToDataTable();
使用此扩展方法(用于列表),将您的用户
对象设置为列表
,只需在其实例上调用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;
}
}
刚刚为类的单个对象编写了另一个扩展方法:
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;
}
公共静态数据表ToDataTable(此T项),其中T:class
{
DataTable=新的DataTable();
属性描述集合属性描述集合=
GetProperties(typeof(T));
对于(int i=0;i
这样称呼它:
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);
}
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = user.ToDataTable();
Client u=JsonConvert.DeserializeObject(json);
DataTable dt=user.ToDataTable();
使用此扩展方法(用于列表),将您的用户
对象设置为列表
,只需在其实例上调用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;
}
}
刚刚为类的单个对象编写了另一个扩展方法:
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;
}
公共静态数据表ToDataTable(此T项),其中T:class
{
DataTable=新的DataTable();
属性描述集合属性描述集合=
GetProperties(typeof(T));
对于(int i=0;i
这样称呼它:
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);
}
Client u = JsonConvert.DeserializeObject<Client>(json);
DataTable dt = user.ToDataTable();
Client u=JsonConvert.DeserializeObject(json);
DataTable dt=user.ToDataTable();
使用此扩展方法(用于列表),将您的用户
对象设置为列表
,只需在其实例上调用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;
}
}
刚刚为类的单个对象编写了另一个扩展方法:
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;
}
公共静态数据表ToDataTable(此T项),其中T:class
{
DataTable=新的DataTable();
属性描述集合属性描述集合=
GetProperties(typeof(T));
对于(int i=0;