我有以下内容:
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));
我真正不喜欢的是我必须指定我每次都指的是哪种类型的行为。
我希望能够写:
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:删除了一些代码以避免混淆。我的最终目标是编写以上代码。
非常感谢
最佳答案
改变它并从那里拿走
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
}
}
Edit1:基于评论
public BehaviourQueueItem<T> ConfigureFor(Action<IBehaviour> dow)
{
dow(behaviour);
return this;
}
Edit2:更多基于评论
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; }
}