// Part 1
string result = CodeHelper.getCastedValue<string>(dataReader["name"]);
int myId = CodeHelper.getCastedValue<int>(dataReader["id"]);
int myId = CodeHelper.getCastedValue<int>(dataReader["id"], /*default value*/ -1);
// Part 2
int test = 5;
if(
CodeHelper.isValueInList<int>(
test,
1, 2, 3, 6, 5
)
) {
// TRUE CODE
}
else {
// FALSE CODE
}
// Part 3
object test = new DataSet();
if (
CodeHelper.isTypeInList<object>(
test,
typeof(DataTable), typeof(DataView), typeof(DataSet)
)
) {
// TRUE CODE
}
else {
// FALSE CODE
}
// Part 4
if(CodeHelper.getTopDataRow(DataSet) == null) {
// EMPTY DATASET
}
if (CodeHelper.getTopDataRow(DataTable) != null) {
// NOT EMPTY DATATABLE
}
هذه بعض الأكواد البرمجية التي أستخدمها لتسهيل التعامل مع أكواد السي شارب و أكواد ال في بي
إليكم الكود الكامل:
namespace HelpersLibrary {
public static class CodeHelper {
public static T getCastedValue<T>(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<T>(object obj, T defaultValue) {
T result = getCastedValue<T>(obj);
if (result.Equals(default(T)))
result = defaultValue;
return result;
}
public static bool isValueInList<T>(T value, params T[] checkList) {
return isValueInList(value, checkList.AsEnumerable());
}
public static bool isValueInList<T>(T value, IEnumerable<T> 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>(T value, params T[] checkList) {
return isValueInListParallel(value, checkList.AsEnumerable());
}
public static bool isValueInListParallel<T>(T value, IEnumerable<T> 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>(T value, params Type[] checkList) {
return isTypeInList(value, checkList.AsEnumerable());
}
public static bool isTypeInList<T>(T value, IEnumerable<Type> 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>(T value, params Type[] checkList) {
return isTypeInListParallel(value, checkList.AsEnumerable());
}
public static bool isTypeInListParallel<T>(T value, IEnumerable<Type> 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>(dataSet);
}
public static DataRow getTopDataRow(DataTable dataTable) {
return getTopDataRow<DataTable>(dataTable);
}
public static DataRow getTopDataRow(DataView dataView) {
return getTopDataRow<DataView>(dataView);
}
private static DataRow getTopDataRow<T>(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;
}
}
}