背景
我不确定如何说出这个问题,所以这就是我想要实现的例子:
public interface IEverythingVM : IA, IB
{
MyTypeA {get;}
MyTypeB {get;}
MyTypeC {get;}
MyTypeD {get;}
MyTypeE {get;}
MyTypeF {get;}
}
public class EverythingVM : IEverythingVM
{
// Populate everything from MyTypeA to MyTypeF
}
public interface IA
{
MyTypeA {get;}
MyTypeB {get;}
MyTypeC {get;}
}
public interface IB
{
MyTypeD {get;}
MyTypeE {get;}
MyTypeF {get;}
}
实现:
我的印象是我可以做类似下面的事情,但是已经把它写出来了,这显然是为什么它不会起作用 - 我试图采取一些大的东西,将它弄清楚,然后重新调整它,这是一个如此荒谬的概念,它让我想大声笑出声。
{
IA varIA = new EverythingVM(param1, param2);
IB varIB = new EverythingVM(param1, param2);
var cm = new ComparisonManager(varIA, varIB);
}
public class ComparisonManager
{
public ComparisonManager(IEverythingVM varEIA, IEverythingVM varEIB)
{
// be able to acces MyTypeA, MyTypeB & MyTypeC from varEIA
// be able to acces MyTypeD, MyTypeE & MyTypeF from varEIB
}
}
问题:
最终我想要实现的是,在某些情况下,我想要MyTypeA
,MyTypeB
& MyTypeC
中提供了ComparisonManager
,而在其他情况下,我需要MyTypeD
,MyTypeE
& MyTypeF
中提供了ComparisonManager
。鉴于目前的结构,我有什么方法可以实现这一目标吗?
答案 0 :(得分:1)
你为什么不这样做:
public class ComparisonManager
{
public ComparisonManager(IA varEIA, IB varEIB) {}
}
答案 1 :(得分:1)
事实证明解决方案是:
public class ComparisonManager<T>
{
private readonly T _diskServiceVm;
private readonly T _panelServiceVm;
public ComparisonManager(T diskServiceVm, T panelServiceVm)
{
_diskServiceVm = diskServiceVm;
_panelServiceVm = panelServiceVm;
}
}
实现:
if (/* circumstance A */)
{
IA varIA = new A(varA, varB);
IA varIB = new B(varA, varB);
// this knows how to deal with MyTypeA, MyTypeB & MyTypeC
var comparisonManager = new ComparisonManager<IA>(varIA , varIB);
}
else
{
IB varIA = new A(varA, varB);
IB varIB = new B(varA, varB);
// this knows how to deal with MyTypeD, MyTypeE & MyTypeF
var comparisonManager = new ComparisonManager<IB>(varIA , varIB);
}