为什么泛型结构不能不受管理?

时间:2018-11-08 13:21:35

标签: c# c#-7.3

请查看以下代码:

namespace ConsoleApp
{
    public struct MyPoorGenericStructThatCannotBeUnmanaged<T> where T: unmanaged
    {
        public T Field;
    }

    public class MyClass<T> where T: unmanaged
    {
    }

    class Program
    {
        static void Main()
        {
            // The type 'MyPoorGenericStructThatCannotBeUnmanaged<int>' must be a non-nullable value type, 
            // along with all fields at any level of nesting, 
            // in order to use it as parameter 'T' in the generic type or method 'MyClass<T>'
            var obj = new MyClass<MyPoorGenericStructThatCannotBeUnmanaged<int>>(); 
        }
    }
}

它无法编译并显示错误:

  

类型“ MyPoorGenericStructThatCannotBeUnmanaged”必须为   非空值类型,以及所有级别的所有字段   嵌套,以便在通用类型中将其用作参数“ T”或   方法“ MyClass”

但是MyPoorGenericStructThatCannotBeUnmanaged<int>是一个不可为空的值类型,并且它在任何嵌套值处的所有字段实际上都是不可为空的值类型。它由通用类型约束where T: unmanaged

保证。

为什么?

1 个答案:

答案 0 :(得分:0)

在解决限制之前,您可以使用基于不安全的解决方法。

解决方法如下:

public unsafe class ArrayOfGenericStructs<TStruct> : IDisposable where TStruct:struct
{
    private void* pointer;

    public ArrayOfGenericStructs(int size)
    {
        pointer = (void*) Marshal.AllocHGlobal(Unsafe.SizeOf<TStruct>() * size);
    }

    public bool IsDisposed { get; private set; }

    public void Dispose()
    {
        if (IsDisposed) return;
        IsDisposed = true;
        if (pointer != null) Marshal.FreeHGlobal(new IntPtr(pointer));
        pointer = null;
    }

    public ref TStruct this[int index]
    {
        get
        {
            return ref Unsafe.AsRef<TStruct>(Unsafe.Add<TStruct>(pointer, index));
        }
    }
}