to avoid confusion I summarised some code:
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
IManager<ISpecificEntity> specificManager = new SpecificEntityManager();
IManager<IIdentifier> manager = (IManager<IIdentifier>) specificManager;
manager.DoStuffWith(new SpecificEntity());
}
}
internal interface IIdentifier
{
}
internal interface ISpecificEntity : IIdentifier
{
}
internal class SpecificEntity : ISpecificEntity
{
}
internal interface IManager<TIdentifier> where TIdentifier : IIdentifier
{
void DoStuffWith(TIdentifier entity);
}
internal class SpecificEntityManager : IManager<ISpecificEntity>
{
public void DoStuffWith(ISpecificEntity specificEntity)
{
}
}
}
当我对代码进行抽打时,I在<代码>Main()上获得了英瓦利德CastException。
I know that ISpecificEntity
implements IIdentifier
.
But obviously a direct cast from an IManager<ISpecificEntity>
into an IManager<IIdentifier>
does not work.
我认为,与共体合作可做trick,但可修改<条码>IManager<TIdentifier> into IManager<in TIdentifier>
。
因此,有没有办法将<条码>具体编码>Manager输入<条码>IManager<IIdentifier>?
感谢一切最好。