2011-12-13 79 views
1

我有一個類簡單的兩個屬性年齡名稱。然後,我有另一個類派生自人類類,它有一個額外的屬性FavCream。現在,我有這些類的兩個不同的實例說H1W1(派生類的女人)和I(基類的)只是想覆蓋與的W1所有派生屬性相應的值從h1如何重寫派生類繼承的屬性值與基類實例

但是,我沒有發現這個內置的。我不喜歡在Woman類中創建一個手動完成作業的公共方法。我認爲,我可以使用反射功能創建一個實用功能,但要尋找經過驗證的方法來處理這個問題。

這是不可能的嗎?在這種情況下,我們不能有任何繼承優勢嗎?或者這是不合邏輯的?

請耐心等待我。謝謝!

class Program 
    { 
     private static Woman _aWoman; 

     static void Main(string[] args) 
     { 
      var aHuman = new Human() { Age = 25, Name = "Ram" }; 
      Program._aWoman = new Woman() { Age = 22, FavCream = "Ponds" , Name = "Sita"}; 
      Console.WriteLine("Woman is " + Program._aWoman.ToString()); // prints Age = 22 Name = Sita FavCream = Ponds 
      Console.WriteLine("Human is " + aHuman.ToString()); // prints Age = 25 Name = Ram 

      **// Do something here so woman derived property has auto over written with property of human instance.** 

      Console.WriteLine("after overwriting, woman is " + _aWoman.ToString()); 
      // The result I m looking for here is "Age = 25 Name = Ram FavCream = Ponds" 
     } 
    } 

    internal class Human 
    { 
     public int Age { get; set; } 
     public string Name { get; set; } 

     public override string ToString() 
     { 
      return "Age = " + this.Age + " Name = " + this.Name; 
     } 
    } 

    internal class Woman: Human 
    { 
     public string FavCream { get; set; } 

     public override string ToString() 
     { 
      return base.ToString() + " FavCream = " + this.FavCream; 
     } 
    } 

UPDATE: 我關於創建方法結束,並且使用反射拷貝的基類的屬性。此外,我試圖用通用限制來增強這一點,但還沒有成功。

感謝大家這樣的快速回復。

// Trying to make it generic... but could not enforce contraint that T must be base class of 'this' 
     public void Overwrite<T>(T baseInstance) where T : Human 
     { 
      Type baseType = baseInstance.GetType(); 
      Type derivedType = this.GetType(); 
      PropertyInfo[] propInfos = null; 

      propInfos = baseType.GetProperties(); 

      foreach (PropertyInfo eachProp in propInfos) 
      { 
       PropertyInfo baseProp = (baseType).GetProperty(eachProp.Name); 
       object baseVal = baseProp.GetValue(baseInstance, null); 
       eachProp.SetValue(this, baseVal, null); 
      } 
     } 
+1

什麼,如果你有兩個實例人類h1和h2?你想從哪裏得到屬性? –

回答

0

您創建的Human類實例與Woman類實例完全分離。 很難看到你想要達到的目標,也許這是什麼?

w1.Age = h1.Age; 
w1.Name = h1.Name; 
+0

是的,但自動。所以,它可以在不同類型的類中重用。 – Santoo

2

是的,你可以使用反射 要提取的所有屬性做人力

// get all public static properties of MyClass type 
PropertyInfo[] propertyInfos; 
propertyInfos = typeof(Human).GetProperties(BindingFlags.Public | 
               BindingFlags.Static); 

可以使用的PropertyInfo的GetValue()和的SetValue()從人類獲取和設置它爲女性的方法。 看MSDN:的SetValue:http://msdn.microsoft.com/en-us/library/axt1ctd9.aspx 的GetValue:http://msdn.microsoft.com/en-us/library/b05d59ty.aspx

而是在運行時分配值的,我建議你重載隱性或顯性的操作,簡單地做一個鑄件從人類到女人。

Woman newWoman = (Woman)H1; 

將人類轉換爲女性的所有邏輯都將被轉儲到超載方法中。

0

你所要求的是不合邏輯的。即使你有兩個女人的例子w1和w2,也沒有將所有屬性從w1複製到w2的內建機制。

您將需要使用反射來實現這一點,但這是一個相當簡單的操作。

0

除了其他的答案,你可以簡單地寫在Human類中的一些功能,將複製的屬性你感興趣:

class Human 
{ 
    ... 

    public void CopyFrom(Human h) 
    { 
     this.Age = g.Age; 
     this.Name = g.Name; 
    } 
} 

其他解決方案可以使用automapper