C# 객체 클론, DataTable - LIST
12654 단어 Datatable
public class ConvertHelper<T> where T : new()
{
private static string module = "ConvertHelper.cs";
public static ObservableCollection<T> ConvertToList(List<T> listobject)
{
ObservableCollection<T> collection = null;
try
{
collection = new ObservableCollection<T>(listobject);
}
catch (Exception ex)
{
ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
"Error occurs on ConvertToList modules: {0}.", ex.Message);
}
return collection;
}
public static ObservableCollection<T> ConvertToObservable(DataTable dt)
{
ObservableCollection<T> collection = null;
//
List<T> ts = new List<T>();
try
{
//
Type type = typeof(T);
//
string tempName = string.Empty;
// DataTable
foreach (DataRow dr in dt.Rows)
{
T t = new T();
//
PropertyInfo[] propertys = t.GetType().GetProperties();
//
foreach (PropertyInfo pi in propertys)
{
//
tempName = pi.Name;
// DataTable ( == )
if (dt.Columns.Contains(tempName))
{
// Setter ,
if (!pi.CanWrite) continue;
//
object value = dr[tempName];
// ,
if (value != DBNull.Value)
pi.SetValue(t, value.ToString(), null);
}
}
//
ts.Add(t);
}
collection = new ObservableCollection<T>(ts);
}
catch (Exception ex)
{
ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
"Error occurs on ConvertToList modules: {0}.", ex.Message);
}
return collection;
}
///
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public static List<T> ConvertToList(DataTable dt)
{
//
List<T> ts = new List<T>();
try
{
//
Type type = typeof(T);
//
string tempName = string.Empty;
// DataTable
foreach (DataRow dr in dt.Rows)
{
T t = new T();
//
PropertyInfo[] propertys = t.GetType().GetProperties();
//
foreach (PropertyInfo pi in propertys)
{
//
tempName = pi.Name;
// DataTable ( == )
if (dt.Columns.Contains(tempName))
{
// Setter ,
if (!pi.CanWrite) continue;
//
object value = dr[tempName];
// ,
if (value != DBNull.Value)
pi.SetValue(t, value.ToString(), null);
}
}
//
ts.Add(t);
}
}
catch (Exception ex)
{
ServiceLocator.Current.GetInstance<IWriteLog>().Log(LogConstant.LogType.Exception, module,
"Error occurs on ConvertToList modules: {0}.", ex.Message);
}
return ts;
}
}
객체 클론 할당
/// <summary>
/// MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Test> list = new List<Test>();
Test test = new Test();
test.ID = "1";
test.NAME = "xz";
list.Add(test);
Test test1=new Test ();
CopyValue(list[0], test1);
test1.NAME = "xznihoa";
list.Add(test1);
}
public static void CopyValue(object origin, object target)
{
System.Reflection.PropertyInfo[] properties = (target.GetType()).GetProperties();
System.Reflection.PropertyInfo[] fields = (origin.GetType()).GetProperties();
for (int i = 0; i < fields.Length; i++)
{
for (int j = 0; j < properties.Length; j++)
{
if (fields[i].Name == properties[j].Name && properties[j].CanWrite)
{
properties[j].SetValue(target, fields[i].GetValue(origin,null), null);
}
}
}
}
}
public class Test
{
public string ID { get; set; }
public string NAME { get; set; }
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
yui--datatable 행 추가 형식텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.