1240 lines
32 KiB
C#
1240 lines
32 KiB
C#
using Microsoft.CSharp;
|
||
using System;
|
||
using System.CodeDom.Compiler;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Dynamic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Reflection;
|
||
using System.Runtime.Serialization;
|
||
using System.Runtime.Serialization.Formatters.Binary;
|
||
using System.Text;
|
||
|
||
namespace AIMSExtension
|
||
{
|
||
public static class GPFunData
|
||
{
|
||
public static bool IsNumericType(this object o)
|
||
{
|
||
TypeCode typeCode = Type.GetTypeCode(o.GetType());
|
||
TypeCode typeCode2 = typeCode;
|
||
return typeCode2 - TypeCode.SByte <= 10;
|
||
}
|
||
|
||
public static List<T> ToList<T>(this DataTable dataTable) where T : new()
|
||
{
|
||
return GPFunData.DataTableToList<T>(dataTable);
|
||
}
|
||
|
||
public static DataTable MergeColumn(DataTable dataTable, string colName)
|
||
{
|
||
bool flag = dataTable.Rows.Count <= 1;
|
||
DataTable result;
|
||
if (flag)
|
||
{
|
||
result = dataTable;
|
||
}
|
||
else
|
||
{
|
||
DataTable dataTable2 = new DataTable();
|
||
int columnIndex = 0;
|
||
List<string> list = new List<string>();
|
||
Dictionary<int, int> dictionary = new Dictionary<int, int>();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
int count = columns.Count;
|
||
int num = 0;
|
||
DataColumn column;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
string columnName = columns[i].ColumnName;
|
||
bool flag2 = !colName.Equals(columnName);
|
||
if (flag2)
|
||
{
|
||
list.Add(columnName);
|
||
column = new DataColumn(columnName, columns[i].DataType);
|
||
dataTable2.Columns.Add(column);
|
||
dictionary.Add(i, num);
|
||
num++;
|
||
}
|
||
else
|
||
{
|
||
columnIndex = i;
|
||
}
|
||
}
|
||
column = new DataColumn(colName, Type.GetType("System.String"));
|
||
dataTable2.Columns.Add(column);
|
||
DataRow[] array = dataTable.Select("", string.Join(",", list));
|
||
int num2 = array.Length;
|
||
DataRow dataRow = dataTable2.NewRow();
|
||
list.Clear();
|
||
int[] cols = dictionary.Keys.ToArray<int>();
|
||
for (int j = 0; j < num2; j++)
|
||
{
|
||
int num3 = j - 1;
|
||
bool flag3 = num3 < 0;
|
||
bool flag4 = flag3 || !GPFunData.IsSameDataRow(array[num3], array[j], cols);
|
||
bool flag5 = flag4;
|
||
if (flag5)
|
||
{
|
||
bool flag6 = num3 >= 0;
|
||
if (flag6)
|
||
{
|
||
dataRow[count - 1] = string.Join(" \r\n", list);
|
||
}
|
||
list.Clear();
|
||
list.Add(array[j][columnIndex].ToString());
|
||
dataRow = dataTable2.NewRow();
|
||
GPFunData.CopyDataRow(array[j], dataRow, dictionary);
|
||
dataTable2.Rows.Add(dataRow);
|
||
}
|
||
else
|
||
{
|
||
list.Add(array[j][columnIndex].ToString());
|
||
}
|
||
}
|
||
result = dataTable2;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool IsSameDataRow(DataRow dr1, DataRow dr2, int[] cols)
|
||
{
|
||
bool result = true;
|
||
for (int i = 0; i < cols.Length; i++)
|
||
{
|
||
int columnIndex = cols[i];
|
||
bool flag = !dr1[columnIndex].ToString().Equals(dr2[columnIndex].ToString());
|
||
if (flag)
|
||
{
|
||
result = false;
|
||
break;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool IsSameDataRow(DataRow dr1, DataRow dr2, Dictionary<int, int> cols)
|
||
{
|
||
bool result = true;
|
||
foreach (KeyValuePair<int, int> current in cols)
|
||
{
|
||
bool flag = dr1[current.Key].ToString() != dr2[current.Value].ToString();
|
||
if (flag)
|
||
{
|
||
result = false;
|
||
break;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool IsSameDataRow(DataRow dr1, DataRow dr2, string[] cols)
|
||
{
|
||
bool result = true;
|
||
for (int i = 0; i < cols.Length; i++)
|
||
{
|
||
string columnName = cols[i];
|
||
bool flag = dr1[columnName].ToString() != dr2[columnName].ToString();
|
||
if (flag)
|
||
{
|
||
result = false;
|
||
break;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool IsSameDataRow(DataRowView dr1, DataRowView dr2, string[] cols)
|
||
{
|
||
bool result = true;
|
||
for (int i = 0; i < cols.Length; i++)
|
||
{
|
||
string property = cols[i];
|
||
bool flag = dr1[property].ToString() != dr2[property].ToString();
|
||
if (flag)
|
||
{
|
||
result = false;
|
||
break;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool CopyDataRow(DataRow dr1, DataRow dr2)
|
||
{
|
||
DataColumnCollection columns = dr1.Table.Columns;
|
||
Dictionary<string, int> dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
||
int i = 0;
|
||
int count = columns.Count;
|
||
while (i < count)
|
||
{
|
||
dictionary.Add(columns[i].ColumnName, i);
|
||
i++;
|
||
}
|
||
columns = dr2.Table.Columns;
|
||
int j = 0;
|
||
int count2 = columns.Count;
|
||
while (j < count2)
|
||
{
|
||
string columnName = columns[j].ColumnName;
|
||
bool flag = dictionary.ContainsKey(columnName);
|
||
if (flag)
|
||
{
|
||
dr2[j] = dr1[dictionary[columnName]];
|
||
}
|
||
j++;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static bool CopyDataRow(DataRow dr1, DataRow dr2, int[] cols)
|
||
{
|
||
bool flag = cols.Length == 0;
|
||
if (flag)
|
||
{
|
||
int i = 0;
|
||
int num = dr2.ItemArray.Length;
|
||
while (i < num)
|
||
{
|
||
dr2[i] = dr1[i];
|
||
i++;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
for (int j = 0; j < cols.Length; j++)
|
||
{
|
||
int columnIndex = cols[j];
|
||
dr2[columnIndex] = dr1[columnIndex];
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static bool CopyDataRow(DataRow dr1, DataRow dr2, Dictionary<int, int> cols)
|
||
{
|
||
foreach (KeyValuePair<int, int> current in cols)
|
||
{
|
||
dr2[current.Value] = dr1[current.Key];
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static bool CopyDataRow(DataRow dr1, DataRow dr2, string[] colNames)
|
||
{
|
||
bool flag = colNames == null || colNames.Length == 0;
|
||
bool result;
|
||
if (flag)
|
||
{
|
||
result = GPFunData.CopyDataRow(dr1, dr2);
|
||
}
|
||
else
|
||
{
|
||
for (int i = 0; i < colNames.Length; i++)
|
||
{
|
||
string columnName = colNames[i];
|
||
dr2[columnName] = dr1[columnName];
|
||
}
|
||
result = true;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static int DeleteEmptyFieldRows(DataTable dataTable, string fieldName)
|
||
{
|
||
int num = 0;
|
||
DataRowCollection rows = dataTable.Rows;
|
||
int count = rows.Count;
|
||
for (int i = count - 1; i >= 0; i--)
|
||
{
|
||
DataRow dataRow = rows[i];
|
||
bool flag = dataRow.RowState == DataRowState.Deleted;
|
||
if (!flag)
|
||
{
|
||
object obj = dataRow[fieldName];
|
||
bool flag2 = obj == DBNull.Value || GPFunctions.CStr<object>(obj) == "";
|
||
if (flag2)
|
||
{
|
||
dataRow.Delete();
|
||
num++;
|
||
}
|
||
}
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public static int DeleteEmptyFieldsRows(DataTable dataTable, string[] fieldNames)
|
||
{
|
||
int num = 0;
|
||
DataRowCollection rows = dataTable.Rows;
|
||
int count = rows.Count;
|
||
for (int i = count - 1; i >= 0; i--)
|
||
{
|
||
DataRow dataRow = rows[i];
|
||
bool flag = dataRow.RowState == DataRowState.Deleted;
|
||
if (!flag)
|
||
{
|
||
bool flag2 = false;
|
||
for (int j = 0; j < fieldNames.Length; j++)
|
||
{
|
||
string columnName = fieldNames[j];
|
||
object obj = dataRow[columnName];
|
||
bool flag3 = obj == DBNull.Value || GPFunctions.CStr<object>(obj) == "";
|
||
if (flag3)
|
||
{
|
||
flag2 = true;
|
||
num++;
|
||
break;
|
||
}
|
||
}
|
||
bool flag4 = flag2;
|
||
if (flag4)
|
||
{
|
||
dataRow.Delete();
|
||
}
|
||
}
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public static List<T> DataTableToList<T>(DataTable dataTable) where T : new()
|
||
{
|
||
T t = Activator.CreateInstance<T>();
|
||
Type type = t.GetType();
|
||
List<T> list = new List<T>();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
Dictionary<string, IndexType> dictionary = new Dictionary<string, IndexType>();
|
||
PropertyInfo[] properties = type.GetProperties();
|
||
for (int i = 0; i < properties.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = properties[i];
|
||
string name = propertyInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag = num >= 0;
|
||
if (flag)
|
||
{
|
||
IndexType indexType;
|
||
indexType.index = num;
|
||
bool flag2 = !propertyInfo.PropertyType.Equals(columns[num].DataType);
|
||
if (flag2)
|
||
{
|
||
indexType.type = 1;
|
||
}
|
||
else
|
||
{
|
||
indexType.type = 0;
|
||
}
|
||
dictionary.Add(name, indexType);
|
||
}
|
||
}
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
t = Activator.CreateInstance<T>();
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
PropertyInfo property = type.GetProperty(current);
|
||
bool flag3 = property == null;
|
||
if (!flag3)
|
||
{
|
||
IndexType indexType = dictionary[current];
|
||
int index = indexType.index;
|
||
bool flag4 = dataRow[index] != DBNull.Value;
|
||
if (flag4)
|
||
{
|
||
bool flag5 = indexType.type == 0;
|
||
if (flag5)
|
||
{
|
||
property.SetValue(t, dataRow[index], null);
|
||
}
|
||
else
|
||
{
|
||
Type type2 = property.PropertyType;
|
||
bool flag6 = type2.IsGenericType && type2.GetGenericTypeDefinition() == typeof(Nullable<>);
|
||
if (flag6)
|
||
{
|
||
type2 = type2.GetGenericArguments()[0];
|
||
}
|
||
object value = Convert.ChangeType(dataRow[index], type2);
|
||
property.SetValue(t, value, null);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
list.Add(t);
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public static DataTable ToListDataTable<T>(DataTable dataTable) where T : new()
|
||
{
|
||
T t = Activator.CreateInstance<T>();
|
||
Type type = t.GetType();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
Dictionary<int, Type> dictionary = new Dictionary<int, Type>();
|
||
PropertyInfo[] properties = type.GetProperties();
|
||
for (int i = 0; i < properties.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = properties[i];
|
||
string name = propertyInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag = num >= 0;
|
||
if (flag)
|
||
{
|
||
bool flag2 = !propertyInfo.PropertyType.Equals(columns[num].DataType);
|
||
if (flag2)
|
||
{
|
||
dictionary.Add(num, propertyInfo.PropertyType);
|
||
}
|
||
}
|
||
}
|
||
foreach (KeyValuePair<int, Type> current in dictionary)
|
||
{
|
||
columns[current.Key].DataType = current.Value;
|
||
}
|
||
dataTable.AcceptChanges();
|
||
return dataTable;
|
||
}
|
||
|
||
public static DataTable ToListDataTable2<T>(DataTable dataTable) where T : new()
|
||
{
|
||
T t = Activator.CreateInstance<T>();
|
||
Type type = t.GetType();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
Dictionary<string, IndexType> dictionary = new Dictionary<string, IndexType>();
|
||
PropertyInfo[] properties = type.GetProperties();
|
||
for (int i = 0; i < properties.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = properties[i];
|
||
string name = propertyInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag = num >= 0;
|
||
if (flag)
|
||
{
|
||
bool flag2 = !propertyInfo.PropertyType.Equals(columns[num].DataType);
|
||
if (flag2)
|
||
{
|
||
columns.Add(string.Format("new__{1}", name), propertyInfo.PropertyType);
|
||
IndexType indexType;
|
||
indexType.index = num;
|
||
indexType.type = columns.Count - 1;
|
||
dictionary.Add(name, indexType);
|
||
}
|
||
}
|
||
}
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
IndexType indexType = dictionary[current];
|
||
int index = indexType.index;
|
||
int type2 = indexType.type;
|
||
bool flag3 = dataRow[index] != DBNull.Value;
|
||
if (flag3)
|
||
{
|
||
dataRow[type2] = Convert.ChangeType(dataRow[index], columns[type2].DataType);
|
||
}
|
||
}
|
||
}
|
||
foreach (string current2 in dictionary.Keys)
|
||
{
|
||
int index2 = columns.IndexOf(current2);
|
||
columns.RemoveAt(index2);
|
||
string name2 = string.Format("new__{0}", current2);
|
||
columns[name2].ColumnName = current2;
|
||
}
|
||
dataTable.AcceptChanges();
|
||
return dataTable;
|
||
}
|
||
|
||
public static List<string> GetDataTableModifiedFirtRowInfo(DataTable dataTable)
|
||
{
|
||
List<string> list = new List<string>();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
int count = columns.Count;
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
bool flag = false;
|
||
bool flag2 = dataRow.RowState == DataRowState.Modified;
|
||
if (flag2)
|
||
{
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
bool flag3 = dataRow[i, DataRowVersion.Original] == DBNull.Value;
|
||
if (flag3)
|
||
{
|
||
bool flag4 = dataRow[i] != DBNull.Value;
|
||
if (flag4)
|
||
{
|
||
flag = true;
|
||
list.Add(string.Format("Row={0}, FieldName={1}, DataType={2}, VALUE={3}, ORIGINAL=DBNull.Value", new object[]
|
||
{
|
||
i,
|
||
columns[i].ColumnName,
|
||
columns[i].DataType.Name,
|
||
dataRow[i].ToString()
|
||
}));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
bool flag5 = dataRow[i] == DBNull.Value;
|
||
if (flag5)
|
||
{
|
||
flag = true;
|
||
list.Add(string.Format("Row={0}, FieldName={1}, DataType={2}, VALUE=DBNull.Value, ORIGINAL={3}", new object[]
|
||
{
|
||
i,
|
||
columns[i].ColumnName,
|
||
columns[i].DataType.Name,
|
||
dataRow[i, DataRowVersion.Original].ToString()
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
string text = dataRow[i].ToString();
|
||
string text2 = dataRow[i, DataRowVersion.Original].ToString();
|
||
bool flag6 = text != text2;
|
||
if (flag6)
|
||
{
|
||
flag = true;
|
||
list.Add(string.Format("Row={0}, FieldName={1}, DataType={2}, VALUE={3}, ORIGINAL={4}", new object[]
|
||
{
|
||
i,
|
||
columns[i].ColumnName,
|
||
columns[i].DataType.Name,
|
||
text,
|
||
text2
|
||
}));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
bool flag7 = flag;
|
||
if (flag7)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public static bool IsDataRowChanged(DataRow dataRow)
|
||
{
|
||
DataRowState rowState = dataRow.RowState;
|
||
bool flag = rowState == DataRowState.Added || rowState == DataRowState.Deleted;
|
||
bool result;
|
||
if (flag)
|
||
{
|
||
result = true;
|
||
}
|
||
else
|
||
{
|
||
bool flag2 = rowState != DataRowState.Modified;
|
||
if (flag2)
|
||
{
|
||
result = false;
|
||
}
|
||
else
|
||
{
|
||
DataColumnCollection columns = dataRow.Table.Columns;
|
||
int count = columns.Count;
|
||
bool flag3 = false;
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
bool flag4 = dataRow[i, DataRowVersion.Original] == DBNull.Value;
|
||
if (flag4)
|
||
{
|
||
bool flag5 = dataRow[i] != DBNull.Value;
|
||
if (flag5)
|
||
{
|
||
flag3 = true;
|
||
break;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
bool flag6 = dataRow[i] == DBNull.Value;
|
||
if (flag6)
|
||
{
|
||
flag3 = true;
|
||
break;
|
||
}
|
||
bool flag7 = dataRow[i].ToString() != dataRow[i, DataRowVersion.Original].ToString();
|
||
if (flag7)
|
||
{
|
||
flag3 = true;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
result = flag3;
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static DataTable DataTableColumnLandscape(DataTable dt, string[] columns, string landscapeColumn, string valueColumn)
|
||
{
|
||
DataColumnCollection columns2 = dt.Columns;
|
||
int num = columns2.IndexOf(landscapeColumn);
|
||
bool flag = num < 0;
|
||
if (flag)
|
||
{
|
||
throw new Exception(string.Format("字段名{0}不存在。", landscapeColumn));
|
||
}
|
||
int num2 = columns2.IndexOf(valueColumn);
|
||
bool flag2 = num2 < 0;
|
||
if (flag2)
|
||
{
|
||
throw new Exception(string.Format("字段名{0}不存在。", valueColumn));
|
||
}
|
||
int[] array = null;
|
||
bool flag3 = columns != null;
|
||
if (flag3)
|
||
{
|
||
int num3 = columns.Length;
|
||
bool flag4 = num3 > 0;
|
||
if (flag4)
|
||
{
|
||
array = new int[num3];
|
||
for (int i = 0; i < num3; i++)
|
||
{
|
||
int num4 = columns2.IndexOf(columns[i]);
|
||
bool flag5 = num4 < 0;
|
||
if (flag5)
|
||
{
|
||
throw new Exception(string.Format("字段名{0}不存在。", columns[i]));
|
||
}
|
||
array[i] = num4;
|
||
}
|
||
}
|
||
}
|
||
return GPFunData.DataTableColumnLandscape(dt, array, num, num2);
|
||
}
|
||
|
||
public static DataTable DataTableColumnLandscape(DataTable dt, int[] commonColumns, int landscapeColumn, int valueColumn)
|
||
{
|
||
List<string> list = new List<string>();
|
||
bool flag = commonColumns == null;
|
||
if (flag)
|
||
{
|
||
List<int> list2 = new List<int>();
|
||
int i = 0;
|
||
int count = dt.Columns.Count;
|
||
while (i < count)
|
||
{
|
||
bool flag2 = i != landscapeColumn && i != valueColumn;
|
||
if (flag2)
|
||
{
|
||
list2.Add(i);
|
||
}
|
||
i++;
|
||
}
|
||
commonColumns = list2.ToArray();
|
||
}
|
||
int num = commonColumns.Length + 1;
|
||
string[] array = new string[num];
|
||
SortedDictionary<string, int> sortedDictionary = new SortedDictionary<string, int>();
|
||
foreach (DataRow dataRow in dt.Rows)
|
||
{
|
||
string text = dataRow[landscapeColumn].ToString();
|
||
bool flag3 = string.IsNullOrEmpty(text);
|
||
if (flag3)
|
||
{
|
||
text = "_";
|
||
}
|
||
bool flag4 = !sortedDictionary.ContainsKey(text);
|
||
if (flag4)
|
||
{
|
||
sortedDictionary[text] = 1;
|
||
}
|
||
}
|
||
DataTable dataTable = new DataTable();
|
||
int num2 = 0;
|
||
int[] array2 = commonColumns;
|
||
DataColumn dataColumn;
|
||
for (int j = 0; j < array2.Length; j++)
|
||
{
|
||
int index = array2[j];
|
||
dataColumn = new DataColumn();
|
||
string text2 = dt.Columns[index].ColumnName;
|
||
list.Add(text2);
|
||
dataColumn.ColumnName = text2;
|
||
dataColumn.DataType = dt.Columns[index].DataType;
|
||
dataTable.Columns.Add(dataColumn);
|
||
array[num2++] = text2;
|
||
}
|
||
string sort = string.Join(",", list.ToArray());
|
||
string columnName = dt.Columns[valueColumn].ColumnName;
|
||
array[num - 1] = columnName;
|
||
Type dataType = dt.Columns[valueColumn].DataType;
|
||
dataColumn = new DataColumn();
|
||
dataColumn.ColumnName = columnName;
|
||
dataColumn.DataType = dataType;
|
||
dataTable.Columns.Add(dataColumn);
|
||
string[] array3 = sortedDictionary.Keys.ToArray<string>();
|
||
int k = 0;
|
||
int num3 = array3.Length;
|
||
while (k < num3)
|
||
{
|
||
dataColumn = new DataColumn();
|
||
dataColumn.ColumnName = "f_" + array3[k];
|
||
dataColumn.DataType = dt.Columns[valueColumn].DataType;
|
||
sortedDictionary[array3[k]] = k + num;
|
||
dataTable.Columns.Add(dataColumn);
|
||
k++;
|
||
}
|
||
DataRow[] array4 = dt.Select("", sort);
|
||
DataRow dataRow2 = dataTable.NewRow();
|
||
int l = 0;
|
||
int num4 = array4.Length;
|
||
while (l < num4)
|
||
{
|
||
string text2 = array4[l][landscapeColumn].ToString();
|
||
bool flag5 = string.IsNullOrEmpty(text2);
|
||
if (flag5)
|
||
{
|
||
text2 = "_";
|
||
}
|
||
int columnIndex = sortedDictionary[text2];
|
||
object obj = array4[l][valueColumn];
|
||
bool flag6 = l == 0;
|
||
bool flag7 = flag6 || !GPFunData.IsSameDataRow(array4[l], array4[l - 1], commonColumns);
|
||
bool flag8 = flag7;
|
||
if (flag8)
|
||
{
|
||
dataRow2 = dataTable.NewRow();
|
||
GPFunData.CopyDataRow(array4[l], dataRow2, array);
|
||
dataTable.Rows.Add(dataRow2);
|
||
}
|
||
else
|
||
{
|
||
double num5 = GPFunctions.CDbl<object>(dataRow2[columnName]) + GPFunctions.CDbl<object>(obj);
|
||
dataRow2[columnName] = Convert.ChangeType(num5, dataType);
|
||
}
|
||
dataRow2[columnIndex] = obj;
|
||
l++;
|
||
}
|
||
return dataTable;
|
||
}
|
||
|
||
public static int DataTableSetCaptions(DataTable dataTable, string strCaptions)
|
||
{
|
||
Dictionary<string, string> dictionary = GPFunctions.StringToDict(strCaptions, ',', '=');
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
int num = 0;
|
||
foreach (DataColumn dataColumn in columns)
|
||
{
|
||
string columnName = dataColumn.ColumnName;
|
||
bool flag = dictionary.ContainsKey(columnName);
|
||
if (flag)
|
||
{
|
||
dataColumn.Caption = dictionary[columnName];
|
||
num++;
|
||
}
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public static DataTable CreateDataTable<T>(T t) where T : new()
|
||
{
|
||
DataTable dataTable = new DataTable("DataTable");
|
||
PropertyInfo[] properties = t.GetType().GetProperties();
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
PropertyInfo[] array = properties;
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = array[i];
|
||
string name = propertyInfo.Name;
|
||
DataColumn dataColumn = new DataColumn();
|
||
dataColumn.ColumnName = name;
|
||
dataColumn.DataType = propertyInfo.PropertyType;
|
||
string displayName = GPFunData.GetDisplayName(propertyInfo);
|
||
bool flag = !string.IsNullOrEmpty(displayName);
|
||
if (flag)
|
||
{
|
||
dataColumn.Caption = displayName;
|
||
}
|
||
columns.Add(dataColumn);
|
||
}
|
||
return dataTable;
|
||
}
|
||
|
||
public static ICollection<KeyValuePair<string, object>> GetDynamicClassByDataRow(DataRow dataRow)
|
||
{
|
||
object obj = new ExpandoObject();
|
||
ICollection<KeyValuePair<string, object>> collection = obj as ICollection<KeyValuePair<string, object>>;
|
||
foreach (DataColumn dataColumn in dataRow.Table.Columns)
|
||
{
|
||
string columnName = dataColumn.ColumnName;
|
||
KeyValuePair<string, object> item = new KeyValuePair<string, object>(columnName, dataRow[columnName]);
|
||
collection.Add(item);
|
||
}
|
||
return collection;
|
||
}
|
||
|
||
public static object CreatNewClassByDataRow(DataRow dataRow)
|
||
{
|
||
CSharpCodeProvider cSharpCodeProvider = new CSharpCodeProvider();
|
||
CompilerParameters compilerParameters = new CompilerParameters();
|
||
compilerParameters.GenerateExecutable = false;
|
||
compilerParameters.GenerateInMemory = true;
|
||
StringBuilder stringBuilder = new StringBuilder();
|
||
stringBuilder.Append("public class MyDynamicClass \n");
|
||
stringBuilder.Append("{\n");
|
||
DataColumnCollection columns = dataRow.Table.Columns;
|
||
foreach (DataColumn dataColumn in columns)
|
||
{
|
||
stringBuilder.Append(string.Format("\t{0} {1};", dataColumn.DataType.Name, dataColumn.ColumnName));
|
||
}
|
||
stringBuilder.Append("}");
|
||
CompilerResults compilerResults = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameters, new string[]
|
||
{
|
||
stringBuilder.ToString()
|
||
});
|
||
Assembly compiledAssembly = compilerResults.CompiledAssembly;
|
||
object obj = compiledAssembly.CreateInstance("MyDynamicClass");
|
||
Type type = obj.GetType();
|
||
foreach (DataColumn dataColumn2 in columns)
|
||
{
|
||
PropertyInfo property = type.GetProperty(dataColumn2.ColumnName);
|
||
bool flag = property != null && property.CanRead;
|
||
if (flag)
|
||
{
|
||
property.SetValue(obj, dataRow[dataColumn2.ColumnName], null);
|
||
}
|
||
}
|
||
return obj;
|
||
}
|
||
|
||
public static T DataRowToEntity<T>(DataRow dataRow) where T : class, new()
|
||
{
|
||
bool flag = dataRow == null;
|
||
T result;
|
||
if (flag)
|
||
{
|
||
result = default(T);
|
||
}
|
||
else
|
||
{
|
||
T t = Activator.CreateInstance<T>();
|
||
PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||
PropertyInfo[] array = properties;
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = array[i];
|
||
DataColumnCollection columns = dataRow.Table.Columns;
|
||
string name = propertyInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag2 = num >= 0 && dataRow[num] != DBNull.Value;
|
||
if (flag2)
|
||
{
|
||
object value = dataRow[num];
|
||
Type propertyType = propertyInfo.PropertyType;
|
||
bool flag3 = propertyType.IsValueType && propertyType != columns[num].DataType;
|
||
if (flag3)
|
||
{
|
||
value = Convert.ChangeType(value, propertyType);
|
||
}
|
||
propertyInfo.SetValue(t, value, null);
|
||
}
|
||
}
|
||
result = t;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static T ToEntity<T>(this DataRow dataRow) where T : class, new()
|
||
{
|
||
return GPFunData.DataRowToEntity<T>(dataRow);
|
||
}
|
||
|
||
public static T DataRowToEntityFields<T>(DataRow dataRow) where T : class, new()
|
||
{
|
||
bool flag = dataRow == null;
|
||
T result;
|
||
if (flag)
|
||
{
|
||
result = default(T);
|
||
}
|
||
else
|
||
{
|
||
T t = Activator.CreateInstance<T>();
|
||
FieldInfo[] fields = t.GetType().GetFields();
|
||
FieldInfo[] array = fields;
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
FieldInfo fieldInfo = array[i];
|
||
DataColumnCollection columns = dataRow.Table.Columns;
|
||
string name = fieldInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag2 = num >= 0 && dataRow[num] != DBNull.Value;
|
||
if (flag2)
|
||
{
|
||
object value = dataRow[num];
|
||
Type fieldType = fieldInfo.FieldType;
|
||
bool flag3 = fieldType.IsValueType && fieldType != columns[num].DataType;
|
||
if (flag3)
|
||
{
|
||
value = Convert.ChangeType(value, fieldType);
|
||
}
|
||
fieldInfo.SetValue(t, value);
|
||
}
|
||
}
|
||
result = t;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static DataTable ListToDataTable<T>(List<T> list) where T : new()
|
||
{
|
||
bool flag = list.Count == 0;
|
||
if (flag)
|
||
{
|
||
throw new Exception("ListToDataTable: no data!");
|
||
}
|
||
Type typeFromHandle = typeof(T);
|
||
T t = list[0];
|
||
DataTable dataTable = GPFunData.CreateDataTable<T>(t);
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
PropertyInfo[] properties = typeFromHandle.GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||
Dictionary<string, IndexType> dictionary = new Dictionary<string, IndexType>();
|
||
PropertyInfo[] array = properties;
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = array[i];
|
||
string name = propertyInfo.Name;
|
||
int num = columns.IndexOf(name);
|
||
bool flag2 = num >= 0;
|
||
if (flag2)
|
||
{
|
||
IndexType indexType;
|
||
indexType.index = num;
|
||
Type propertyType = propertyInfo.PropertyType;
|
||
bool flag3 = !propertyType.Equals(columns[num].DataType);
|
||
if (flag3)
|
||
{
|
||
indexType.type = 1;
|
||
}
|
||
else
|
||
{
|
||
indexType.type = 0;
|
||
}
|
||
dictionary.Add(name, indexType);
|
||
}
|
||
}
|
||
dataTable.BeginLoadData();
|
||
foreach (T current in list)
|
||
{
|
||
DataRow dataRow = dataTable.NewRow();
|
||
foreach (string current2 in dictionary.Keys)
|
||
{
|
||
IndexType indexType = dictionary[current2];
|
||
int index = indexType.index;
|
||
object obj = typeFromHandle.GetProperty(current2).GetValue(current, null);
|
||
bool flag4 = obj != null;
|
||
if (flag4)
|
||
{
|
||
bool flag5 = indexType.type == 1;
|
||
if (flag5)
|
||
{
|
||
obj = Convert.ChangeType(obj, columns[index].DataType);
|
||
}
|
||
dataRow[index] = obj;
|
||
}
|
||
}
|
||
dataTable.Rows.Add(dataRow);
|
||
}
|
||
dataTable.EndLoadData();
|
||
dataTable.AcceptChanges();
|
||
return dataTable;
|
||
}
|
||
|
||
public static bool DataTableDelete(DataTable table)
|
||
{
|
||
DataRowCollection rows = table.Rows;
|
||
int i = 0;
|
||
int count = rows.Count;
|
||
while (i < count)
|
||
{
|
||
DataRow dataRow = rows[i];
|
||
bool flag = dataRow.RowState != DataRowState.Deleted;
|
||
if (flag)
|
||
{
|
||
dataRow.Delete();
|
||
}
|
||
i++;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static int GetDataTableChangedCount(DataTable dataTable)
|
||
{
|
||
int num = 0;
|
||
bool flag = dataTable == null;
|
||
int result;
|
||
if (flag)
|
||
{
|
||
result = 0;
|
||
}
|
||
else
|
||
{
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
bool flag2 = dataRow.RowState != DataRowState.Unchanged;
|
||
if (flag2)
|
||
{
|
||
num++;
|
||
}
|
||
}
|
||
result = num;
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static bool DataTableCopy(DataTable dataTable, DataTable dtTarget, string[] columns)
|
||
{
|
||
Dictionary<int, int> dictionary = new Dictionary<int, int>();
|
||
int num = columns.Length;
|
||
DataColumnCollection columns2 = dataTable.Columns;
|
||
DataColumnCollection columns3 = dtTarget.Columns;
|
||
bool flag = true;
|
||
for (int i = 0; i < num; i++)
|
||
{
|
||
string text = columns[i];
|
||
int num2 = columns2.IndexOf(text);
|
||
int num3 = columns3.IndexOf(text);
|
||
bool flag2 = num2 < 0;
|
||
if (flag2)
|
||
{
|
||
throw new Exception(string.Format("DataTableCopy:字段{0}不在原DataTable中。", text));
|
||
}
|
||
bool flag3 = num3 < 0;
|
||
if (flag3)
|
||
{
|
||
throw new Exception(string.Format("DataTableCopy:字段{0}不在目标DataTable中。", text));
|
||
}
|
||
dictionary.Add(num2, num3);
|
||
bool flag4 = !columns2[num2].DataType.Equals(columns2[num3].DataType);
|
||
if (flag4)
|
||
{
|
||
flag = false;
|
||
}
|
||
}
|
||
DataRowCollection rows = dataTable.Rows;
|
||
foreach (DataRow dataRow in rows)
|
||
{
|
||
DataRow dataRow2 = dtTarget.NewRow();
|
||
bool flag5 = dataRow.RowState != DataRowState.Deleted;
|
||
if (flag5)
|
||
{
|
||
foreach (KeyValuePair<int, int> current in dictionary)
|
||
{
|
||
bool flag6 = flag;
|
||
if (flag6)
|
||
{
|
||
dataRow2[current.Value] = dataRow[current.Key];
|
||
}
|
||
else
|
||
{
|
||
dataRow2[current.Value] = Convert.ChangeType(dataRow[current.Key], columns3[current.Value].DataType);
|
||
}
|
||
}
|
||
}
|
||
dtTarget.Rows.Add(dataRow2);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static bool RemoveLongerItem(Dictionary<string, int> dic, int n)
|
||
{
|
||
string[] array = dic.Keys.ToArray<string>();
|
||
string[] array2 = array;
|
||
for (int i = 0; i < array2.Length; i++)
|
||
{
|
||
string key = array2[i];
|
||
bool flag = dic[key] >= n;
|
||
if (flag)
|
||
{
|
||
dic.Remove(key);
|
||
}
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static string StartsWithCode(Dictionary<string, int> dic, string codeStr)
|
||
{
|
||
string text = "";
|
||
foreach (KeyValuePair<string, int> current in dic)
|
||
{
|
||
bool flag = codeStr.StartsWith(current.Key);
|
||
if (flag)
|
||
{
|
||
bool flag2 = current.Key.Length > text.Length;
|
||
if (flag2)
|
||
{
|
||
text = current.Key;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return text;
|
||
}
|
||
|
||
public static void Swap<T>(ref T lhs, ref T rhs)
|
||
{
|
||
T t = lhs;
|
||
lhs = rhs;
|
||
rhs = t;
|
||
}
|
||
|
||
public static void SwapIfGreater<T>(ref T lhs, ref T rhs) where T : IComparable<T>
|
||
{
|
||
bool flag = lhs.CompareTo(rhs) > 0;
|
||
if (flag)
|
||
{
|
||
T t = lhs;
|
||
lhs = rhs;
|
||
rhs = t;
|
||
}
|
||
}
|
||
|
||
public static void SwapIfLess<T>(ref T lhs, ref T rhs) where T : IComparable<T>
|
||
{
|
||
bool flag = lhs.CompareTo(rhs) < 0;
|
||
if (flag)
|
||
{
|
||
T t = lhs;
|
||
lhs = rhs;
|
||
rhs = t;
|
||
}
|
||
}
|
||
|
||
public static List<string> StringToList(string source, string[] cs)
|
||
{
|
||
Dictionary<string, int> dictionary = new Dictionary<string, int>();
|
||
string[] array = source.Split(cs, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
for (int i = 0; i < array2.Length; i++)
|
||
{
|
||
string text = array2[i];
|
||
string text2 = text.Trim();
|
||
bool flag = !string.IsNullOrEmpty(text2);
|
||
if (flag)
|
||
{
|
||
bool flag2 = !dictionary.ContainsKey(text2);
|
||
if (flag2)
|
||
{
|
||
dictionary[text2] = 1;
|
||
}
|
||
else
|
||
{
|
||
dictionary[text2]++;
|
||
}
|
||
}
|
||
}
|
||
return dictionary.Keys.ToList<string>();
|
||
}
|
||
|
||
public static List<string> StringToList(string source, string deli)
|
||
{
|
||
string[] cs = new string[]
|
||
{
|
||
deli
|
||
};
|
||
return GPFunData.StringToList(source, cs);
|
||
}
|
||
|
||
public static string GetDisplayName(PropertyInfo pInfo)
|
||
{
|
||
Attribute[] array = (Attribute[])pInfo.GetCustomAttributes(typeof(DisplayNameAttribute), false);
|
||
bool flag = array.Length != 0;
|
||
string result;
|
||
if (flag)
|
||
{
|
||
DisplayNameAttribute displayNameAttribute = (DisplayNameAttribute)array[0];
|
||
result = displayNameAttribute.DisplayName;
|
||
}
|
||
else
|
||
{
|
||
result = "";
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static double SumColumnValue(DataTable dataTable, string columnName)
|
||
{
|
||
double num = 0.0;
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
num += GPFunctions.CDbl<object>(dataRow[columnName]);
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public static double SumColumnValue(DataTable dataTable, int colIndex)
|
||
{
|
||
double num = 0.0;
|
||
foreach (DataRow dataRow in dataTable.Rows)
|
||
{
|
||
num += GPFunctions.CDbl<object>(dataRow[colIndex]);
|
||
}
|
||
return num;
|
||
}
|
||
|
||
public static T Clone<T>(T RealObject)
|
||
{
|
||
T result;
|
||
using (Stream stream = new MemoryStream())
|
||
{
|
||
IFormatter formatter = new BinaryFormatter();
|
||
formatter.Serialize(stream, RealObject);
|
||
stream.Seek(0L, SeekOrigin.Begin);
|
||
result = (T)((object)formatter.Deserialize(stream));
|
||
}
|
||
return result;
|
||
}
|
||
|
||
public static T CloneEx<T>(T data) where T : new()
|
||
{
|
||
bool flag = data == null;
|
||
if (flag)
|
||
{
|
||
throw new NullReferenceException("CloneEx: data is null ");
|
||
}
|
||
Type type = data.GetType();
|
||
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||
T t = Activator.CreateInstance<T>();
|
||
Type typeFromHandle = typeof(T);
|
||
PropertyInfo[] array = properties;
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
PropertyInfo propertyInfo = array[i];
|
||
string name = propertyInfo.Name;
|
||
PropertyInfo property = typeFromHandle.GetProperty(name);
|
||
bool flag2 = property != null;
|
||
if (flag2)
|
||
{
|
||
property.SetValue(t, propertyInfo.GetValue(data, null), null);
|
||
}
|
||
}
|
||
return t;
|
||
}
|
||
|
||
public static string[] GetDataTableColumns(DataTable dataTable)
|
||
{
|
||
DataColumnCollection columns = dataTable.Columns;
|
||
int count = columns.Count;
|
||
string[] array = new string[count];
|
||
for (int i = 0; i < count; i++)
|
||
{
|
||
array[i] = columns[i].ColumnName;
|
||
}
|
||
return array;
|
||
}
|
||
}
|
||
}
|