接口问题C#和依赖项注入

问题描述

我有问题。让我们来看一个例子: 您获得了将由Employee.cs和Owener.cs实现的接口:

public interface IEmployee
 {
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Location { get; set; }
  }

 public class Employee: IEmployee
 {
     public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Location { get; set; }
}

  public class Owner: IEmployee
{
    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string Location { get; set; }

    public string Status{ get; set; } <--- //problem string
}

现在,当我们使用依赖注入时,它返回员工或经理的对象,这就是我遇到问题的地方。

public class EmployeeCheck{

   private IEmployee empObj;

  public EmployeeCheck(IEmployee _em)
  {
     empObj=_em
  }

public void PrintCheck()
 {
   string str=_em.FirstName;
   string str2=(Owner)_emp.Status <--- //problem...how do I access it?? It can't be accessed cause 
                                       //IEMployee doesn't have status field!
  }

因此,基本上,如果我使用IEmployee作为接口,则无法访问新的Owner类中的字段,并且如果确实将它们放在接口中,则不需要实现它的Employee类将被强制执行实现不需要的东西!而且由于DI注入或其他设计模式,我确实需要IEmployee


好的,我不能使用抽象类...所以让我们讨论更多有关IStatus解决方案的信息...所以您正在谈论编写这样的代码:

public interface IStatus:IEmployee
{
    public string Title { get; set; }
}

公共类所有者:IEmployee,IStatus { 公共字符串FirstName {get;组; }

public string LastName { get; set; }

public string Location { get; set; }

public string Status{ get; set; } <--- //problem string

}

但是我该如何在“员工检查”类中使用它?

公共类EmployeeCheck {

   private IEmployee empObj;

  public EmployeeCheck(IEmployee _em,IStatus)
  {
 empObj=_em
  }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)