C#:使用一个通用的创建一个指针数组 [英] C#: Using a generic to create a pointer array

查看:284
本文介绍了C#:使用一个通用的创建一个指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午所有,



一点点帮助,如果你请。为了规避.NET 2GB的对象限制我已经提出,在堆上分配内存的一类,这让我创建数组到我的可用RAM的限制。然而,为了便于开发(因为它是一个概念证明),它是硬编码为多头。现在,它的作品我一直在试图改变使用泛型这样我就可以使用多种类型相同的代码的代码。



在分配内存,并正确索引数组我需要同样类型的指针,数组将举行即多头排列需求的数组长* [] myLargeArray 。问题是,当我使用泛型这一声明变得 T * [] myLargeArray ,它总是产生错误的不能走的地址,获得大小,或者声明指向一个托管类型('T')



先谢谢了。

$ b $ ; b

PS之前有人问,是我真的需要这么大的阵列



代码示例为一个二维数组:

  
LargeArray< INT> myArray的=新LargeArray&所述; INT>(X,Y);

公众安全的类LargeArray其中T:结构
{

私人T * []塔尔;
...
公共LargeArray(长sizeI,长sizeJ)
{
...
myLargeArray =新T * [sizeI]

}
}


解决方案

按照 C#编程指南




以下任何类型的可以是
指针类型:




  • 为sbyte,字节,短,USHORT,INT,UINT,长,ULONG,char和float,
    双,十进制或布尔

  • 任何枚举类型

  • 任何指针类型。

  • 包含非托管类型
    领域的任何用户定义的结构类型只。




当您将结构约束你的泛型类型,编译器不具有足够的信息来推断所有的上述要求得到满足(特别是最后一个点)。



由于我们没有在C#模板,你可能要考虑的数字类型有意义,或者一个工厂类创建阵列/指针适配器的重载创建一个 LargeArray 在给定大小某种类型的。


Afternoon all,

a little help if you please. In order to circumvent the 2Gb object limit in .NET I have made a class that allocates memory on the heap and this allows me to create arrays up to the limit of my free RAM. However, for ease of development (as it was a proof of concept) it was hard coded for longs. Now that it works I've been trying to alter the code to use generics so I can use the same code for multiple types.

In allocating the memory and correctly index the array I need an array of pointers of the same type that the array will hold i.e. a long array needs long*[] myLargeArray. The problem is when I use generics this declaration becomes T*[] myLargeArray, which always produces the error 'Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')'

Thanks in advance.

PS Before anyone asks, yes I really do need such large arrays.

Code example for a 2D array:


    LargeArray <int> myArray = new LargeArray<int>(x, y);

    public unsafe class LargeArray where T : struct
    {
        ...
        private T*[] tArr;
        ...
        public LargeArray(long sizeI, long sizeJ)
        {
            ...
            myLargeArray = new T*[sizeI];
            ...
        }
    }

解决方案

According to the C# programming guide:

Any of the following types may be a pointer type:

  • sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool
  • Any enum type.
  • Any pointer type.
  • Any user-defined struct type that contains fields of unmanaged types only.

When you place the struct constraint on your generic type, the compiler does not have enough information to infer that all of the above requirements will be met (specifically the last point).

Since we don't have templates in C#, you may want to consider creating overloads of your array/pointer adapter for the numeric types that make sense, or a factory class that creates a LargeArray given a size of a certain type.

这篇关于C#:使用一个通用的创建一个指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