简体   繁体   English

C#未定义的通用类列表?

[英]C# List of undefined generic classes?

I've a class like: 我有一个像这样的课程:

class GClass<T1, T2> {}

implementations: 实现:

class CClass1<String, String> {}
class CCLass2<String, Double> {}

On my C# code I'd need to define a object like this List<GClass<T1,?> . 在我的C#代码上,我需要定义一个类似List<GClass<T1,?>

Is it possible? 可能吗?

By other hand, I've tested with List<GClass<String, Object>> , however I can't add a object like this: 另一方面,我已经用List<GClass<String, Object>>进行了测试,但是我不能添加这样的对象:

List<GClass<String, Object>> list = new List<GClass<String, Object>>();
CClass1 c1 = new CClass1();
list.Add(c1);

Compiler dumps me an compilation error. 编译器会向我转储一个编译错误。

EDIT 编辑

I've tested with it: 我已经测试过了:

public interface IViz<T, TQuery, out TOut>
{
    Configuration.IVizConfiguration<T> VizConfiguration { get; }
    IList<TOut> SelectedValues { get; }

    IEnumerable<TQuery> generate(IEnumerable<T> search);
    Expression<Func<T, bool>> generateExpression(Object selectedVizValue);
    IEnumerable<Expression<Func<T, bool>>> generateExpressions(IEnumerable<Object> selectedVizValues);
}

However, compiler tells me: 但是,编译器告诉我:

Error 3 Invalid variance: The type parameter 'TOut' must be invariantly valid on 'UI.Forms.SearchTool.Visualization.IViz.SelectedValues'. 错误3无效方差:类型参数“ TOut”必须在“ UI.Forms.SearchTool.Visualization.IViz.SelectedValues”上始终有效。 'TOut' is covariant. “ TOut”是协变的。 D:\\projects\\living\\clients\\NetClient\\UI\\Forms\\SearchTool\\Visualization\\IViz.cs 8 42 UI D:\\ projects \\ living \\ clients \\ NetClient \\ UI \\ Forms \\ SearchTool \\ Visualization \\ IViz.cs 8 42 UI

You can accomplish this behaviour by adding an interface and use the generic modifier out on your type parameter, making it covariant. 您可以通过添加一个接口来完成此行为,使用通用的修饰out你的类型参数,使之协。

From documentation, out (Generic Modifier) : 文档中取出(通用修饰符)

For generic type parameters, the out keyword specifies that the type parameter is covariant. 对于泛型类型参数,out关键字指定类型参数是协变的。 You can use the out keyword in generic interfaces and delegates. 您可以在通用接口和委托中使用out关键字。

Covariance enables you to use a more derived type than that specified by the generic parameter. 通过协方差,您可以使用比通用参数指定的类型更多的派生类型。 This allows for implicit conversion of classes that implement variant interfaces and implicit conversion of delegate types. 这允许实现变体接口的类的隐式转换和委托类型的隐式转换。 Covariance and contravariance are supported for reference types, but they are not supported for value types. 引用类型支持协方差和协方差,但值类型不支持它们。

Example: 例:

public interface GInterface<T1, out T2> {}
public class GClass<T1, T2> : GInterface<T1, T2> {}

Usage: 用法:

List<GInterface<String, Object>> list = new List<GInterface<String, Object>>();
GClass<string, string> c1 = new GClass<string, string>();
list.Add(c1);

No, you cannot do that but you can define a common interface having only one of the generic types: 不,您不能这样做,但是可以定义仅具有一种通用类型的通用接口:

interface IClass<T> { }
class GClass<T1, T2> : IClass<T1> { }

class CClass1 : GClass<string, object> { }
class CClass2 : GClass<string, double> { }

Then you can create a list of the common interface: 然后,您可以创建公共接口的列表:

List<IClass<String>> list = new List<IClass<String>>();
list.Add(new CClass1());
list.Add(new CClass2());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM