C# 如何控制[相关通用接口]的实现
请先阅读第二版C# 如何控制[相关通用接口]的实现,c#,C#,请先阅读第二版 假设我们有两个依赖的通用接口: interface ITemplate1<T1, T2> where T1 : ITemplate1<T1, T2> where T2 : ITemplate2<T1, T2> { T2 t2 { get; set; } } interface ITemplate2<T1, T2> where T1 : ITemplate1<T1, T2> wh
假设我们有两个依赖的通用接口:
interface ITemplate1<T1, T2>
where T1 : ITemplate1<T1, T2>
where T2 : ITemplate2<T1, T2>
{
T2 t2 { get; set; }
}
interface ITemplate2<T1, T2>
where T1 : ITemplate1<T1, T2>
where T2 : ITemplate2<T1, T2>
{
T1 t1 { get; set; }
}
..防止将多个类声明为:
class MyClass1<T> where T : ITemplate1<Class1_1, Class2_1>
{
...
}
class MyClass2<T> where T : ITemplate1<Class1_2, Class2_2>
{
...
}
第二版: 我想我可以问一个更简单的问题,以免给亲爱的读者带来不便。如果我把你弄错了,我向你道歉。提出这样一个令人困惑的问题的原因是,真正的问题是完全复杂的。无论如何,以下是简单的形式: 标题: 如何控制接口的实现 说明:
我有一个实现两个依赖接口的类。我需要控制给定的类型,因为只有一些类型对彼此一致。-->第一版我不知道您是否正在搜索这个,但您可以使您的ClassX\y更通用,例如:
class Class1_x<T2> : ITemplate1<Class1_x<T2>, T2>
where T2 : ITemplate2<Class1_x<T2>, T2>
{
public T2 t2
{
get; set;
}
}
class Class2_x<T1> : ITemplate2<T1, Class2_x<T1>>
where T1 : ITemplate1<T1, Class2_x<T1>>
{
public T1 t1
{
get;
set;
}
}
class Class1\u x:ITemplate1
其中T2:2
{
公共t2t2
{
获得;设置;
}
}
类别2\u x:ITemplate2
其中T1:1
{
公共T1 T1
{
得到;
设置
}
}
或者像这样:
class Class1_xy<T1,T2> : ITemplate1<T1,T2>
where T2 : ITemplate2<T1,T2>
where T1 : ITemplate1<T1,T2>
{
public T2 t2
{
get; set; }
}
class Class2_xy<T1, T2> : ITemplate2<T1, T2>
where T2 : ITemplate2<T1, T2>
where T1 : ITemplate1<T1, T2>
{
public T1 t1
{ get; set; }
}
class ClassBoth_xy<T1, T2> : ITemplate1<T1,T2>, ITemplate2<T1, T2>
where T2 : ITemplate2<T1, T2>
where T1 : ITemplate1<T1, T2>
{
public T1 t1
{ get; set; }
public T2 t2
{ get; set; }
}
class Class1\u xy:ITemplate1
其中T2:2
其中T1:1
{
公共t2t2
{
get;set;}
}
类别2_xy:项目板2
其中T2:2
其中T1:1
{
公共T1 T1
{get;set;}
}
类ClassAll_xy:ITemplate1,ITemplate2
其中T2:2
其中T1:1
{
公共T1 T1
{get;set;}
公共t2t2
{get;set;}
}
但我看不出这其中的确切意义-也许你能给我们一些启示?使用类这两个_xy
确实是个好主意,但不能限制MyClass
由不正确的
对实现。我通过以下方式解决了这个问题:
class MyClass<T1, T2>
where T1 : ITemplate1<T1, T2>, new()
where T2 : ITemplate2<T1, T2>, new()
{
public MyClass()
{
T1 _t1 = new T1();
T2 _t2 = new T2();
bool isValid = (_t1 is Class1_1 && _t2 is Class2_1) ||
(_t1 is Class1_2 && _t2 is Class2_2);
if( !isValid )
throw new Exception("Inconsistent types have been used to instantiate MyClass.");
}
...
}
class-MyClass
其中T1:ITemplate1,new()
其中T2:ITemplate2,new()
{
公共MyClass()
{
T1 _T1=新的T1();
T2 _T2=新的T2();
bool isValid=(_t1为1类&&u t2为2类)||
(_t1为1类_2&&u t2为2类_2);
如果(!isValid)
抛出新异常(“已使用不一致的类型实例化MyClass。”);
}
...
}
Hi,您可以尝试类似MyClass的方法,其中T:ITemplate1并为T1、T2添加ITemplate1的约束,但我认为您会更早地达到泛型系统的限制—您与won Class1一起给出的最后一个示例。。。定义编译。。。那么问题是什么呢?问题是我们在C#中还没有或语句!哦,我明白了,但是为什么你不能用下面我的例子呢?如果您真的需要将其约束到任何一种情况,那么使用另一个接口或基类创建两个密封类?顺便说一句:我认为您永远不会在泛型约束中看到“或”语句-只要想想您可以对hi@Carsten做的所有恶作剧,谢谢,但我需要更多的限制,正如我在版本部分所描述的。嗨-你在那里失去了我-你可以马上做这个(也许我被所有的1_2,2_1,2_2搞糊涂了,…你能不能给出一些你不想实现的现实世界的例子?
class Class1_x<T2> : ITemplate1<Class1_x<T2>, T2>
where T2 : ITemplate2<Class1_x<T2>, T2>
{
public T2 t2
{
get; set;
}
}
class Class2_x<T1> : ITemplate2<T1, Class2_x<T1>>
where T1 : ITemplate1<T1, Class2_x<T1>>
{
public T1 t1
{
get;
set;
}
}
class Class1_xy<T1,T2> : ITemplate1<T1,T2>
where T2 : ITemplate2<T1,T2>
where T1 : ITemplate1<T1,T2>
{
public T2 t2
{
get; set; }
}
class Class2_xy<T1, T2> : ITemplate2<T1, T2>
where T2 : ITemplate2<T1, T2>
where T1 : ITemplate1<T1, T2>
{
public T1 t1
{ get; set; }
}
class ClassBoth_xy<T1, T2> : ITemplate1<T1,T2>, ITemplate2<T1, T2>
where T2 : ITemplate2<T1, T2>
where T1 : ITemplate1<T1, T2>
{
public T1 t1
{ get; set; }
public T2 t2
{ get; set; }
}
class MyClass<T1, T2>
where T1 : ITemplate1<T1, T2>, new()
where T2 : ITemplate2<T1, T2>, new()
{
public MyClass()
{
T1 _t1 = new T1();
T2 _t2 = new T2();
bool isValid = (_t1 is Class1_1 && _t2 is Class2_1) ||
(_t1 is Class1_2 && _t2 is Class2_2);
if( !isValid )
throw new Exception("Inconsistent types have been used to instantiate MyClass.");
}
...
}