日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

对象属性之间的相互赋值

發布時間:2024/1/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对象属性之间的相互赋值 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

(一)前言

當不同命名空間下的兩個類具有相同的屬性,并且需要進行相互賦值時,如下圖中的Jasen.Core.Info類的實例與Jasen.Core.Test.Info類的實例需要相互賦值時,按照一般的思路直接賦值就可以了。通常,這種情況在調用Web Service的時候比較常見。當需要轉換的類很多時,亦或者需要轉換的屬性很多時,我們就需要根據一定的規則來對這種場景來進行設計了,誰也不會傻布拉吉的一個一個屬性的去給對象賦值。

?

?

?(二)ObjectMapper?類負責對象之間相對應的屬性間的賦值

?1?///?<summary>
?2?????///?
?3?????///?</summary>
?4?????public?class?ObjectMapper??
?5?????{
?6?????????///?<summary>
?7?????????///?
?8?????????///?</summary>
?9?????????///?<param?name="sourceType"></param>
10?????????///?<param?name="targetType"></param>
11?????????///?<returns></returns>
12?????????public?static?IList<PropertyMapper>?GetMapperProperties(Type?sourceType,?Type?targetType)
13?????????{
14?????????????var?sourceProperties?=?sourceType.GetProperties();
15?????????????var?targetProperties?=?targetType.GetProperties();
16?
17?????????????return?(from?s?in?sourceProperties
18?????????????????????from?t?in?targetProperties
19?????????????????????where?s.Name?==?t.Name?&&?s.CanRead?&&?t.CanWrite?&&?s.PropertyType?==?t.PropertyType
20?????????????????????select?new?PropertyMapper
21???????????????????????????????{
22???????????????????????????????????SourceProperty?=?s,
23???????????????????????????????????TargetProperty?=?t
24???????????????????????????????}).ToList();
25?????????}???????
26?
27?????????///?<summary>
28?????????///?
29?????????///?</summary>
30?????????///?<param?name="source"></param>
31?????????///?<param?name="target"></param>
32?????????public?static?void?CopyProperties(object?source,?object?target)
33?????????{
34?????????????var?sourceType?=?source.GetType();
35?????????????var?targetType?=?target.GetType();
36?????????????var?mapperProperties?=?GetMapperProperties(sourceType,?targetType);
37?
38?????????????for?(int?index?=?0,count=mapperProperties.Count;?index?<?count;?index++)
39?????????????{
40?????????????????var?property?=?mapperProperties[index];
41?????????????????var?sourceValue?=?property.SourceProperty.GetValue(source,?null);
42?????????????????property.TargetProperty.SetValue(target,?sourceValue,?null);
43?????????????}
44?????????}
45?????}

?

public?static?void?CopyProperties(object?source,?object?target)方法將源對象的屬性值賦給目標對象的屬性。其中必須滿足以下條件:s.Name?==?t.Name?&&?s.CanRead?&&?t.CanWrite?&&?s.PropertyType?==?t.PropertyType,也就是兩個對象之間賦值的屬性名,屬性類型必須相同,而且源對象的屬性必須可讀,目標對象的屬性可寫。

?

public?static?IList<PropertyMapper>?GetMapperProperties(Type?sourceType,?Type?targetType)方法通過LINQ將源對象和目標對象相對應的屬性對放置在List<PropertyMapper>集合中,每一個PropertyMapper對應一個SourceProperty和TargetProperty。PropertyMapper類如下:

?1?///?<summary>
?2?????///?
?3?????///?</summary>
?4?????public?class?PropertyMapper
?5?????{
?6?????????///?<summary>
?7?????????///?
?8?????????///?</summary>
?9?????????public?PropertyInfo?SourceProperty
10?????????{
11?????????????get;
12?????????????set;
13?????????}
14?
15?????????///?<summary>
16?????????///?
17?????????///?</summary>
18?????????public?PropertyInfo?TargetProperty
19?????????{
20?????????????get;
21?????????????set;
22?????????}
23?????}

?

最后循環來遍歷List<PropertyMapper>集合,通過反射進行賦值,主要代碼如下:

???????????????? for?(int?index?=?0,count=mapperProperties.Count;?index?<?count;?index++)
39?????????????{
40?????????????????var?property?=?mapperProperties[index];
41?????????????????var?sourceValue?=?property.SourceProperty.GetValue(source,?null);
42?????????????????property.TargetProperty.SetValue(target,?sourceValue,?null);
43?????????????}

?(三)單元測試

?1???????? [TestMethod()]
?2?????????public?void?CopyPropertiesTest()
?3?????????{
?4?????????????Jasen.Core.Info?source?=?new?Jasen.Core.Info()
?5?????????????{
?6?????????????????Name?=?"jasen",
?7?????????????????CreateTime?=?"2011-3-31".AsDateTime(),
?8?????????????????Exist?=?true,
?9?????????????????ConflictOption?=?ConflictOption.OverwriteChanges
10?????????????};
11?????????????Info?target?=?new?Info();??
12?????????????ObjectMapper.CopyProperties(source,?target);
13?
14?????????????Assert.AreEqual(source.ConflictOption,target.ConflictOption);
15?????????????Assert.AreEqual(source.CreateTime,?target.CreateTime);
16?????????????Assert.AreEqual(source.Exist,?target.Exist);
17?????????????Assert.AreEqual(source.Name,?target.Name);
18?????????}

?

(四)其他

當然,當需要轉換的對象很多時,可以采用字典Dictionary來提高性能,Dictionary主要用來保存對象對應屬性間的List<PropertyMapper>關系,當下次再次調用時候就不需要再次獲取對象對應屬性間的List<PropertyMapper>關系了。

?

轉載于:https://www.cnblogs.com/jasenkin/archive/2011/03/31/copy_object_property_value_to_other_object_property.html

總結

以上是生活随笔為你收集整理的对象属性之间的相互赋值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。