close

同事問了一個問題:

      假如有兩個物件(A和B),這兩個物件分屬不同類別,且大多數的屬性都一樣,但彼此沒有繼承關係,也不能修改原始類別的程式碼。要如何快速將物件A的屬性值全部複製給物件B??

思考了一下,由於功力不深,所以只想得出PropertyInfo類別。

 

首先定義ClassA及ClassB

public class ClassA {
    public string Str1 { get; set; }
    public string Str2 { get; set; }
    public string Str3 { get; set; }
    public string Str4 { get; set; }
    public string Str5 { get; set; }
    public string Str6 { get; set; }
    public string Str7 { get; set; }
    public string Str8 { get; set; }
    public string Str9 { get; set; }
    public string Str10 { get; set; }
    public string Str11 { get; set; }
    public string Str12 { get; set; }
    public string Str13 { get; set; }
    public string Str14 { get; set; }
    public string Str15 { get; set; }
    public string Str16 { get; set; }
    public string Str17 { get; set; }
    public string Str18 { get; set; }
    public string Str19 { get; set; }
    public string Str20 { get; set; }
    public string Str21 { get; set; }
    public string Str22 { get; set; }
    public string Str23 { get; set; }
    public string Str24 { get; set; }
    public string Str25 { get; set; }
    public string Str26 { get; set; }
    public string Str27 { get; set; }
    public string Str28 { get; set; }
    public string Str29 { get; set; }
    public string Str30 { get; set; }
    public string Str31 { get; set; }
    public string Str32 { get; set; }
    public string Str33 { get; set; }
    public string Str34 { get; set; }
    public string Str35 { get; set; }
    public string Str36 { get; set; }
    public string Str37 { get; set; }
    public string Str38 { get; set; }
    public string Str39 { get; set; }
    public string Str40 { get; set; }
    public string Str41 { get; set; }
    public string Str42 { get; set; }
    public string Str43 { get; set; }
    public string Str44 { get; set; }
    public string Str45 { get; set; }
    public string Str46 { get; set; }
    public string Str47 { get; set; }
    public string Str48 { get; set; }
    public string Str49 { get; set; }
    public string Str50 { get; set; }
    public string Str51 { get; set; }
    public string Str52 { get; set; }
    public string Str53 { get; set; }
    public string Str54 { get; set; }
    public string Str55 { get; set; }
    public string Str56 { get; set; }
    public string Str57 { get; set; }
    public string Str58 { get; set; }
    public string Str59 { get; set; }
    public string Str60 { get; set; }
    public string Str61 { get; set; }
    public string Str62 { get; set; }
    public string Str63 { get; set; }
    public string Str64 { get; set; }
    public string Str65 { get; set; }
    public string Str66 { get; set; }
    public string Str67 { get; set; }
    public string Str68 { get; set; }
    public string Str69 { get; set; }
    public string Str70 { get; set; }
    public string Str71 { get; set; }
    public string Str72 { get; set; }
    public string Str73 { get; set; }
    public string Str74 { get; set; }
    public string Str75 { get; set; }
    public string Str76 { get; set; }
    public string Str77 { get; set; }
    public string Str78 { get; set; }
    public string Str79 { get; set; }
    public string Str80 { get; set; }
    public string Str81 { get; set; }
    public string Str82 { get; set; }
    public string Str83 { get; set; }
    public string Str84 { get; set; }
    public string Str85 { get; set; }
    public string Str86 { get; set; }
    public string Str87 { get; set; }
    public string Str88 { get; set; }
    public string Str89 { get; set; }
    public string Str90 { get; set; }
    public string Str91 { get; set; }
    public string Str92 { get; set; }
    public string Str93 { get; set; }
    public string Str94 { get; set; }
    public string Str95 { get; set; }
    public string Str96 { get; set; }
    public string Str97 { get; set; }
    public string Str98 { get; set; }
    public string Str99 { get; set; }
    public string Str100 { get; set; }
}
 
public class ClassB {
    public string Str1 { get; set; }
    public string Str2 { get; set; }
    public string Str3 { get; set; }
    public string Str4 { get; set; }
    public string Str5 { get; set; }
    public string Str6 { get; set; }
    public string Str7 { get; set; }
    public string Str8 { get; set; }
    public string Str9 { get; set; }
    public string Str10 { get; set; }
    public string Str11 { get; set; }
    public string Str12 { get; set; }
    public string Str13 { get; set; }
    public string Str14 { get; set; }
    public string Str15 { get; set; }
    public string Str16 { get; set; }
    public string Str17 { get; set; }
    public string Str18 { get; set; }
    public string Str19 { get; set; }
    public string Str20 { get; set; }
    public string Str21 { get; set; }
    public string Str22 { get; set; }
    public string Str23 { get; set; }
    public string Str24 { get; set; }
    public string Str25 { get; set; }
    public string Str26 { get; set; }
    public string Str27 { get; set; }
    public string Str28 { get; set; }
    public string Str29 { get; set; }
    public string Str30 { get; set; }
    public string Str31 { get; set; }
    public string Str32 { get; set; }
    public string Str33 { get; set; }
    public string Str34 { get; set; }
    public string Str35 { get; set; }
    public string Str36 { get; set; }
    public string Str37 { get; set; }
    public string Str38 { get; set; }
    public string Str39 { get; set; }
    public string Str40 { get; set; }
    public string Str41 { get; set; }
    public string Str42 { get; set; }
    public string Str43 { get; set; }
    public string Str44 { get; set; }
    public string Str45 { get; set; }
    public string Str46 { get; set; }
    public string Str47 { get; set; }
    public string Str48 { get; set; }
    public string Str49 { get; set; }
    public string Str50 { get; set; }
    public string Str51 { get; set; }
    public string Str52 { get; set; }
    public string Str53 { get; set; }
    public string Str54 { get; set; }
    public string Str55 { get; set; }
    public string Str56 { get; set; }
    public string Str57 { get; set; }
    public string Str58 { get; set; }
    public string Str59 { get; set; }
    public string Str60 { get; set; }
    public string Str61 { get; set; }
    public string Str62 { get; set; }
    public string Str63 { get; set; }
    public string Str64 { get; set; }
    public string Str65 { get; set; }
    public string Str66 { get; set; }
    public string Str67 { get; set; }
    public string Str68 { get; set; }
    public string Str69 { get; set; }
    public string Str70 { get; set; }
    public string Str71 { get; set; }
    public string Str72 { get; set; }
    public string Str73 { get; set; }
    public string Str74 { get; set; }
    public string Str75 { get; set; }
    public string Str76 { get; set; }
    public string Str77 { get; set; }
    public string Str78 { get; set; }
    public string Str79 { get; set; }
    public string Str80 { get; set; }
    public string Str81 { get; set; }
    public string Str82 { get; set; }
    public string Str83 { get; set; }
    public string Str84 { get; set; }
    public string Str85 { get; set; }
    public string Str86 { get; set; }
    public string Str87 { get; set; }
    public string Str88 { get; set; }
    public string Str89 { get; set; }
    public string Str90 { get; set; }
 
