指向类型通用的阵列? [英] pointer to a array of type generic?

查看:88
本文介绍了指向类型通用的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用指针T的数组,其中T可能是一个C#原始的,或者C#类的通用方法。我正沿着细下去,直到我试图在T的一部分。

I'd like to create a generic method that uses a pointer to an array of T where T could be a C# primitive, or a C# class. I was going along fine until I attempted the "T" part.

有没有解决该错误的方式不能声明指向一个非非托管类型TIN

Is there a way around the error "can not declare a pointer to a non unmanaged type TIn"

我试图用通过固定的关键字牵制,使这成为可能。

I tried to use pinning via the "fixed" keyword to make this possible.

    public static object DoSomething<TIn, TOut>(object SObj, Action<TIn, TOut> takeAction)
    {
        double[]A = (double[]) SObj;
        TIn[]B = new TIn[5];
        unsafe
        {
            fixed (double* dbl = A) // <--- works okay
            {

            }
            fixed (TIn* Sptr = B)  // <--- fails
            {

            }
        }
    }

-

@dtb:刚刚签出blittable。 Blittable型的一维阵列,例如整数的阵列。但是,包含Blittable型的变量阵列的类型不是本身blittable。即使有一个biltable约束,好像他们已经将它仅限于一维数组。

@dtb: just checked out blittable. "One-dimensional arrays of blittable types, such as an array of integers. However, a type that contains a variable array of blittable types is not itself blittable." Even if there was a biltable constraint, it seems like they've limited it to arrays of one dimension.

推荐答案

从本质上讲,没有。您不能创建一个指向托管类型 - 只对某些基本类型和结构,所有的领域本身结构还是非托管类型。你的泛型参数类型不会有这些特性,所以它禁止指针的建设,为您提供了错误

Essentially, no. You can't create a pointer to a managed type - only to certain primitive types and structs where all the fields are themselves structs or unmanaged types. Your generic parameter type will not have these properties, so it forbids construction of a pointer and gives you that error.

http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx ,指针可以到发


  • 为sbyte,字节,短,USHORT,INT,UINT,长,ULONG,CHAR ,浮动,双,十进制或布尔。

  • sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

任何枚举类型。

任何指针类型。

这包含非托管类型的字段的任何用户定义的结构类型只。

Any user-defined struct type that contains fields of unmanaged types only.

幸运的是,指针算法是不可能的,有一大堆的好处并不是有一个指向托管类型。为什么你想建立一个?

Fortunately, as pointer arithmetic is impossible, there's not a whole lot of benefit to having a pointer to a managed type. Why do you want to build one?

这篇关于指向类型通用的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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