0

What I want is simple:

public class Collection<T1,...TN> where T1...TN : SomeClass {
    public static Type[] types = {typeof(T1)...typeof(TN)}
}

How one can generate such base classes for Ns from 1 to N (say default is 40)?

I ended up with something like this .tt template:

<#@ template hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>

using System;
using System.Collections.Generic;
using System.Text;

namespace Services
{

    public interface ICollections {
        public Type[] Types {get;}
    }

<#
int top = 10;
for (int i = 0; i<=top; i++)

{ #>
    public class Collections< <# for (int j = 0; j<=i; j++) {#> T<#=j#> <#= (j<i  )?(","):(" ") #> <# }#> > : ICollections <# for (int j = 0; j<=i; j++) {#> 
    where T<#=j#> : SomeClass <# }#>
    {
        public Type[] Types => new[] { <# for (int j = 0; j<=i; j++) {#> typeof(T<#=j#>) <#= (j<i  )?(","):(" ") #> <# }#>};
    }
<# } #>
}
DuckQueen
  • 772
  • 10
  • 62
  • 134
  • Not sure it will help but you can see this : [variadic-templates-in-c-sharp](https://stackoverflow.com/questions/6844890/simulate-variadic-templates-in-c-sharp) – Pritom Mar 31 '20 at 19:55
  • In theory, declare 40 classes with a varying number of generic parameters. In practice, don't do it. – Lasse V. Karlsen Mar 31 '20 at 19:55
  • @LasseV.Karlsen: why do not do it? – DuckQueen Mar 31 '20 at 20:17
  • I believe you will stress processes such as ngen, and I believe there has to be better solutions to whatever problem these types are to help with solving. – Lasse V. Karlsen Apr 01 '20 at 08:16
  • You seem to have fixed it compile time. Is your question about creating the classes run time? If so - I dare say it cannot be done. Please correct me if I am wrong. – LosManos Apr 01 '20 at 15:14

0 Answers0