2017-04-12 143 views
1

我需要在c#中做这样的事情。但在执行(对象)我有一个编译错误。C#类依赖注入

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 
    } 
} 

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

+0

你收到了什么编译错误? – EJoshuaS

+0

@EJoshuaS他正在传递一个'ParentClass',但他没有超载,需要一个'ParentClass'。 –

+0

@ScottChamberlain是的,我确实看到这一点,只是认为值得要求这篇文章包含完整的错误文本否则它对未来的读者来说会少许多) – EJoshuaS

回答

-1

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

看看在Liskov substitution principle - 如果你正确地完成您的实现,可以为ParentClass实例替代的实例,例如,class1,但反之不真正在所有。

可能性是,您不需要(或不想)重载。只需要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 
    } 
} 
+0

谢谢,是exacly我需要:) –

+0

顺便说一下,谁downvoted,为什么? – EJoshuaS

+0

@LuisRivasNoriega如果它解决了问题,你可以[接受答案](http://stackoverflow.com/help/accepted-answer)? – EJoshuaS

0

您需要使用一个接口这里

尝试改变ParentClass这样的:

public interface IParentClass{} 

然后让每个类的实现它,像这样:

public class class1 : IParentClass 
{ 
} 

public class class2 : IParentClass 
{ 
} 

然后在你的大师班,试试这个:

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(); 
} 
+0

谢谢,但我需要根据类的具体行为。例如。如果class1接着是DoMethod1(class1.Name),如果class2接着DoMethod2(class2.PhoneNumber); 这就是为什么我需要一个具体的方法foreach类 –

+0

我已经更新了我的答案,以显示如何为派生类定义方法。 HTH – geekzster

0

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

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(); 

    } 
} 
0

错误你看到的是你的class1,2,3对象的结果被投作为它们的父类,因为ExecutionMethod的签名( xxx) 而且没有一个Exec的重载方法,它将一类'ParentClass'作为参数。

也许最简单的方法是创建一个接口:

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

这样,才能防止方法调用期间丧气使用的接口。

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方法的一些功能 - 那么子类会必须重写该方法。