C#泛型约束:结构数组

C#泛型约束:结构数组,c#,arrays,generics,value-type,generic-constraints,C#,Arrays,Generics,Value Type,Generic Constraints,我想创建一个泛型约束,该约束包含一个值类型(结构)数组的类型,类似于: public class X<T> where T : struct[] 公共类X,其中T:struct[] 或许 public class X<T, U> where U : struct where T : U[] 公共类X 其中U:struct 其中T:U[] 但这不起作用。似乎System.Array不能用作类型约束 那么-如何将泛型参数约束为结构数组 第一次回答后更新

我想创建一个泛型约束,该约束包含一个值类型(结构)数组的类型,类似于:

public class X<T> where T : struct[]
公共类X,其中T:struct[]
或许

public class X<T, U>
    where U : struct
    where T : U[]
公共类X
其中U:struct
其中T:U[]
但这不起作用。似乎System.Array不能用作类型约束

那么-如何将泛型参数约束为结构数组

第一次回答后更新:

public interface IDeepCopyable<T>
{
    T DeepCopy(T t);
}


public class DeepCopyArrayOfValueTypes<T> : IDeepCopyable<T>
    where T : is an array of value types
{
    T Copy(T t) {...}
}
公共接口IDeepCopyable
{
T深拷贝(T);
}
公共类DeepCopyArrayOfValueTypes:IDeepCopyable
其中T:是值类型的数组
{
T拷贝(T){…}
}
您不需要这样做

只需将其约束为
:struct
,然后在使用type参数时写入
T[]
,而不是
T

public class DeepCopyArrayOfValueTypes<T> : IDeepCopyable<T[]>
    where T : struct
{
    public T[] DeepCopy(T[] t) {...}
}
公共类DeepCopyArrayOfValueTypes:IDeepCopyable
其中T:struct
{
公共T[]DeepCopy(T[]T{…}
}

如果可能的话,数组中的元素将没有编译时类型。这应该是
IDeepCopyable,其中T:IDeepCopyable
请注意,这将限制您使用不可为空的结构数组。@Eric不幸的是,C#中不支持相反的类型。(在CIL中,它是
.ctor([mscorlib]System.ValueType)
)很好的解决方案。它适用于课堂。如果在方法中有一个用于约束的方法。。。