c# - 如何使这些结构函数通用?

标签 c# generics struct

我有这两个函数,它们将流读入缓冲区并将其加载到给定的结构中。

    TestStruct1 ReadRecFromStream2(Stream stream)
    {
        byte[] buffer = new byte[Marshal.SizeOf(typeof(TestStruct1))];
        stream.Read(buffer, 0, 128);
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        try
        {
            return (TestStruct1)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TestStruct1));
        }
        finally
        {
            handle.Free();
        }
    }

    TestStruct2 ReadRecFromStream(Stream stream)
    {
        byte[] buffer = new byte[Marshal.SizeOf(typeof(TestStruct2))];
        stream.Read(buffer, 0, 128);
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        try
        {
            return (TestStruct2)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(TestStruct2));
        }
        finally
        {
            handle.Free();
        }
    }

我想将这些组合成一个通用函数以采用其中任何一个结构,我只是不确定执行此操作的正确方法是什么。

这是正确的方法吗?

    private T ReadRecFromStream<T>(Stream stream)
    {
        byte[] buffer = new byte[Marshal.SizeOf(typeof(T))];
        stream.Read(buffer, 0, HeaderSize);
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        try
        {
            return (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
        }
        finally
        {
            handle.Free();
        }
    }

最佳答案

也许你可以使用这些方法来转换byte[]:

public static unsafe byte[] ToBytes<T>(this T value)
    where T : struct
{
    var result = new byte[Marshal.SizeOf(typeof(T))];
    fixed (byte* b = &result[0])
    {
        var p = new IntPtr(b);
        Marshal.StructureToPtr(value, p, false);
    }

    return result;
}

public static unsafe T FromBytes<T>(this byte[] bytes, int startIndex = 0)
    where T : struct
{
    fixed (byte* b = &bytes[startIndex])
    {
        var p = new IntPtr(b);
        return (T)Marshal.PtrToStructure(p, typeof(T));
    }
}

使用这个你的方法可以更改为:

T ReadRecFromStream<T>(Stream stream)
    where T : struct
{
    byte[] buffer = new byte[Marshal.SizeOf(typeof(T))];
    stream.Read(buffer, 0, buffer.Length);
    return buffer.FromBytes<T>()
}

Read 的工作方式类似。

关于c# - 如何使这些结构函数通用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36201861/

相关文章:

c# - 有效且高效地管理许多 TCP 连接

c# - Linq 复杂搜索导致 NullReferenceException

c# - 是否可以在 C# 中定义参数化类型别名?

java - 如何将这种鸭子类型(duck typing)(Python)转换为 Java 泛型?

c - 在结构体内部定义结构体指针

c# - 您必须添加对程序集 mscorlib 的引用,version=4.0.0

c# - Entity Framework 继承 - 仅检索父类型

java - 如何引用 Java 中接口(interface)实现的类类型?

matlab - 如何比较matlab细胞或结构

c# - C# 结构私有(private)/公共(public)错误