Generic pointer

I am trying to point to a pointer to a generic structure (blittable). All this seems fine when I deal with unrelated structures =>, then I can use Marshal.PtrToStructure (...), but this function does not get generic structures (why?)

So, I wrote the following:

public static object ReadValue<T>(IntPtr ptr) where T : struct
    {
        var dm = new DynamicMethod("$", typeof(T), Type.EmptyTypes);
            ILGenerator il = dm.GetILGenerator();
            il.Emit(OpCodes.Ldc_I4, ptr.ToInt32());
            il.Emit(OpCodes.Ldobj, typeof(T));
            il.Emit(OpCodes.Ret);

            var func = (Func<T>)dm.CreateDelegate(typeof(Func<T>));
            return func();
    }

But now this is something wrong with the Ldobj instruction. VS gives me: Additional information: An operation can destabilize runtime.

What am I doing wrong? Does anyone know a better approach to this problem (ptr to the general structure) or have found an error in this function?

0
source share
1 answer

, , .net . StructLayout FieldOffset 0.

http://netpl.blogspot.com/2010/10/is-net-type-safe.html

+1

Source: https://habr.com/ru/post/1627021/


All Articles