父類是
BLOject
裡面有
Key和EditState屬性
子類PrintSheet,PrintSheetPart都同時繼續左抽像類BLOject
AddDataWithMark(PrintSheet.parts, PrintSheetPart);
如果BLobject是接口,所有實現了此接口的類會用Add方法後都會成功把PrintSheetPart加入PrintSheet
但如果BLOject是抽像類,當調用添加更改方法時,由於子類轉父類後,雖然Key,EditState都會改變,但再用Add方法把PrintSheetPart加入PrintSheet是不會加到的。後來使用泛型涵數就可以解決到。
具體如下改變:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using HSCIS_SL.WCFService;
namespace HSCIS_SL
{
public class ArrayOperater
{
/*
/// <summary>
/// 數組中是否已存在記錄
/// </summary>
/// <param name="array"></param>
/// <param name="key"></param>
/// <returns></returns>
private static bool ItemIsInArray(List<BLObject> array, string key)
{
return GetItem(array, key) != null;
}
public static void Add(List<BLObject> array, BLObject value)
{
//if (!ItemIsInArray(array, value.Key))
//{
// List<BLObject> list = new List<BLObject>();
// foreach (BLObject item in array)
// {
// list.Add(item);
// }
// list.Add(value);
// array = list.ToArray();
//}
if (!ItemIsInArray(array, value.Key))
{
array.Add(value);
}
}
public static List<BLObject> ConvertList(IList list)
{
List<BLObject> rtn = new List<BLObject>();
foreach (BLObject item in list)
{
rtn.Add(item);
}
return rtn;
}
/// <remarks>把 Class存起來,並設置相關標識(新增,更改,刪除)</remarks>
public static bool AddDataWithMark(List<BLObject> array, BLObject value)
{
BLObject l_objItem;
try
{
l_objItem = GetItem(array, value.Key);
if (l_objItem == null) //000
{
//objUnit.DomainObject.EditState = (objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Insert);//標識為新增
value.EditState = (int)EditStateEnum.ES_Insert; //標識為新增
Add(array, value);
}
else
{
if ((l_objItem.EditState & (int)EditStateEnum.ES_Insert) <= 0) //**0
{
//if ((l_objItem.EditState & EditStateEnum.ES_Delete) > 0) //1**
//{
// Remove(ref array, l_objItem.Key); //如果被刪除的class不是新增的,將該class Remove
// //objUnit.DomainObject.EditState = objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Delete; //再將新增的標識為被刪除過(100)
// //objUnit.DomainObject.EditState = objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Insert; //標識為新增(101)
// value.EditState = EditStateEnum.ES_Update;
// Add(ref array, value);
//}
//else //000
//{
// Remove(ref array, l_objItem.Key);
// value.EditState = l_objItem.EditState | EditStateEnum.ES_Update; //標識為被更改過
// Add(ref array, value);
//}
Remove(array, l_objItem.Key); //如果被刪除的class不是新增的,將該class Remove
value.EditState = (int)EditStateEnum.ES_Update;
Add(array, value);
}
else //001/101
{
//新增完再新增不做動作
//value.DomainObject.EditState = (int)General.ObjectEditStateEnum.OE_Insert;
}
}
return true;
}
catch (Exception e)
{
throw e;
}
}
public static void Remove(List<BLObject> array, string key)
{
//List<BLObject> list = new List<BLObject>();
//foreach (BLObject item in array)
//{
// if (item.Key != key)
// {
// list.Remove(item);
// }
//}
//array = list.ToArray();
BLObject item = GetItem(array, key);
if (item != null)
array.Remove(item);
}
public static BLObject GetItem(List<BLObject> array, string key)
{
var result = array.Where(o => o.Key == key);
if (result.Count() > 0)
{
return result.First();
}
else
{
return null;
}
}
public static bool DeleteData(List<BLObject> array, string key)
{
BLObject l_objItem;
l_objItem = GetItem(array,key);
if (l_objItem == null)
{
return false;
}
if ((l_objItem.EditState & (int)EditStateEnum.ES_Insert) > 0 && (l_objItem.EditState & (int)EditStateEnum.ES_Delete) <= 0)
{
Remove(array,key); //如果要刪除的class是新增的,那麼將它Remove
}
//如果要刪除的class是刪除後再新增的,那麼就把它設為不是新增的,繼續標識為被刪除的
else if ((l_objItem.EditState & (int)EditStateEnum.ES_Insert) > 0 && (l_objItem.EditState & (int)EditStateEnum.ES_Delete) > 0)
{
l_objItem.EditState = l_objItem.EditState & (~(int)EditStateEnum.ES_Insert); //obj.blnNew = False
}
else
{
l_objItem.EditState = l_objItem.EditState | (int)EditStateEnum.ES_Delete; //如果要刪除的class不是新增的,那麼將它標識為被刪除的
}
return true;
}
public static int Count_WithDeleteState(List<BLObject> array)
{
int i=0;
//foreach (BLObject item in array)
//{
// if (ItemStateIsNotDelete(item))
// {
// i++;
// }
//}
i = array.Where(item => ItemStateIsNotDelete(item)).Count();
return i;
}
public static bool ItemStateIsNotDelete(BLObject item)
{
//不等 1** (1*1取外)
int intEditState = (int)EditStateEnum.ES_Delete | (int)EditStateEnum.ES_Insert;
if (((item.EditState & (int)EditStateEnum.ES_Delete) != (int)EditStateEnum.ES_Delete)
|| ((item.EditState & intEditState) == intEditState))
{
return true;
}
return false;
}
*/
/// <summary>
/// 數組中是否已存在記錄
/// </summary>
/// <param name="array"></param>
/// <param name="key"></param>
/// <returns></returns>
private static bool ItemIsInArray<T>(List<T> array, string key)
{
return GetItem(array, key) != null;
}
public static void Add<T>(List<T> array, T value)
{
//if (!ItemIsInArray(array, value.Key))
//{
// List<BLObject> list = new List<BLObject>();
// foreach (BLObject item in array)
// {
// list.Add(item);
// }
// list.Add(value);
// array = list.ToArray();
//}
if (!ItemIsInArray(array, (value as BLObject).Key))
{
array.Add(value);
}
}
public static List<BLObject> ConvertList(IList list)
{
List<BLObject> rtn = new List<BLObject>();
foreach (BLObject item in list)
{
rtn.Add(item);
}
return rtn;
}
/// <remarks>把 Class存起來,並設置相關標識(新增,更改,刪除)</remarks>
public static bool AddDataWithMark<T>(List<T> array, T value)
{
//BLObject l_objItem;
T l_objItem;
try
{
l_objItem = GetItem(array, (value as BLObject).Key);
if (l_objItem == null) //000
{
//objUnit.DomainObject.EditState = (objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Insert);//標識為新增
(value as BLObject).EditState = (int)EditStateEnum.ES_Insert; //標識為新增
Add(array, value);
}
else
{
if (((l_objItem as BLObject).EditState & (int)EditStateEnum.ES_Insert) <= 0) //**0
{
//if ((l_objItem.EditState & EditStateEnum.ES_Delete) > 0) //1**
//{
// Remove(ref array, l_objItem.Key); //如果被刪除的class不是新增的,將該class Remove
// //objUnit.DomainObject.EditState = objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Delete; //再將新增的標識為被刪除過(100)
// //objUnit.DomainObject.EditState = objUnit.DomainObject.EditState | (int)General.ObjectEditStateEnum.OE_Insert; //標識為新增(101)
// value.EditState = EditStateEnum.ES_Update;
// Add(ref array, value);
//}
//else //000
//{
// Remove(ref array, l_objItem.Key);
// value.EditState = l_objItem.EditState | EditStateEnum.ES_Update; //標識為被更改過
// Add(ref array, value);
//}
Remove(array, (l_objItem as BLObject).Key); //如果被刪除的class不是新增的,將該class Remove
(value as BLObject).EditState = (int)EditStateEnum.ES_Update;
Add(array, value);
}
else //001/101
{
//新增完再新增不做動作
//value.DomainObject.EditState = (int)General.ObjectEditStateEnum.OE_Insert;
}
}
return true;
}
catch (Exception e)
{
throw e;
}
}
public static void Remove<T>(List<T> array, string key)
{
//List<BLObject> list = new List<BLObject>();
//foreach (BLObject item in array)
//{
// if (item.Key != key)
// {
// list.Remove(item);
// }
//}
//array = list.ToArray();
T item = GetItem(array, key);
if (item != null)
array.Remove(item);
}
public static T GetItem<T>(List<T> array, string key)
{
T t = default(T);
foreach (var o in array)
{
if ((o as BLObject).Key == key)
{
t = o;
break;
}
}
return t;
//var result = array.Where(o => o.Key == key);
// if (result.Count() > 0)
// {
// return result.First();
// }
// else
// {
// return null;
// }
}
public static bool DeleteData<T>(List<T> array, string key)
{
//BLObject l_objItem;
T l_objItem;
l_objItem = GetItem(array, key);
if (l_objItem == null)
{
return false;
}
if (((l_objItem as BLObject).EditState & (int)EditStateEnum.ES_Insert) > 0 && ((l_objItem as BLObject).EditState & (int)EditStateEnum.ES_Delete) <= 0)
{
Remove(array, key); //如果要刪除的class是新增的,那麼將它Remove
}
//如果要刪除的class是刪除後再新增的,那麼就把它設為不是新增的,繼續標識為被刪除的
else if (((l_objItem as BLObject).EditState & (int)EditStateEnum.ES_Insert) > 0 && ((l_objItem as BLObject).EditState & (int)EditStateEnum.ES_Delete) > 0)
{
(l_objItem as BLObject).EditState = (l_objItem as BLObject).EditState & (~(int)EditStateEnum.ES_Insert); //obj.blnNew = False
}
else
{
(l_objItem as BLObject).EditState = (l_objItem as BLObject).EditState | (int)EditStateEnum.ES_Delete; //如果要刪除的class不是新增的,那麼將它標識為被刪除的
}
return true;
}
public static int Count_WithDeleteState(List<BLObject> array)
{
int i = 0;
//foreach (BLObject item in array)
//{
// if (ItemStateIsNotDelete(item))
// {
// i++;
// }
//}
i = array.Where(item => ItemStateIsNotDelete(item)).Count();
return i;
}
public static bool ItemStateIsNotDelete(BLObject item)
{
//不等 1** (1*1取外)
int intEditState = (int)EditStateEnum.ES_Delete | (int)EditStateEnum.ES_Insert;
if (((item.EditState & (int)EditStateEnum.ES_Delete) != (int)EditStateEnum.ES_Delete)
|| ((item.EditState & intEditState) == intEditState))
{
return true;
}
return false;
}
}
}