未指定类型的泛型类的数组

发布于 2024-10-21 05:37:27 字数 195 浏览 9 评论 0原文

在 C# 中是否可以创建未指定泛型类型的数组?大概是这样的:

ShaderParam<>[] params = new ShaderParam<>[5];
params[0] = new ShaderParam<float>();

或者由于 C# 的强类型,这根本不可能?

Is it possible in C# to create an array of unspecified generic types? Something along the lines of this:

ShaderParam<>[] params = new ShaderParam<>[5];
params[0] = new ShaderParam<float>();

Or is this simply not possible due to C#'s strong typing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

回首观望 2024-10-28 05:37:27

这是不可能的。在泛型类型在您的控制之下的情况下,您可以创建一个非泛型基本类型,例如,

ShaderParam[] params = new ShaderParam[5]; // Note no generics
params[0] = new ShaderParam<float>(); // If ShaderParam<T> extends ShaderParam

我的猜测是,这是您无法控制的 XNA 类型。

It's not possible. In the case where the generic type is under your control, you can create a non-generic base type, e.g.

ShaderParam[] params = new ShaderParam[5]; // Note no generics
params[0] = new ShaderParam<float>(); // If ShaderParam<T> extends ShaderParam

My guess is that this is an XNA type you have no control over though.

允世 2024-10-28 05:37:27

可以ShaderParam使用协变接口:

interface IShaderParam<out T> { ... }
class ShaderParam<T> : IShaderParam<T> { ... }

用法:

IShaderParam<object>[] parameters = new IShaderParam<object>[5];
parameters[0] = new ShaderParam<string>(); // <- note the string!

但是您不能将其与float等值类型一起使用的例子中。协方差仅对引用类型有效(例如 my 示例中的 string)。如果类型参数出现在逆变位置(例如作为方法参数),您也不能使用它。尽管如此,了解这项技术可能还是有好处的。

You could use a covariant interface for ShaderParam<T>:

interface IShaderParam<out T> { ... }
class ShaderParam<T> : IShaderParam<T> { ... }

Usage:

IShaderParam<object>[] parameters = new IShaderParam<object>[5];
parameters[0] = new ShaderParam<string>(); // <- note the string!

But you can't use it with value types like float in your example. Covariance is only valid with reference types (like string in my example). You also can't use it if the type parameter appears in contravariant positions, e.g. as method parameters. Still, it might be good to know about this technique.

还不是爱你 2024-10-28 05:37:27

那没有意义。

如果您编写 params[3] 会发生什么?
会是什么类型呢?

您可能想要创建一个非泛型基类或接口并使用它们的数组。

That wouldn't make sense.

What would happen if you write params[3]?
What type would it be?

You may want to create a non-generic base class or interface and use an array of them.

哭泣的笑容 2024-10-28 05:37:27

这在定义 T 的通用类通用扩展方法中是可能的:

ShaderParam<T>[] params = new ShaderParam<T>[5];

This is possible in a generic class or a generic extension method where T is defined:

ShaderParam<T>[] params = new ShaderParam<T>[5];
南薇 2024-10-28 05:37:27

不,对于实例化类型而言,ShaderParam 概念是没有意义的。换句话说,具体的 ShaderParam 不是 ShaderParam 的实例。因此,声明的数组类型对于保存该实例是非法的。 (除此之外,它本身就已经是非法语法了。)

No, the concept ShaderParam<> is meaningless as far as an instantiated type is concerned. In other words, a concrete ShaderParam<float> is not an instance of ShaderParam<>. Therefore, the declared type of the array would be illegal for holding that instance. (Above and beyond the fact that it's already illegal syntax to begin with.)

亢潮 2024-10-28 05:37:27

不直接。封闭泛型是幕后的特定“类型”,因此即使是开放泛型也太笼统,无法成为“强类型”。

我能想到的最好的解决方案(我之前使用过)是为泛型类创建一个非泛型祖先。该解决方案是否有效取决于您计划使用该阵列做什么。

Not directly. A closed generic is a specific "type" behind the scenes, so even an open generic is too general to be a "strong type".

The best solution I can think of, which I've used before, is to create a non-generic ancestor to the generic class. Whether that solution will function depends on what you plan to use the array for.

夏天碎花小短裙 2024-10-28 05:37:27

创建未指定泛型类型的数组:

Object[] ArrayOfObjects = new Object[5];
ArrayOfObjects[0] = 1.1;
ArrayOfObjects[1] = "Hello,I am an Object";
ArrayOfObjects[2] = new DateTime(1000000);

System.Console.WriteLine(ArrayOfObjects[0]);
System.Console.WriteLine(ArrayOfObjects[1]);
System.Console.WriteLine(ArrayOfObjects[2]);

如果您需要对对象执行某些特定于类型的操作,您可以使用反射

希望这会有所帮助。

create an array of unspecified generic types:

Object[] ArrayOfObjects = new Object[5];
ArrayOfObjects[0] = 1.1;
ArrayOfObjects[1] = "Hello,I am an Object";
ArrayOfObjects[2] = new DateTime(1000000);

System.Console.WriteLine(ArrayOfObjects[0]);
System.Console.WriteLine(ArrayOfObjects[1]);
System.Console.WriteLine(ArrayOfObjects[2]);

If you need to perform some type specific actions on the object you could use Reflection

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文