2014-02-27 66 views
1

我在Windows 8 Phone應用程序的工作,我有兩件事情在這裏一個是庫項目和其他正常的應用程序,讓我先解釋一下我的代碼:重寫方法值

在圖書館項目

class A 
    { 
     public static string empName ="ABC"; 
     public static int empID = 123; 

     public virtual List<string> ListOfEmployees() 
     { 
      List<string> empList = new List<string> 
      empList.Add("Adam"); 
      empList.Add("Eve"); 
      return empList; 
     } 

} 

我在子項目中引用的庫項目,我的孩子和庫項目有兩種不同的解決方案。

在其中是每個項目的入口點每個孩子申請我們App.xaml.cs兒童應用

class Properties : A 
{ 

public void setValues(){ 
     empName ="ASDF" 
     ListOfEmployees(); 
} 
    public override List<string> ListOfEmployees() 
      { 
       List<string> empList = new List<string> 
       empList.Add("Kyla"); 
       empList.Add("Sophia"); 
       return empList; 
      } 
     } 

現在。

在這種App.xaml.cs文件我創造的這個Properties and calling setValues method.

什麼,我在這裏看到的只是靜態變量的值將被覆蓋,但該方法不overridden.Why這樣一個對象?我在這裏做錯了什麼?

我得到的ASDF和清單,亞當和夏娃作爲輸出

但我需要ASDF和清單,凱拉和索菲​​亞作爲輸出。

如何實現這一目標?

編輯

我是多麼使用這些值:

在我的基地:

class XYZ : A 

    { 
     // now i can get empName as weel as the ListOfEmployees() 
     string employeeName = null; 

     public void bind() 
     { 
     employeeName = empName ; 
     ListOfEmployees(); // here is the bug where i always get Adam and Eve and not the Kyla and sophia 
     } 
    } 

回答

0

變化override關鍵字新的,你會得到你後的行爲。查看this link瞭解何時使用哪些更多信息。

+0

我想,它仍然是相同的 – user2056563

+0

請看看我的編輯,我在我的問題表明 – user2056563

+0

代碼是一樣的,但要確保你嘗試用庫項目和正常的應用程序 – user2056563

1

現在我明白了,您想從您的項目庫中調用overriden值。

你不能用傳統的C#機制來做到這一點,因爲你需要依賴注入。沿着這些路線的東西:

// library 
public interface IA 
{ 
    List<string> ListOfEmployees(); 
} 

public class ABase : IA 
{ 
    public virtual List<string> ListOfEmployees() {} 
} 


public static class Repository 
{ 
    private static IA _a; 

    public static IA A 
    { 
     get { return _a = _a ?? new ABase(); } 
     set { _a = value; } 
    } 
} 

// in your app 

class Properties : ABase 
{ 
    public override List<string> ListOfEmployees() { /* ... */ } 
} 

Repository.A = new Properties(); 
+0

是的,在我的孩子的應用程序,我可以做到這一點,並給予實施,但在我的基地,我需要有默認impl – user2056563

+0

那麼,您的複雜需求需要一個複雜的解決方案。請參閱編輯。 –