Я хочу использовать приведенные ниже коды.Итак, у меня есть общий список , я думаю, что я должен использовать using System.Reflection;метод для получения свойства list для заполнения строки [] columnNames, object [] columnValues, но как я могу его использовать?
foreach (var item in List<myClass>)
{
// i want to fill get columnNames and values to fill below arrays how to?
}
public static class MyExtensionMethod
{
public static bool DynamicInsertCustomerExtension(this MyDataContext db, string[] columnNames, object[] columnValues)
{
try
{
if (columnNames.Length == 0 || columnValues.Length == 0)
{
throw new Exception("Column Name or Column Value array is empty!");
}
if (columnNames.Length != columnValues.Length)
{
throw new Exception("Column Name array and Column Value array are in different length!");
}
TBL_Customer entity = new TBL_Customer();
for (int i = 0; i < columnNames.Length; i++)
{
entity.GetType().GetProperty(columnNames[i]).SetValue(entity, columnValues[i], null);
}
db.TBL_Customers.InsertOnSubmit(entity);
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}