C#类依赖注入

时间:2017-04-12 17:00:54

标签: c# reflection dependency-injection

我需要在c#中做这样的事情。但是在Exec(对象)中我遇到了编译错误。

public class ParentClass { }
public class class1 : ParentClass
{
}
public class class2 : ParentClass
{
}
public class class3 : ParentClass
{
}

public class MasterClass 
{
    public void ExecutionMethod(ParentClass myObject)
    {
        //some code
        Exec(myObject);
        //some code
    }

    public void Exec(class1 obj)
    {
        //some code
    }
    public void Exec(class2 obj)
    {
        //some code
    }
    public void Exec(class3 obj)
    {
        //some code
    }
}

我使用Reflection解决了,但我认为必须是一个更好的方法,有人可以给我一个好主意

5 个答案:

答案 0 :(得分:1)

正如@ScottChamberlain在评论中指出的那样,你没有任何方法采用ParentClass类型的参数。

查看Liskov substitution principle - 如果您已正确完成实施,则可以将class1的实例替换为ParentClass的实例,但是converse 是真的。

可能的是,你不需要(或想要)过载。只需让ParentClass成为一个抽象类,其中包含所有子类必须实现的抽象Execute方法,然后您可以直接在类上调用Execute而不必担心重载。更好的是,只需使ParentClass成为一个界面。 (顺便说一句,这有时称为Strategy Pattern。)

public interface IParent {
  void Execute();
}

public class class1 : ParentClass {
   //Execute method implementation
}

public class class2 : ParentClass {
   // ...
}

public class class3 : ParentClass {
  // ....
}

public class MasterClass 
{
    public void ExecutionMethod(IParent myObject)
    {
        //some code
        myObject.Execute();
        //some code
    }
}

答案 1 :(得分:1)

我建议你看一下面向对象的设计模式。具体来说,这个问题的策略模式。无论如何,你可以像这样实现你想要的东西:

public interface IParent 
{ 
    void Exec(); 
}
public class Child1 : IParent
{
    void Exec() { /*code*/ }
}
public class Child2 : IParent
{
    void Exec() { /*code*/ }
}
public class Child3 : IParent
{
    void Exec() { /*code*/ }
}

public class MasterClass 
{
    public void ExecutionMethod(IParent myObject)
    {
        //some code
        myObject.Exec();
        //some code
    }
}

如果您希望父类具有Exec方法的某些功能,您也可以使用抽象类而不是接口 - 那么子类必须覆盖该方法。

答案 2 :(得分:0)

您需要在此处使用界面

尝试像这样更改ParentClass

public interface IParentClass{}

然后让每个类实现它,如下所示:

public class class1 : IParentClass
{
}

public class class2 : IParentClass
{
}

然后在你的MasterClass中试试这个:

public void ExecutionMethod(IParentClass myObject)
{
    //some code
    Exec(myObject);
    //some code
}

public void Exec(IParentClass obj)
{
    //some code
}

然后您可以传入任何实现IParentClass接口的类。

现在作为一项增强功能 - 如果您希望IParentClass的每个实现都有一些可以在Exec方法中调用的方法和属性,请执行以下操作:

public interface IParentClass
{
    void DoTheThing();
}

这将强制您在派生类中使用此方法,例如,class1将如下所示:

public class class1 : IParentClass
{
    public void DoTheThing() 
    {
        // things get done...
    }
}
public class class2 : IParentClass
{
    public void DoTheThing() 
    {
        // things get done a different way...
    }
}

现在在你的Exec方法中,你可以这样调用:

public void Exec(IParentClass obj)
{
    obj.DoTheThing();
}

答案 3 :(得分:0)

您可以使用命令模式和依赖注入。我在下面给你一个想法。具体的实现将调用接收器上的执行(你逻辑去那里

public interface ICommand
{
    void Execute();
}

public class Command1 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}
public class Command2 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}
public class Command3 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}

public class CommandManager
{
    //you should use DI here to inject each concerete implementation of the command
    private Dictionary<string, ICommand> _commands;

    public CommandManager()
    {
        _commands = new Dictionary<string, ICommand>();
    }

    public void Execute(string key)
    {
        _commands[key].Execute();

    }
}

答案 4 :(得分:0)

您看到的错误是由于ExecutionMethod(xxx)的签名而导致您的class1,2,3对象被转换为其父类型。 并没有一个重写的Exec方法,它采用一种'ParentClass'作为参数。

可能最简单的方法是创建一个接口:

IDomainObject{}.
public class ParentClass : IDomainObject { }
public void ExecutionMethod(IDomainObject myObject)
    {        
        Exec(myObject);      
    }

以这种方式使用接口将阻止方法调用期间的向下转发。