c#流畅的队列或列表接口

本文关键字:列表 接口 队列 | 更新日期: 2023-09-27 18:18:45

我有以下内容:

public interface IBehaviour
{
    event EventHandler Completed;
    void Execute();
}
public interface IBehaviourA : IBehaviour
{
    // Some specific stuff here
    object A { get; set; }
}
public interface IBehaviourB : IBehaviour
{
    // Some specific stuff here
    object B {get;set;}
}
public interface IBehaviourQueue
{
    void Run();
    BehaviourQueueItem<IBehaviour> AddBehaviour<T>() where T : IBehaviour;
}
public class BehaviourQueue : Queue<BehaviourQueueItem<IBehaviour>>,  IBehaviourQueue
{
    private IBehaviourFactory factory;
    public BehaviourQueue(IBehaviourFactory factory)
    {
        this.factory = factory;
    }
    public BehaviourQueueItem<IBehaviour> AddBehaviour<T>() where T:IBehaviour
    {
        T behaviour = factory.GetNew<T>();
        var queueItem = new BehaviourQueueItem<IBehaviour>(behaviour);
        Enqueue(queueItem); 
        return queueItem;
    }
    public void Run()
    {
        //Run each queue item
    }
}
public class BehaviourQueueItem<T>
{
    public IBehaviour behaviour;
    public BehaviourQueueItem(IBehaviour behaviour)
    {
        this.behaviour = behaviour;
    }
    public void WhenComplete(Func<T, bool> action) 
    {
        CompletedAction = action;
    }
    public BehaviourQueueItem<T> ConfigureFor<Z>(Action<Z> dow) where Z : IBehaviour
    {
        dow((Z)behaviour);
        return this;
    }
}

我可以这样写:

        var q =new BehaviourQueue(new BehaviourFactory());
        q
             .AddBehaviour<IBehaviourA>()
             .ConfigureFor<IBehaviourA>(x => x.A = "someValueA")
             .WhenComplete(x => DoStuffWithSomeProperty(((IBehaviourA)x).A));
        q
             .AddBehaviour<IBehaviourB >()
             .ConfigureFor<IBehaviourB >(x => x.B = "someValueB")
             .WhenComplete(x => DoStuffWithSomeProperty(((IBehaviourB)x).B));

我真正不喜欢的是我必须指定我每次引用的IBehaviour的类型。我希望能写:

        var q =new BehaviourQueue(new BehaviourFactory()); // Queue here is of IBehaviour
        q
             .AddBehaviour<IBehaviourA>()
             .Configure(x => x.A = "someValueA")
             .WhenComplete(x => DoStuffWithSomeProperty(x.A));
        q
             .AddBehaviour<IBehaviourB>()
             .Configure(x => x.B = "someValueB")
             .WhenComplete(x => DoStuffWithSomeProperty(x.B));

你知道我应该写些什么才能创建一个基本类型的列表,并添加特定的项目,并流畅地配置它吗?

Edit1:删除一些代码以避免混淆。我的最终目标是编写上面的代码。

多谢

c#流畅的队列或列表接口

改变这个,从那里开始

public interface IBehaviourQueue<T> where T : IBehaviour
{
    void Run();
    BehaviourQueueItem<T> AddBehaviour<T>() where T
}

使你的类类型特定,约束类型,然后从你的方法中删除类型。

public class BehaviourQueue<T> : Queue<BehaviourQueueItem<T>>,  IBehaviourQueue<T> where T : IBehaviour
{
    private IBehaviourFactory factory;
    public BehaviourQueue(IBehaviourFactory factory)
    {
        this.factory = factory;
    }
    public BehaviourQueueItem<T> AddBehaviour()
    {
        T behaviour = factory.GetNew<T>();
        var queueItem = new BehaviourQueueItem<T>(behaviour);
        Enqueue(queueItem); 
        return queueItem;
    }
    public void Run()
    {
        //Run each queue item
    }
}

编辑1:基于注释

public BehaviourQueueItem<T> ConfigureFor(Action<IBehaviour> dow)
{
    dow(behaviour);
    return this;
}

Edit2: more基于评论

public class BehaviourQueueItem<T>
{
    public T behaviour;
    public BehaviourQueueItem(T behaviour)
    {
        this.behaviour = behaviour;
    }
    public void WhenComplete(Func<T, bool> action)
    {
        CompletedAction = action;
    }
    public BehaviourQueueItem<T> ConfigureFor(Action<T> dow)
    {
        dow(behaviour);
        return this;
    }
    public Func<T, bool> CompletedAction { get; set; }
}