使用泛型来序列化基元。 [英] using generics to serialize primitives.

查看:58
本文介绍了使用泛型来序列化基元。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将基元转换为字节数组的最简单方法是什么?我尝试了

BinaryFormatter序列化,但它序列化了对象,所以当我序列化

an和int时,需要54个字节而不是4个。我尝试了类似

以下但它不会编译(我承认我经常使用泛型,但是没有写过很多b $ b因此可能会有所不同):


class X< T>

{

public byte [] ToByteArray(T val)

{

BinaryWriter bw = new BinaryWriter(new MemoryStream());

bw.Write(val);

BinaryReader br = new BinaryReader(bw.BaseStream);

br.BaseStream.Position = 0;

返回br.ReadBytes((int)br.BaseStream.Length);

}

}


我不想为每种原始类型都有重载方法。有没有

一个简单的方法来做到这一点......或者已经编写了重载来做

这个,我只是错过了它?这似乎是一个非常基本的任务,应该已经在某处已经存在。


感谢

What is the easiest way to convert primitives to a byte array? I tried the
BinaryFormatter serialization but it serializes objects so when I serialized
an and int it took 54 bytes instead of 4. I tried something like the
following but it won''t compile (I admit I use generics a lot but haven''t
written many so this may be way off):

class X<T>
{
public byte[] ToByteArray(T val)
{
BinaryWriter bw = new BinaryWriter(new MemoryStream());
bw.Write(val);
BinaryReader br = new BinaryReader(bw.BaseStream);
br.BaseStream.Position = 0;
return br.ReadBytes((int)br.BaseStream.Length);
}
}

I don''t want to have an overloaded method for every primitive type. Is there
an easy way to do this...or have the overloads already been written to do
this and I am just missing it? It seems like a pretty basic task that should
be there somewhere already.

thanks

推荐答案

嗯,可能有一个更好的方法隐藏 - 但是如何(有点

肮脏,但它似乎工作......):

Marc


使用System;

使用System.Reflection;

静态类程序

{

static void Main()

{

byte [] data = BitHelper< int> .Write(12345);

int value = BitHelper< int> .Read(data,0);

}


静态类BitHelper< T>

{

静态BitHelper()

{

MethodInfo方法=

typeof( BitConverter)。GetMethod(" GetBytes",new Type [] {typeof(T)});

if(method == null)throw new NotSupportedException();

write =(Func< T,

byte []>)Delegate.CreateDelegate(typeof(Func< T,byte []>),met (b)方法= typeof(BitConverter).GetMethod(&To; +

typeof(T).Name,new Type [] {typeof(byte []),typeof(int)});

if(method == null)抛出新的NotSupportedException();

read =(Func< byte [],int,

T>)Delegate.CreateDelegate(typeof(Func< byte [],int, T>),方法);

}

静态Func< byte [],int,Tread;

静态Func< T,byte [ ]写;

public static T Read(byte [] buffer,int index){return

read(buffer,index); } $ / $
public static byte [] Write(T value){return write(value); } $ / $
public static int写(T值,byte []缓冲区,int索引)

{

byte [] tmp =写入(值);

tmp.CopyTo(缓冲区,索引);

返回tmp.Length;

}


}

}}
Well, there might be a better way tucked away - but how about (a little
grubby, but it seems to work...):

Marc

using System;
using System.Reflection;
static class Program
{
static void Main()
{
byte[] data = BitHelper<int>.Write(12345);
int value = BitHelper<int>.Read(data, 0);
}

static class BitHelper<T>
{
static BitHelper()
{
MethodInfo method =
typeof(BitConverter).GetMethod("GetBytes", new Type[] { typeof(T) });
if (method == null) throw new NotSupportedException();
write = (Func<T,
byte[]>)Delegate.CreateDelegate(typeof(Func<T, byte[]>), method);

method = typeof(BitConverter).GetMethod("To" +
typeof(T).Name, new Type[] {typeof(byte[]), typeof(int)});
if (method == null) throw new NotSupportedException();
read = (Func<byte[], int,
T>)Delegate.CreateDelegate(typeof(Func<byte[], int, T>), method);
}
static Func<byte[], int, Tread;
static Func<T, byte[]write;
public static T Read(byte[] buffer, int index) { return
read(buffer, index); }
public static byte[] Write(T value) { return write(value); }
public static int Write(T value, byte[] buffer, int index)
{
byte[] tmp = Write(value);
tmp.CopyTo(buffer, index);
return tmp.Length;
}

}
}}


哦;如果您使用的是.NET 2.0 / 3.0,那么您需要添加:


委托TArg3 Func< TArg1,TArg2,TArg3>(TArg1 arg1,TArg2 arg2);

委托TArg2 Func< TArg1,TArg2>(TArg1 arg1);


Marc
Oh; if you are using .NET 2.0/3.0, then you''ll need to add:

delegate TArg3 Func<TArg1, TArg2, TArg3>(TArg1 arg1, TArg2 arg2);
delegate TArg2 Func<TArg1, TArg2>(TArg1 arg1);

Marc


当然你可以使用不安全的

代码做一些更有趣的事情......


Marc
Of course you can probably do some more interesting things using unsafe
code...

Marc


这篇关于使用泛型来序列化基元。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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