using LiveChartsCore.Geo; using SkiaSharp; using SunlightCentralizedControlManagement_SCCM_.UserClass; using SunlightCentralizedControlManagement_SCCM_.ViewModel; using SunlightCentralizedControlManagement_SCCM_.WindowsView; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; using System.Xml.Linq; using TouchSocket.Core; using TouchSocket.Sockets; using VncSharpWpf; using static SunlightCentralizedControlManagement_SCCM_.WindowsView.ViewStep; using static System.Net.WebRequestMethods; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar; namespace SunlightCentralizedControlManagement_SCCM_.View { /// /// MonitorView.xaml 的交互逻辑 /// public partial class MonitorView : UserControl, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { if (propertyName != null) { PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } public MonitorView() { DataContext = this; InitializeComponent(); CountDown(); machine= MainWindowViewModel.machine; workOrder= MainWindowViewModel.workOrder; dyelot= MainWindowViewModel.dyelot; machin_LOG.Text = MainWindowViewModel.machin_LOG; IsInteractive = false; DATA_view(); UserInformation userInformation = new UserInformation(); userInformation.Topmost = true; userInformation.Show(); userInformation.Activate(); } private string machine; private string workOrder; private string dyelot; private int machines_info=0; public static string Sys_log; public static DataTable Product_DAT = new DataTable(); public static bool Product_bool = false; private void Griddata_SelectionChanged(object sender, SelectionChangedEventArgs e)//设备列表 { int rownum = Griddata.SelectedIndex;//获取鼠标选中行并定义变量 string mac_s = "800"; if (rownum != -1)//判断鼠标定位是否有效 { mac_s = (Griddata.Columns[0].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第1列, } else { return; } if ((mac_s != "800") && (mac_s != "801") && (mac_s != "802")) { Stop.IsEnabled = true; Paused.IsEnabled = true; Insert.IsEnabled = false; edit.IsEnabled = false; Resume.IsEnabled = false; Delete.IsEnabled = false; Jump.IsEnabled = false; DyeingMachine.IsEnabled = true; Curve.IsEnabled = true; Screen.IsEnabled = true; Receipt.IsEnabled = true; LOCK_XML.IsEnabled = true; TechnologicalProcess_bool = true; IsInteractive = false; if (rownum != -1)//判断鼠标定位是否有效 { machine = (Griddata.Columns[2].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第1列, workOrder = (Griddata.Columns[3].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, dyelot = (Griddata.Columns[4].GetCellContent(Griddata.Items[rownum]) as TextBlock).Text;//定位第列, MainWindowViewModel.machine = machine; MainWindowViewModel.workOrder = workOrder; MainWindowViewModel.dyelot = dyelot; MainWindowViewModel.machin_LOG = Properties.Resources.Machine + ":" + machine + " " + Properties.Resources.WorkOrder + ":" + workOrder; machin_LOG.Text = MainWindowViewModel.machin_LOG; } } else { MessageBox.Show(Properties.Resources.TMNR, "800", MessageBoxButton.OK, MessageBoxImage.Information); // } } private void Gridstep_SelectionChanged(object sender, SelectionChangedEventArgs e)//步骤定位 { TechnologicalProcess_bool = false; int rownum = Gridstep.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { ID = (Gridstep.Columns[2].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第0列, Numder = (Gridstep.Columns[3].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第1列, P1 = (Gridstep.Columns[6].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第6列, P2 = (Gridstep.Columns[7].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第7列, P3 = (Gridstep.Columns[8].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第8列, P4 = (Gridstep.Columns[9].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第9列, P5 = (Gridstep.Columns[10].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第10列, } } private void ListViewItem_DyeingMachine(object sender, MouseButtonEventArgs e) { machines_info = 0; log.Visibility = Visibility.Visible; rdp.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Collapsed; }//信息 private async Task PingTest(string ip) { bool result = false; Ping pingSender = new Ping(); try { PingReply reply = await pingSender.SendPingAsync(ip, 3000); if (reply.Status == IPStatus.Success) { result = true; } } catch (PingException) { result = false; } return result; } private async void ListViewItem_Screen(object sender, MouseButtonEventArgs e) { DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); string host = drEmployee.Field("IP"); int port = 5900; string password = "123456";//drEmployee.Field("SYSKEY"); // bool Desktop = drEmployee.Field("Desktop"); if (!drEmployee.Field("Desktop")) { MessageBox.Show(Properties.Resources.Not_a, "Desktop", MessageBoxButton.OK); return; } machines_info = 1; log.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Collapsed; rdp.Visibility = Visibility.Visible; // RemoteDesktopWpf remoteDesktopWpf = new RemoteDesktopWpf(); // Picture.Content = remoteDesktopWpf; if (await PingTest(host)) { if (!rdp.IsConnected) { try { rdp.VncPort = port; rdp.Passwd = password; rdp.Connect(host, true, false); } catch (Exception ex) { LogGing.ERRDATA(ex); } } } }//远程 private void ListViewItem_Receipt(object sender, MouseButtonEventArgs e) { DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); if (!drEmployee.Field("Dispense")) { MessageBox.Show(Properties.Resources.Not_a, "Dispense", MessageBoxButton.OK); return; } int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC833" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") }); machines_info = 2; log.Visibility = Visibility.Collapsed; rdp.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Visible; DyelotView dyelotView = new DyelotView(); dyelotView.DyelotDatatable(Product_DAT); Picture.Content = dyelotView; }//单据 private void ListViewItem_LOCK_XML(object sender, MouseButtonEventArgs e) { string mode = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First().Field("LOCK"); Dictionary dat_821 = new Dictionary(); dat_821.Clear(); if (mode != "True") { dat_821.Add("LOCK", "True"); MainWindowViewModel.Updata_Machines(MainWindowViewModel.Machines, "LOCK", "Name='" + machine + "'", "True"); } else { dat_821.Add("LOCK", "FALSE"); MainWindowViewModel.Updata_Machines(MainWindowViewModel.Machines, "LOCK", "Name='" + machine + "'", "False"); } DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC829" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); if (mode != "True") { MessageBox.Show(machine + Properties.Resources.user_lock, "800", MessageBoxButton.OK, MessageBoxImage.Information); } else { MessageBox.Show(machine + Properties.Resources.user_unlock, "800", MessageBoxButton.OK, MessageBoxImage.Information); } }//锁定 private void ListViewItem_Curve(object sender, MouseButtonEventArgs e) { log.Visibility = Visibility.Collapsed; rdp.Visibility = Visibility.Collapsed; Picture.Visibility = Visibility.Visible; Picture.Content = new View.CurveDiagram(true, "", "", "", workOrder); machines_info = 3; }//曲线 private void ListViewItem_Resume(object sender, MouseButtonEventArgs e) { IsInteractive = false; if (!string.IsNullOrEmpty(machine)) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "CONTINUE"); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); } Insert.IsEnabled = false; edit.IsEnabled = false; Resume.IsEnabled = false; Delete.IsEnabled = false; Jump.IsEnabled = false; TechnologicalProcess_bool = true; }//恢复 private void ListViewItem_Paused(object sender, MouseButtonEventArgs e) { IsInteractive = true; if (!string.IsNullOrEmpty(machine)) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "PAUSE"); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); } Insert.IsEnabled = true; edit.IsEnabled = true; Paused.IsEnabled = true; Resume.IsEnabled = true; Delete.IsEnabled = true; Jump.IsEnabled = true; }//暂停 private void ListViewItem_Stop(object sender, MouseButtonEventArgs e) { IsInteractive = false; if (!string.IsNullOrEmpty(machine)) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "STOP"); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); } }//停止 private void ListViewItem_edit(object sender, MouseButtonEventArgs e) { if (string.IsNullOrEmpty(Numder)) { Numder = null; P1 = "0"; P2 = "0"; P3 = "0"; P4 = "0"; P5 = "0"; } ViewStep viewstop = new ViewStep(Numder, P1, P2, P3, P4, P5); viewstop.data = ID; viewstop.AddressUpdated += new ViewStep.AddressUpdateHandler(edit_ButtonClicked); viewstop.ShowDialog(); }//编辑 private void edit_ButtonClicked(object sender, AddressUpdateEventArgs e)//编辑返回结果 { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "EDIT"); dat_821.Add("StepID", e.StepID); dat_821.Add("StepName", e.StepNAME); dat_821.Add("ParameterName", e.PNAME); dat_821.Add("Parameter1", e.P1); dat_821.Add("Parameter2", e.P2); dat_821.Add("Parameter3", e.P3); dat_821.Add("Parameter4", e.P4); dat_821.Add("Parameter5", e.P5); dat_821.Add("ID", ID); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); TechnologicalProcess_bool = true; } private void ListViewItem_Jump(object sender, MouseButtonEventArgs e) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "JUMP"); dat_821.Add("ID", ID); dat_821.Add("Numder", Numder); dat_821.Add("P1", P1); dat_821.Add("P2", P2); dat_821.Add("P3", P3); dat_821.Add("P4", P4); dat_821.Add("P5", P5); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); TechnologicalProcess_bool = true; }//跳步 private void ListViewItem_Insert(object sender, MouseButtonEventArgs e) { ViewStep viewstop = new ViewStep(null, "0", "0", "0", "0", "0"); viewstop.data = ID; viewstop.AddressUpdated += new ViewStep.AddressUpdateHandler(Insert_ButtonClicked); viewstop.ShowDialog(); }//插入 private void Insert_ButtonClicked(object sender, AddressUpdateEventArgs e)//cr返回结果 { TechnologicalProcess_bool = true; Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "INSERT"); dat_821.Add("StepID", e.StepID); dat_821.Add("StepName", e.StepNAME); dat_821.Add("ParameterName", e.PNAME); dat_821.Add("Parameter1", e.P1); dat_821.Add("Parameter2", e.P2); dat_821.Add("Parameter3", e.P3); dat_821.Add("Parameter4", e.P4); dat_821.Add("Parameter5", e.P5); dat_821.Add("ID", ID); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); } private void ListViewItem_Delete(object sender, MouseButtonEventArgs e) { if (!string.IsNullOrEmpty(machine)) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "DELETE"); dat_821.Add("ID", ID); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); TechnologicalProcess_bool = true; } }//删除 string Numder = null; string ID = null; string P1, P2, P3, P4, P5; private void MenuItem_edit(object sender, RoutedEventArgs e)//右键编辑 { int rownum = Gridstep.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { ID = (Gridstep.Columns[2].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第0列, Numder = (Gridstep.Columns[3].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第1列, P1 = (Gridstep.Columns[6].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第6列, P2 = (Gridstep.Columns[7].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第7列, P3 = (Gridstep.Columns[8].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第8列, P4 = (Gridstep.Columns[9].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第9列, P5 = (Gridstep.Columns[10].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第10列, if (string.IsNullOrEmpty(Numder)) { Numder = null; P1 = "0"; P2 = "0"; P3 = "0"; P4 = "0"; P5 = "0"; } ViewStep viewstop = new ViewStep(Numder, P1, P2, P3, P4, P5); viewstop.data = ID; viewstop.AddressUpdated += new ViewStep.AddressUpdateHandler(edit_ButtonClicked); viewstop.ShowDialog(); } } private void MenuItem_Insert(object sender, RoutedEventArgs e)//右键插入 { int rownum = Gridstep.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { ID = (Gridstep.Columns[2].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位列, ViewStep viewstop = new ViewStep(null, "0", "0", "0", "0", "0"); viewstop.data = ID; viewstop.AddressUpdated += new ViewStep.AddressUpdateHandler(Insert_ButtonClicked); viewstop.ShowDialog(); } } private void MenuItem_Delete(object sender, RoutedEventArgs e)//右键删除 { int rownum = Gridstep.SelectedIndex;//获取鼠标选中行并定义变量 if (rownum != -1)//判断鼠标定位是否有效 { ID = (Gridstep.Columns[2].GetCellContent(Gridstep.Items[rownum]) as TextBlock).Text;//定位第0列, if (!string.IsNullOrEmpty(machine)) { Dictionary dat_821 = new Dictionary(); dat_821.Clear(); dat_821.Add("INSTRUCTION", "DELETE"); dat_821.Add("ID", ID); DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int index = Convert.ToInt16(drEmployee.Field("ID")); MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC821" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") + dat_821.ToJsonString() }); TechnologicalProcess_bool = true; } } } public void CountDown() { DispatcherTimer timer1s = new DispatcherTimer//初始化循环,每1秒调用一次Tick { Interval = TimeSpan.FromSeconds(2)//秒 }; timer1s.Tick += Tick_Event_1S; timer1s.Start(); }//时间周期初始化 int Time_D = 0; void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件1S { DATA_view(); if (Paused.IsEnabled) { if (!Resume.IsEnabled) { Time_D++; if (Time_D > 10) { TechnologicalProcess_bool = true; } } } else { Time_D=0; } if (!string.IsNullOrEmpty(machine)) { try { DataRow drEmployee = MainWindowViewModel.Machines.Select("NAME='" + machine + "'").First(); int State_ = Convert.ToInt16(drEmployee.Field("State")); int index = Convert.ToInt16(drEmployee.Field("ID")); if ((State_ == 101) || (State_ == 201) || (State_ == 202) || (State_ == 309))//获得细节信息 { if (machines_info == 0) { MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC832" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") }); }//信息 else if (machines_info == 1) { }//输送状态 else if (machines_info == 2 && Product_bool) { Product_bool = false; DyelotView dyelotView = new DyelotView(); dyelotView.DyelotDatatable(Product_DAT); Picture.Content = dyelotView; }//单据 MainWindowViewModel.stringQueue.Enqueue(new MainWindowViewModel.QueueString { ID = index, DAT = "SC831" + MainWindowViewModel.Selet_Machines(MainWindowViewModel.Machines, "SYSKEY", "ID='" + index + "'") }); } else { MainWindowViewModel.MachineLOG = null; MainWindowViewModel.dt_TP.Clear(); } } catch (Exception ) { MainWindowViewModel.MachineLOG = null; MainWindowViewModel.dt_TP.Clear(); }//返回错误为空 } } public string sys_machine; //显示log public string Sys_machine //通知UI控件参数改变 { get { return sys_machine; } set { sys_machine = value; OnPropertyChanged("Sys_machine"); } } bool _isInteractive; public bool IsInteractive//步骤表交互选择 { get => _isInteractive; set { _isInteractive = value; OnPropertyChanged("IsInteractive"); } } private void Gridstep_ContextMenuOpening(object sender, ContextMenuEventArgs e) { if(!IsInteractive) e.Handled = true;//阻止菜单显示 } public static bool TechnologicalProcess_bool = true; private async void DATA_view()//IO显示 { await Task.Run(() => { try { TechnologicalMachine_View = ToObservableCollection(MainWindowViewModel.Machines); } catch (Exception) { } try { if (MainWindowViewModel.dt_TP.Rows.Count > 0) { if (TechnologicalProcess_bool) { TechnologicalProcess_View = ToObservableCollection(MainWindowViewModel.dt_TP); } Sys_machine = MainWindowViewModel.MachineLOG; } } catch (Exception) { } }); } ObservableCollection technologicalProcess_View = new ObservableCollection(); ObservableCollection technologicalMachine_View = new ObservableCollection(); public ObservableCollection TechnologicalProcess_View { get { return technologicalProcess_View; } set { technologicalProcess_View = value; RaisePropertyChanged("TechnologicalProcess_View"); } } public ObservableCollection TechnologicalMachine_View { get { return technologicalMachine_View; } set { technologicalMachine_View = value; RaisePropertyChanged("TechnologicalMachine_View"); } } public class TechnologicalM { public string Name { get; set; } public string WorkOrder { get; set; } public string Dyelot { get; set; } public string Temperature { get; set; } public string WaterLevel { get; set; } public string Process { get; set; } public string Step { get; set; } public string Message { get; set; } public string State { get; set; } public string LOCK { get; set; } } public class TechnologicalP { public int RUN { get; set; } public string ProgramID { get; set; } public string Program { get; set; } public double Step { get; set; } public string StepID { get; set; } public string StepName { get; set; } public string ParameterName { get; set; } public string Parameter1 { get; set; } public string Parameter2 { get; set; } public string Parameter3 { get; set; } public string Parameter4 { get; set; } public string Parameter5 { get; set; } public string DYELOT { get; set; } } public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() { try { Type t = typeof(T); PropertyInfo[] propertys = t.GetProperties(); ObservableCollection lst = new ObservableCollection(); string typeName = string.Empty; foreach (DataRow dr in dt.Rows) { T entity = new T(); foreach (PropertyInfo pi in propertys) { typeName = pi.Name; if (dt.Columns.Contains(typeName)) { if (!pi.CanWrite) continue; object value = dr[typeName]; if (value == DBNull.Value) continue; if (pi.PropertyType == typeof(string)) { pi.SetValue(entity, value.ToString(), null); } else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?)) { pi.SetValue(entity, int.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime)) { pi.SetValue(entity, DateTime.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(float)) { pi.SetValue(entity, float.Parse(value.ToString()), null); } else if (pi.PropertyType == typeof(double)) { pi.SetValue(entity, double.Parse(value.ToString()), null); } else { pi.SetValue(entity, value, null); } } } lst.Add(entity); } return lst; } catch (Exception) { return null; } } } }