1
廣義方法我有以下代碼...不能通過派生類接受通用基礎類
using System;
public interface ICommand<in TArgs>
where TArgs : BaseArgs
{
void Execute();
void Execute(TArgs args);
}
public class BaseArgs
{ }
public abstract class BaseCommand<TArgs> : ICommand<TArgs>
where TArgs : BaseArgs
{
public void Execute()
{
var args = this.CreateArgs();
this.Execute(args);
}
public void Execute(TArgs args)
{
this.GetData(args);
}
protected abstract void GetData(TArgs args);
protected abstract TArgs CreateArgs();
}
public class ActualArgs : BaseArgs
{ }
public class ActualCommand : BaseCommand<ActualArgs>
{
protected override void GetData(ActualArgs args)
{
var messenger = new Messenger(this.Execute);
var m2 = new Messenger2(this);
}
protected override ActualArgs CreateArgs()
{
return new ActualArgs();
}
}
public class Messenger
{
public Messenger(Action caller)
{
caller();
}
}
public class Messenger2
{
public Messenger2(BaseCommand<BaseArgs> caller)
{
caller.Execute();
}
}
理想我想使用Messenger2
但ActualCommand
行抱怨說,它不能投ActualCommand<ActualArgs>
到BaseCommand<BaseArgs>
,我真的不想申報Messenger2
作爲
new Messenger2<ActualCommand<ActualArgs>>(this)
是'公衆Messenger2(BaseCommand調用者)'可以接受嗎? –
slawekwin
不,因爲我會有很多從BaseCommand派生的命令,每個命令都會有自己的Args對象 –