Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 约束N维数组的泛型_C#_Arrays_Generics - Fatal编程技术网

C# 约束N维数组的泛型

C# 约束N维数组的泛型,c#,arrays,generics,C#,Arrays,Generics,有没有办法将泛型类型约束为n维数组 所以,T[],T[,],T[,,]。。。等等 我基本上是在尝试向我拥有的任何类型的数组添加扩展方法。因此,我希望使用泛型获得这些方法的组合,这样就不需要在内部重复代码 public static bool IsFull(this MyType[] self) { ... } public static bool IsFull(this MyType[,] self) { ... } public static bool IsFull(this MyType[,

有没有办法将泛型类型约束为n维数组

所以,
T[]
T[,]
T[,,]
。。。等等

我基本上是在尝试向我拥有的任何类型的数组添加扩展方法。因此,我希望使用泛型获得这些方法的组合,这样就不需要在内部重复代码

public static bool IsFull(this MyType[] self) { ... }
public static bool IsFull(this MyType[,] self) { ... }
public static bool IsFull(this MyType[,,] self) { ... }
有一种方法看起来像这样,但是对于
[,]
[,,]
等应该具有完全相同的逻辑:

public static bool IsFull(this MyType[] self)
    for (int i=0; i < self.Length; i++) {
        MyType t = self.GetValue(i);
        if (t == null || !t.IsFull()) {
            return false;
        }
    }
    return true;
公共静态bool已满(此MyType[]self)
for(int i=0;i
我相信您最终必须检查您的数组元素是否是
MyType
的实例或另一个数组-如果可以接受,那么您可能可以在
系统上添加扩展。数组
-类似以下内容:

public static class Extension
{
    public static bool IsFull(this Array self)
    {
        for (int i = 0; i < self.Length; i++)
        {
            var t = self.GetValue(i);
            var arrT = t as Array;
            var tt = t as MyType;

            if (t == null || (arrT != null && !arrT.IsFull()) || (tt != null && !tt.IsFull()))
            {
                return false;
            }
        }
        return true;
    }
}
公共静态类扩展
{
公共静态布尔已满(此数组自身)
{
for(int i=0;i
我相信您最终必须检查您的数组元素是否是
MyType
的实例或另一个数组-如果可以接受,那么您可能可以在
系统上添加扩展。数组
-类似以下内容:

public static class Extension
{
    public static bool IsFull(this Array self)
    {
        for (int i = 0; i < self.Length; i++)
        {
            var t = self.GetValue(i);
            var arrT = t as Array;
            var tt = t as MyType;

            if (t == null || (arrT != null && !arrT.IsFull()) || (tt != null && !tt.IsFull()))
            {
                return false;
            }
        }
        return true;
    }
}
公共静态类扩展
{
公共静态布尔已满(此数组自身)
{
for(int i=0;i
使用T4,您应该能够执行以下操作。否则,我不知道有什么类型的约束可以实现你在C#中所寻找的。完全相同的逻辑如何在超过1维的情况下工作,你正在将
self[I]
读入
MyType
变量?@KMoussa啊,是的,我必须将其更改为
GetValue
,感谢T4,你应该能够这样做。否则,我不知道有什么类型的约束来实现你在C中所寻找的。完全相同的逻辑如何在超过1维的情况下工作,你正在将
self[I]
读入
MyType
变量?@KMoussa啊,是的,我必须将其更改为
GetValue
,谢谢谢谢-我没有得到
t作为数组的部分
t
这里应该始终是
MyType
。也许您正在考虑交错数组
[]]
,其中
GetValue
可以返回一个数组。这可能是因为我在方法中混淆地调用了
IsFull
,但这也是
MyType.IsFull
@GP89上的一个方法。哦,你完全正确,我确实将它与锯齿状阵列相混淆了。我不明白
t作为阵列的部分是什么
t
这里应该始终是
MyType
。也许您正在考虑交错数组
[]]
,其中
GetValue
可以返回一个数组。这可能是因为我在方法中混淆地调用了
IsFull
,但这也是
MyType.IsFull
@GP89上的一个方法哦,你完全正确,我确实将它与锯齿数组混淆了