我想要的很简单:
public class Collection<T1,...TN> where T1...TN : SomeClass {
public static Type[] types = {typeof(T1)...typeof(TN)}
}
如何为Ns从1到N生成这样的基类(例如默认值为40)?
我最终得到了这样的.tt模板:
<#@ 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 )?(","):(" ") #> <# }#>};
}
<# } #>
}
您似乎已经确定了编译时间。
您是否对创建类运行时有疑问?如果是这样-我敢说这是不可能的。如果我错了,请纠正我。