C# (Re)使用约束类型的泛型参数而不声明它们
这可能是不可能的,但我想检查一下这样的东西是否可以用一种简单的方式表达:C# (Re)使用约束类型的泛型参数而不声明它们,c#,generics,C#,Generics,这可能是不可能的,但我想检查一下这样的东西是否可以用一种简单的方式表达: // obviously doesn't work class Foo<T> : IFoo<T1,T2> where T: Bar<T1,T2> { // ... Baz<T1,T2> getBaz(){...} } //显然不起作用 Foo类:IFoo T:酒吧在哪里 { // ... Baz getBaz(){…} } 现在我声明了Foo,但我
// obviously doesn't work
class Foo<T> : IFoo<T1,T2>
where T: Bar<T1,T2>
{
// ...
Baz<T1,T2> getBaz(){...}
}
//显然不起作用
Foo类:IFoo
T:酒吧在哪里
{
// ...
Baz getBaz(){…}
}
现在我声明了Foo
,但我在语义上不喜欢它,获得约束的唯一方法是在Foo
的通用参数列表中列出Bar
,我希望减少冗余,而不是增加冗余
EDIT:
Baz
应该是返回类型,而不是方法名问题在于它没有定义T1和T2是什么
你可以做:
class Foo<T> : IFoo<int,string>
where T: Bar<int,string>
{
// ...
void Baz(){...}
// can call e.g. Qux<int, string>();
}
class Foo<T, T1, T2> : IFoo<T1,T2>
where T: Bar<T1,T2>
{
// ...
void Baz(){...}
// can call e.g. Qux<T1, T2>();
}
类Foo:IFoo
T:酒吧在哪里
{
// ...
void Baz(){…}
//可以调用,例如Qux();
}
你可以这样做:
class Foo<T> : IFoo<int,string>
where T: Bar<int,string>
{
// ...
void Baz(){...}
// can call e.g. Qux<int, string>();
}
class Foo<T, T1, T2> : IFoo<T1,T2>
where T: Bar<T1,T2>
{
// ...
void Baz(){...}
// can call e.g. Qux<T1, T2>();
}
类Foo:IFoo
T:酒吧在哪里
{
// ...
void Baz(){…}
//可以调用,例如Qux();
}
但事实上,没有办法知道T1和T2是什么(人类甚至猜不出你想做什么,所以编译器无法推断出来)。问题在于,它没有定义T1和T2是什么 你可以做:
class Foo<T> : IFoo<int,string>
where T: Bar<int,string>
{
// ...
void Baz(){...}
// can call e.g. Qux<int, string>();
}
class Foo<T, T1, T2> : IFoo<T1,T2>
where T: Bar<T1,T2>
{
// ...
void Baz(){...}
// can call e.g. Qux<T1, T2>();
}
类Foo:IFoo
T:酒吧在哪里
{
// ...
void Baz(){…}
//可以调用,例如Qux();
}
你可以这样做:
class Foo<T> : IFoo<int,string>
where T: Bar<int,string>
{
// ...
void Baz(){...}
// can call e.g. Qux<int, string>();
}
class Foo<T, T1, T2> : IFoo<T1,T2>
where T: Bar<T1,T2>
{
// ...
void Baz(){...}
// can call e.g. Qux<T1, T2>();
}
类Foo:IFoo
T:酒吧在哪里
{
// ...
void Baz(){…}
//可以调用,例如Qux();
}
但事实上,没有办法知道T1和T2是什么(人类甚至猜不到您要做什么,因此编译器无法推断它)。为给定泛型创建运行时类型所需的类型参数必须用泛型的类名在尖括号中声明:
class Foo<T, T1, T2> : IFoo<T1, T2>
where T: Bar<T1, T2>
{
//...
void Baz() {...}
}
类Foo:IFoo
T:酒吧在哪里
{
//...
void Baz(){…}
}
基本上,由于Foo
对于任何给定的x、y和z组合都是唯一的,因此它们都必须声明为“最上面”
注意,您不需要用Baz声明'T1,T2',但是,这些类型参数已经为类定义了。您可以在中找到更多信息,为给定泛型创建运行时类型所需的类型参数必须用泛型的类名在尖括号中声明:
class Foo<T, T1, T2> : IFoo<T1, T2>
where T: Bar<T1, T2>
{
//...
void Baz() {...}
}
类Foo:IFoo
T:酒吧在哪里
{
//...
void Baz(){…}
}
基本上,由于Foo
对于任何给定的x、y和z组合都是唯一的,因此它们都必须声明为“最上面”
注意,您不需要用Baz声明'T1,T2',但是,这些类型参数已经为类定义了。您可以在中找到更多信息。您可以围绕它创建一个包装类,但我怀疑这比您原来的解决方案要好:
public class FooWrapper<T1, T2>
{
public Foo<Bar<T1, T2>> FooObj;
public FooWrapper()
{
FooObj = new Foo<Bar<T1, T2>>();
}
public class Foo<T> : IFoo<T1, T2> where T : Bar<T1, T2>
{
// ...
public void Baz()
{
Type t1 = typeof(T1);
Type t2 = typeof(T2);
}
}
}
公共类FooWrapper
{
公共食品食品;
公共食品包装器()
{
FooObj=新的Foo();
}
公共类Foo:IFoo其中T:Bar
{
// ...
公共图书馆
{
类型t1=类型(t1);
t2型=t2型;
}
}
}
您可以围绕它创建一个包装类,但我怀疑这比您原来的解决方案要好:
public class FooWrapper<T1, T2>
{
public Foo<Bar<T1, T2>> FooObj;
public FooWrapper()
{
FooObj = new Foo<Bar<T1, T2>>();
}
public class Foo<T> : IFoo<T1, T2> where T : Bar<T1, T2>
{
// ...
public void Baz()
{
Type t1 = typeof(T1);
Type t2 = typeof(T2);
}
}
}
公共类FooWrapper
{
公共食品食品;
公共食品包装器()
{
FooObj=新的Foo();
}
公共类Foo:IFoo其中T:Bar
{
// ...
公共图书馆
{
类型t1=类型(t1);
t2型=t2型;
}
}
}
感谢您提出的富有想象力的解决方案…..我将把FooWrapper
构造函数重构为静态方法,但基本上就是这样:)感谢您提出的富有想象力的解决方案…..我将把FooWrapper
构造函数重构为静态方法,但基本上就是这样:)谢谢…..我在写问题时混淆了返回类型和方法名称,但这并没有显著改变其含义。谢谢…..我在写问题时混淆了返回类型和方法名称,但它并没有显著改变含义,虽然我是这么想的……T1和T2可以被隐式包含,但我想这只是一种过火的行为……但是,从哪里隐式地感谢你呢?这就是我所想的……T1和T2可以隐式包含,但我想这只是一个过火的举动……但从哪里含蓄地感谢你呢?