1private void button1_Click(object sender, RoutedEventArgs e)
2 {
3 MessageBox.Show(CreateStateRow<cinlapdemoDataSet.CustomersDataTable,cinlapdemoDataSet.CustomersRow>().RowState.ToString());
4 }
5
6 public static R CreateStateRow<T, R>(params NewRowState[] newRowState)
7 where T : DataTable
8 where R : DataRow
9 {
10 NewRowState nrs = NewRowState.Modified;
11
12 if (newRowState.Length > 1)
13 {
14 throw new Exception("创建行状态个数大于1");
15 }
16 else if (newRowState.Length == 1)
17 {
18 nrs = newRowState[0];
19 }
20
21 int oldRecord = 1;
22 int newRecord = 2;
23
24 if (nrs != NewRowState.Modified)
25 {
26 if (nrs == NewRowState.Added)
27 {
28 oldRecord = -1;
29 newRecord = 1;
30 }
31 else
32 {
33 oldRecord = 1;
34 newRecord = -1;
35 }
36 }
37
38 Type tableType = typeof(T);
39 Type rowType = typeof(R);
40
41 T dataTable = (T)tableType.Assembly.CreateInstance(tableType.FullName);
42
43 DataRow row = dataTable.NewRow();
44
45 FieldInfo fiOldRecord = rowType.GetField("oldRecord", BindingFlags.NonPublic | BindingFlags.Instance);
46 FieldInfo fiNewRecord = rowType.GetField("newRecord", BindingFlags.NonPublic | BindingFlags.Instance);
47
48 fiOldRecord.SetValue(row, oldRecord);
49 fiNewRecord.SetValue(row, newRecord);
50
51 return (R)row;
52 }
53
54 public enum NewRowState : int
55 {
56 Added = 0,
57 Modified = 1,
58 Deleted = 2
59 }
2 {
3 MessageBox.Show(CreateStateRow<cinlapdemoDataSet.CustomersDataTable,cinlapdemoDataSet.CustomersRow>().RowState.ToString());
4 }
5
6 public static R CreateStateRow<T, R>(params NewRowState[] newRowState)
7 where T : DataTable
8 where R : DataRow
9 {
10 NewRowState nrs = NewRowState.Modified;
11
12 if (newRowState.Length > 1)
13 {
14 throw new Exception("创建行状态个数大于1");
15 }
16 else if (newRowState.Length == 1)
17 {
18 nrs = newRowState[0];
19 }
20
21 int oldRecord = 1;
22 int newRecord = 2;
23
24 if (nrs != NewRowState.Modified)
25 {
26 if (nrs == NewRowState.Added)
27 {
28 oldRecord = -1;
29 newRecord = 1;
30 }
31 else
32 {
33 oldRecord = 1;
34 newRecord = -1;
35 }
36 }
37
38 Type tableType = typeof(T);
39 Type rowType = typeof(R);
40
41 T dataTable = (T)tableType.Assembly.CreateInstance(tableType.FullName);
42
43 DataRow row = dataTable.NewRow();
44
45 FieldInfo fiOldRecord = rowType.GetField("oldRecord", BindingFlags.NonPublic | BindingFlags.Instance);
46 FieldInfo fiNewRecord = rowType.GetField("newRecord", BindingFlags.NonPublic | BindingFlags.Instance);
47
48 fiOldRecord.SetValue(row, oldRecord);
49 fiNewRecord.SetValue(row, newRecord);
50
51 return (R)row;
52 }
53
54 public enum NewRowState : int
55 {
56 Added = 0,
57 Modified = 1,
58 Deleted = 2
59 }