如何在C#中基于层次结构接口初始化对象

鲁皮什

我有一个接口ICommandHandler <TCommand>,其中TCommand的类型为ICommand(另一个接口)。SomeWorkProcessCommandHandler类实现ICommandHandler。

我无法做的是将SomeWorkProcessCommandHandler类传递给另一个期望在其构造函数中使用ICommandHandler的类

在下面的代码中,Manager类在其构造函数中期望ICommandHandler <TCommand>。传递someWorkProcessCommandHandler对象时出现编译时错误。如果我强制转换,则会出现运行时异常。

如何在构造函数中传递someWorkProcessCommandHandler对象?

请不要告诉我将其从构造函数中删除,并在Execute方法中找出处理程序。我不是要这么做。我要在这里说明的唯一一件事是,如何传递具有层次结构接口的对象

    public interface ICommand
    {
    }

    public interface ICommandHandler<TCommand> where TCommand : ICommand
    {
        void Handle(TCommand command);
    }

    public class SomeWorkProcessCommand : ICommand
    {
    }

    public class SomeWorkProcessCommandHandler : ICommandHandler<SomeWorkProcessCommand>
    {
        public void Handle(SomeWorkProcessCommand command)
        {
            Console.WriteLine("Some work Process Command Handler ");
        }
    }

    public class Manager 
    {
        private readonly ICommandHandler<ICommand> handler;

        public Manager(ICommandHandler<ICommand> handler)
        {
            this.handler = handler;
        }

        public void Execute(ICommand command)
        {

            handler.Handle(command);
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var someWorkProcessCommandHandler = new SomeWorkProcessCommandHandler();
            someWorkProcessCommandHandler.Handle(new SomeWorkProcessCommand());// This line works fine if the below code is not present

            //var manager = new Manager(someWorkProcessCommandHandler);  //Compile time error
            var manager = new Manager((ICommandHandler<ICommand>)someWorkProcessCommandHandler); //Runtime error after casting
            manager.Execute(new SomeWorkProcessCommand());

            Console.ReadKey();
        }
    }
坦维尔·优素福

问题出在您的Manager类设计中。您也应该在那里使用泛型。这是更新的代码,它可以工作:

public interface ICommand
{
}

public interface ICommandHandler<TCommand> where TCommand : ICommand
{
    void Handle(TCommand command);
}

public class SomeWorkProcessCommand : ICommand
{
}

public class SomeWorkProcessCommandHandler : ICommandHandler<SomeWorkProcessCommand>
{
    public void Handle(SomeWorkProcessCommand command)
    {
        Console.WriteLine("Some work Process Command Handler ");
    }
}

public class Manager<TCommandHandler, TCommand>
    where TCommandHandler : ICommandHandler<TCommand>
    where TCommand : ICommand
{
    private readonly TCommandHandler handler;

    public Manager(TCommandHandler handler)
    {
        this.handler = handler;
    }

    public void Execute(TCommand command)
    {
        handler.Handle(command);
    }
}


class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello World!");

        var someWorkProcessCommandHandler = new SomeWorkProcessCommandHandler();
        someWorkProcessCommandHandler.Handle(new SomeWorkProcessCommand());// This line works fine if the below code is not present

        var manager = new Manager<SomeWorkProcessCommandHandler, SomeWorkProcessCommand>(someWorkProcessCommandHandler);  //Works now
        manager.Execute(new SomeWorkProcessCommand());

        Console.ReadKey();
    }
}

希望它能解决您的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章