有時(shí)候,需要把一個(gè)類的相同名稱的字段、屬性相互賦值,難道要一句一句代碼的寫?在數(shù)據(jù)庫操作的模型中,視圖模型和表模型有一定的字段是同名的,很多時(shí)候可能需要從視圖模型復(fù)制到表模型,或者反過來。。。 重復(fù)勞動(dòng)是比較郁悶,我又比較懶。所以寫了個(gè)轉(zhuǎn)換器:
|
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Collections;
namespace SilverlightApplication1 { /// <summary> /// 類屬性/字段的值復(fù)制工具 /// </summary> public class ClassValueCopier { /// <summary> /// 復(fù)制 /// </summary> /// <param name="destination">目標(biāo)</param> /// <param name="source">來源</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int Copy(object destination, object source) { if (destination == null || source == null) { return 0; } return Copy(destination, source, source.GetType()); } /// <summary> /// 復(fù)制 /// </summary> /// <param name="destination">目標(biāo)</param> /// <param name="source">來源</param> /// <param name="type">復(fù)制的屬性字段模板</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int Copy(object destination, object source, Type type) { return Copy(destination, source, type, null); } /// <summary> /// 復(fù)制 /// </summary> /// <param name="destination">目標(biāo)</param> /// <param name="source">來源</param> /// <param name="type">復(fù)制的屬性字段模板</param> /// <param name="excludeName">排除下列名稱的屬性不要復(fù)制</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int Copy(object destination, object source, Type type, IEnumerable<string> excludeName) { if (destination == null || source == null) { return 0; } if (excludeName == null) { excludeName = new List<string>(); } int i = 0; Type desType = destination.GetType(); foreach (FieldInfo mi in type.GetFields()) { if (excludeName.Contains(mi.Name)) { continue; } try { FieldInfo des = desType.GetField(mi.Name); if (des != null && des.FieldType == mi.FieldType) { des.SetValue(destination, mi.GetValue(source)); i++; } } catch { } } foreach (PropertyInfo pi in type.GetProperties()) { if (excludeName.Contains(pi.Name)) { continue; } try { PropertyInfo des = desType.GetProperty(pi.Name); if (des != null && des.PropertyType == pi.PropertyType && des.CanWrite && pi.CanRead) { des.SetValue(destination, pi.GetValue(source, null), null); i++; } } catch { //throw ex; } } return i; } } #region 擴(kuò)展方法 For .NET 3.0+ /// <summary> /// 類屬性/字段的值復(fù)制工具 擴(kuò)展方法 /// </summary> public static class ClassValueCopier2 { /// <summary> /// 獲取實(shí)體類的屬性名稱 /// </summary> /// <param name="source">實(shí)體類</param> /// <returns>屬性名稱列表</returns> public static List<string> GetPropertyNames(this object source) { if (source == null) { return new List<string>(); } return GetPropertyNames(source.GetType()); } /// <summary> /// 獲取類類型的屬性名稱(按聲明順序) /// </summary> /// <param name="source">類類型</param> /// <returns>屬性名稱列表</returns> public static List<string> GetPropertyNames(this Type source) { return GetPropertyNames(source, true); } /// <summary> /// 獲取類類型的屬性名稱 /// </summary> /// <param name="source">類類型</param> /// <param name="declarationOrder">是否按聲明順序排序</param> /// <returns>屬性名稱列表</returns> public static List<string> GetPropertyNames(this Type source, bool declarationOrder) { if (source == null) { return new List<string>(); } var list = source.GetProperties().AsQueryable(); if (declarationOrder) { list = list.OrderBy(p => p.MetadataToken); } return list.Select(o => o.Name).ToList(); ; } /// <summary> /// 從源對(duì)象賦值到當(dāng)前對(duì)象 /// </summary> /// <param name="destination">當(dāng)前對(duì)象</param> /// <param name="source">源對(duì)象</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int CopyValueFrom(this object destination, object source) { return CopyValueFrom(destination, source, null); } /// <summary> /// 從源對(duì)象賦值到當(dāng)前對(duì)象 /// </summary> /// <param name="destination">當(dāng)前對(duì)象</param> /// <param name="source">源對(duì)象</param> /// <param name="excludeName">排除下列名稱的屬性不要復(fù)制</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int CopyValueFrom(this object destination, object source, IEnumerable<string> excludeName) { if (destination == null || source == null) { return 0; } return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName); } /// <summary> /// 從當(dāng)前對(duì)象賦值到目標(biāo)對(duì)象 /// </summary> /// <param name="source">當(dāng)前對(duì)象</param> /// <param name="destination">目標(biāo)對(duì)象</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int CopyValueTo(this object source, object destination) { return CopyValueTo(destination, source, null); } /// <summary> /// 從當(dāng)前對(duì)象賦值到目標(biāo)對(duì)象 /// </summary> /// <param name="source">當(dāng)前對(duì)象</param> /// <param name="destination">目標(biāo)對(duì)象</param> /// <param name="excludeName">排除下列名稱的屬性不要復(fù)制</param> /// <returns>成功復(fù)制的值個(gè)數(shù)</returns> public static int CopyValueTo(this object source, object destination, IEnumerable<string> excludeName) { if (destination == null || source == null) { return 0; } return ClassValueCopier.Copy(destination, source, source.GetType(), excludeName); } } #endregion }
|
說明:
1,只能復(fù)制相同名稱,相同類型的字段、屬性 2,可以選擇只復(fù)制某個(gè)父類、接口下的字段和屬性 3,如果你的 .NET 版本不是 3.0及以上,得刪掉擴(kuò)展方法和 Linq 的引用
|