2014-02-27 68 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

那么,您的复杂需求需要一个复杂的解决方案。请参阅编辑。 –