【发布时间】:2013-07-05 18:54:29
【问题描述】:
具有受限界面的复杂访客场景:
public enum EDTypes { A1, A2 }
public interface IProcing { string doIt(string value); }
public interface IFooIni : IProcing { }
public interface IFooEnd : IProcing { }
public class FooIni_A1 : IFooIni { public string doIt(string value) { return "itsIni_A01"; } }
public class FooEnd_A1 : IFooEnd { public string doIt(string value) { return "itsEnd_A01"; } }
public class FooIni_A2 : IFooIni { public string doIt(string value) { return "itsIni_A02"; } }
public class FooEnd_A2 : IFooEnd { public string doIt(string value) { return "itsEnd_A02"; } }
public interface IFooSet<H, T> : IProcing
where H : IFooIni
where T : IFooEnd
{
H FooIni { get; set; }
List<IProcing> FooBar { get; set; }
T FooEnd { get; set; }
}
以及多个具体实现:
public class FooSet_A1 : IFooSet<FooIni_A1, FooEnd_A1>
{
public FooIni_A1 FooIni { get; set; }
public List<IProcing> FooBar { get; set; }
public FooEnd_A1 FooEnd { get; set; }
public string doIt(string value) { return "itsIni_FooSetA1"; }
}
public class FooSet_A2 : IFooSet<FooIni_A2, FooEnd_A2>
{
public FooIni_A2 FooIni { get; set; }
public List<IProcing> FooBar { get; set; }
public FooEnd_A2 FooEnd { get; set; }
public string doIt(string value) { return "itsIni_FooSetA2"; }
}
为什么做不到:
public class testfoo
{
private IFooSet<IFooIni, IFooEnd> getInstance(EDTypes type)
{
IFooSet<IFooIni, IFooEnd> res = null;
switch (type)
{
case EDTypes.A1:
/*
Unable to cast object of type
'_protoTest.FooSet_A1'
to type
'_protoTest.IFooSet`2[_protoTest.IFooIni,_protoTest.IFooEnd]'.
*/
res = (IFooSet<IFooIni, IFooEnd>)new FooSet_A1();
break;
case EDTypes.A2:
res = (IFooSet<IFooIni, IFooEnd>)new FooSet_A2();
break;
}
return res;
}
public void testIt()
{
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
IFooSet<IFooIni, IFooEnd> A1 = (IFooSet<IFooIni, IFooEnd>)getInstance(EDTypes.A1);
string x = A1.doIt("ASDFG");
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
}
}
有人here 告诉要在上面创建另一个接口,但我的工厂结果需要有问题的接口结构。
@Eldritch Conundrum 告诉我“T 上的协方差”并帮助我
很多了解正在发生的事情。
我还没有解决这个问题的办法,并且对截止日期发疯了
但我的问题是“为什么”会发生,他解释得很好。
放更多细节。标记为答案。
非常感谢
在@Eldritch Conundrum 的最后评论之后,我使用了动态关键字并解决了我的问题。
我失去了所有的智能,但现在它的工作!
谢谢@Eldritch 难题
有代码:
*public enum EDTypes { A1, A2 }
public interface IProcing { string doIt(string value); }
public interface IFooIni : IProcing { }
public interface IFooEnd : IProcing { }
public class FooIni_A1 : IFooIni { public string doIt(string value) { return "itsIni_A01"; } }
public class FooEnd_A1 : IFooEnd { public string doIt(string value) { return "itsEnd_A01"; } }
public class FooIni_A2 : IFooIni { public string doIt(string value) { return "itsIni_A02"; } }
public class FooEnd_A2 : IFooEnd { public string doIt(string value) { return "itsEnd_A02"; } }
public interface IFooSet<H, T> : IProcing
where H : IFooIni
where T : IFooEnd
{
H FooIni { get; set; }
List<IProcing> FooBar { get; set; }
T FooEnd { get; set; }
}
public class FooSet_A1 : IFooSet<FooIni_A1, FooEnd_A1>
{
public FooIni_A1 FooIni { get; set; }
public List<IProcing> FooBar { get; set; }
public FooEnd_A1 FooEnd { get; set; }
public string doIt(string value) { return "itsIni_FooSetA1"; }
}
public class FooSet_A2 : IFooSet<FooIni_A2, FooEnd_A2>
{
public FooIni_A2 FooIni { get; set; }
public List<IProcing> FooBar { get; set; }
public FooEnd_A2 FooEnd { get; set; }
public string doIt(string value) { return "itsIni_FooSetA2"; }
}
public class testfoo
{
private IProcing getInstance(EDTypes type)
{
dynamic res = null;
switch (type)
{
case EDTypes.A1:
res = new FooSet_A1();
break;
case EDTypes.A2:
res = new FooSet_A2();
break;
}
return res;
}
public void testIt()
{
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
dynamic A1 = getInstance(EDTypes.A1);
string s1 = A1.doIt("ASDFG");
dynamic A2 = getInstance(EDTypes.A2);
string s2 = A2.doIt("ASDFG");
// +*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
}
}*
【问题讨论】:
标签: c#-4.0 design-patterns generics casting type-constraints