    public int Str91 { get; set; }
    public int Str92 { get; set; }
    public int Str93 { get; set; }
    public int Str94 { get; set; }
    public int Str95 { get; set; }
    public int Str96 { get; set; }
    public int Str97 { get; set; }
    public int Str98 { get; set; }
    public int Str99 { get; set; }
    public int Str100 { get; set; }
}

 

 

 

接著產生物件A、物件B


ClassA a = new ClassA() {
            Str1 = "Str1",
            Str2 = "Str2",
            Str3 = "Str3",
            Str4 = "Str4",
            Str5 = "Str5",
            Str6 = "Str6",
            Str7 = "Str7",
            Str8 = "Str8",
            Str9 = "Str9",
            Str10 = "Str10",
            Str11 = "Str11",
            Str12 = "Str12",
            Str13 = "Str13",
            Str14 = "Str14",
            Str15 = "Str15",
            Str16 = "Str16",
            Str17 = "Str17",
            Str18 = "Str18",
            Str19 = "Str19",
            Str20 = "Str20",
            Str21 = "Str21",
            Str22 = "Str22",
            Str23 = "Str23",
            Str24 = "Str24",
            Str25 = "Str25",
            Str26 = "Str26",
            Str27 = "Str27",
            Str28 = "Str28",
            Str29 = "Str29",
            Str30 = "Str30",
            Str31 = "Str31",
            Str32 = "Str32",
            Str33 = "Str33",
            Str34 = "Str34",
            Str35 = "Str35",
            Str36 = "Str36",
            Str37 = "Str37",
            Str38 = "Str38",
            Str39 = "Str39",
            Str40 = "Str40",
            Str41 = "Str41",
            Str42 = "Str42",
            Str43 = "Str43",
            Str44 = "Str44",
            Str45 = "Str45",
            Str46 = "Str46",
            Str47 = "Str47",
            Str48 = "Str48",
            Str49 = "Str49",
            Str50 = "Str50",
            Str51 = "Str51",
            Str52 = "Str52",
            Str53 = "Str53",
            Str54 = "Str54",
            Str55 = "Str55",
            Str56 = "Str56",
            Str57 = "Str57",
            Str58 = "Str58",
            Str59 = "Str59",
            Str60 = "Str60",
            Str61 = "Str61",
            Str62 = "Str62",
            Str63 = "Str63",
            Str64 = "Str64",
            Str65 = "Str65",
            Str66 = "Str66",
            Str67 = "Str67",
            Str68 = "Str68",
            Str69 = "Str69",
            Str70 = "Str70",
            Str71 = "Str71",
            Str72 = "Str72",
            Str73 = "Str73",
            Str74 = "Str74",
            Str75 = "Str75",
            Str76 = "Str76",
            Str77 = "Str77",
            Str78 = "Str78",
            Str79 = "Str79",
            Str80 = "Str80",
            Str81 = "Str81",
            Str82 = "Str82",
            Str83 = "Str83",
            Str84 = "Str84",
            Str85 = "Str85",
            Str86 = "Str86",
            Str87 = "Str87",
            Str88 = "Str88",
            Str89 = "Str89",
            Str90 = "Str90",
            Str91 = "Str91",
            Str92 = "Str92",
            Str93 = "Str93",
            Str94 = "Str94",
            Str95 = "Str95",
            Str96 = "Str96",
            Str97 = "Str97",
            Str98 = "Str98",
            Str99 = "Str99",
            Str100 = "Str100"
        };
 
        ClassB b = new ClassB();

 

 

 

最後就是複製的動作了


Type typeB = typeof(ClassB);
PropertyInfo[] propertiesA = typeof(ClassA).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo infoA in propertiesA) {
    string name = infoA.Name;
    PropertyInfo infoB = typeB.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
    if (infoB != null && infoA.PropertyType == infoB.PropertyType) {
        infoB.SetValue(b, infoA.GetValue(a, null), null);
    }
}

 

 

 

結果

Copy_Properties

 

其實Reflection命名空間還蠻神奇的,它甚至連物件內的private變數也能進行修改。只是一直沒時間好好研究它......

arrow
arrow
    全站熱搜
    創作者介紹

    AnferneeHardaway 發表在 痞客邦 留言(0) 人氣()