Array of a generic class with unspecified type

南楼画角 提交于 2019-12-01 03:44:58

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.

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.

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.

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

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

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.)

Vinnyq12

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!