using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace HelpersLibrary { public static class CodeHelper { public static T getCastedValue(object obj) { T result = default(T); if (obj != null && !Convert.IsDBNull(obj)) { Type normalType = typeof(T); Type nullableType = Nullable.GetUnderlyingType(normalType); if (nullableType != null) { result = (T)Convert.ChangeType(obj, nullableType); } else { result = (T)Convert.ChangeType(obj, normalType); } } return result; } public static T getCastedValue(object obj, T defaultValue) { T result = getCastedValue(obj); if (result.Equals(default(T))) result = defaultValue; return result; } public static bool isValueInList(T value, params T[] checkList) { return isValueInList(value, checkList.AsEnumerable()); } public static bool isValueInList(T value, IEnumerable checkList) { bool result = false; if (value != null && checkList != null) { foreach (T item in checkList) { if (item != null) { if (value.Equals(item)) { result = true; break; } } } } return result; } public static bool isValueInListParallel(T value, params T[] checkList) { return isValueInListParallel(value, checkList.AsEnumerable()); } public static bool isValueInListParallel(T value, IEnumerable checkList) { bool result = false; if (value != null && checkList != null) { Parallel.ForEach( checkList, () => false, (listItem, loopState, madeChange) => { if (listItem != null) { if (madeChange = value.Equals(listItem)) loopState.Break(); } return madeChange; }, (changed) => { if (changed) lock (checkList) { result = changed; } } ); } return result; } public static bool isTypeInList(T value, params Type[] checkList) { return isTypeInList(value, checkList.AsEnumerable()); } public static bool isTypeInList(T value, IEnumerable checkList) { bool result = false; if (value != null && checkList != null) { Type valueType = value.GetType(); foreach (Type item in checkList) { if (item != null) { if (valueType.IsAssignableFrom(item)) { result = true; break; } } } } return result; } public static bool isTypeInListParallel(T value, params Type[] checkList) { return isTypeInListParallel(value, checkList.AsEnumerable()); } public static bool isTypeInListParallel(T value, IEnumerable checkList) { bool result = false; if (value != null && checkList != null) { Type valueType = value.GetType(); Parallel.ForEach( checkList, () => false, (listItem, loopState, madeChange) => { if (listItem != null) { if (madeChange = valueType.IsAssignableFrom(listItem)) loopState.Break(); } return madeChange; }, (changed) => { if (changed) lock (checkList) { result = changed; } } ); } return result; } public static DataRow getTopDataRow(DataSet dataSet) { return getTopDataRow(dataSet); } public static DataRow getTopDataRow(DataTable dataTable) { return getTopDataRow(dataTable); } public static DataRow getTopDataRow(DataView dataView) { return getTopDataRow(dataView); } private static DataRow getTopDataRow(T data) { DataRow dataRow = null; if (data != null) { if ( isTypeInList( data, typeof(DataSet), typeof(DataTable), typeof(DataView) ) ) { if (data is DataSet) { DataSet dataSet = data as DataSet; if (dataSet.Tables.Count > 0) { if (dataSet.Tables[0].Rows.Count > 0) { dataRow = dataSet.Tables[0].Rows[0]; } } } else if (data is DataTable) { DataTable dataTable = data as DataTable; if (dataTable != null) { if (dataTable.Rows.Count > 0) { dataRow = dataTable.Rows[0]; } } } else if (data is DataView) { DataView dataView = data as DataView; if (dataView != null) { if (dataView.Table != null) { if (dataView.Table.Rows.Count > 0) { dataRow = dataView.Table.Rows[0]; } } } } } else { throw new InvalidCastException("Data parameter is not valid, you are only alowed to use: DataTable, DataSet and DataView"); } } return dataRow; } } }