C#檢測pc光驅裡能否拔出了光盤的辦法。本站提示廣大學習愛好者:(C#檢測pc光驅裡能否拔出了光盤的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#檢測pc光驅裡能否拔出了光盤的辦法正文
本文實例講述了C#完成應用泛型將DataSet轉為Model的辦法。分享給年夜家供年夜家參考。詳細以下:
由於網站須要用C#開辟,習氣了java的泛型,所以看了一下C#下,也能夠如許做,隨意寫了一個。
public static List<T> PutAllVal<T>(T entity, DataSet ds) where T : new() {
List<T> lists = new List<T>();
if (ds.Tables[0].Rows.Count > 0) {
foreach (DataRow row in ds.Tables[0].Rows) {
lists.Add(PutVal(new T(),row));
}
}
return lists;
}
public static T PutVal<T>(T entity, DataRow row) where T : new() {
//初始化 假如為null
if (entity == null){
entity = new T();
}
//獲得類型
Type type = typeof(T);
//獲得屬性聚集
PropertyInfo[] pi = type.GetProperties();
foreach (PropertyInfo item in pi){
//給屬性賦值
if (row[item.Name] != null && row[item.Name] != DBNull.Value) {
if (item.PropertyType == typeof(System.Nullable<System.DateTime>)) {
item.SetValue(entity, Convert.ToDateTime(row[item.Name].ToString()), null);
} else {
item.SetValue(entity, Convert.ChangeType(row[item.Name], item.PropertyType), null);
}
}
}
return entity;
}
願望本文所述對年夜家的C#法式設計有所贊助。