Array of a generic class with unspecified type

前端 未结 8 814
长发绾君心
长发绾君心 2021-01-11 18:32

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

ShaderParam<>[] params = new ShaderParam<&g         


        
8条回答
  •  孤独总比滥情好
    2021-01-11 18:50

    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.

提交回复
热议问题