将不可序列化的类转换为字节数组

发布于 2024-12-12 10:57:05 字数 3017 浏览 0 评论 0原文

我有一个场景,我正在多个非常不同的系统之间同步数据。 (数据本身相似,但不同系统上的表具有完全不同的格式。)为了协助这种同步,我有一个数据库表,它存储来自每个系统的对象哈希以及项目键和其他相关信息。当任一系统中的对象的哈希发生更改时,我会更新另一个系统。

我的数据库表看起来像这样。

CREATE TABLE [dbo].[SyncHashes](
    [SyncHashId] [int] IDENTITY(1,1) NOT NULL,
    [ObjectName] [nvarchar](50) NULL,
    [MappingTypeValue] [nvarchar](25) NULL,
    [MappingDirectionValue] [nvarchar](25) NULL,
    [SourceSystem] [nvarchar](50) NULL,
    [SourceKey] [nvarchar](200) NULL,
    [SourceHash] [nvarchar](50) NULL,
    [TargetSystem] [nvarchar](50) NULL,
    [TargetKey] [nvarchar](200) NULL,
    [TargetHash] [nvarchar](50) NULL,
    [UpdateNeededValue] [nvarchar](max) NULL,
    [CreatedOn] [datetime] NULL,
    [ModifiedOn] [datetime] NULL,
    [Version] [timestamp] NOT NULL, 
    [IsActive] [bit] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [SyncHashId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

到目前为止,一切都很好。但是...

为了有效地计算对象的哈希值(例如 MD5 哈希值(这就是我正在使用的)),您需要能够将其转换为 字节数组

而且......

似乎为了将对象转换为字节数组,它必须是可序列化的。 (至少这是我读过的内容,而且我从 .NET 得到的错误似乎表明这是真的。)

对于其中一个系统,我能够使所有数据库对象可序列化,所以这很棒。生成哈希值,一切都同步,世界变得美丽!

对于另一个系统,情况不太好。我从实体框架 4(代码优先)模型传递了一个数据库上下文,并且实体未序列化。

当我尝试使用类似下面的内容来转换为字节时,.NET 会抱怨并发出轻微的脾气——同时拒绝给我我如此礼貌地要求的漂亮的小字节数组。

foreach(var dataItem in context.TableName)
{
    var byteArray = (byte[]) dataItem;
}

好的。没问题。

我自己有一个很好的小扩展方法,我认为它可以解决问题。

public static byte[] ObjectToByteArray<T>(this T obj)
{
    if (obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();

    bf.Serialize(ms, obj);
    return ms.ToArray();
}

但是哦不!如果对象(实体)不可序列化,则此例程会抛出另一个很好的小(完全预期的)异常。

所以...我修改例程并在方法定义中添加一个 where 子句,如下所示。

public static byte[] ObjectToByteArray<T>(this T obj) where T : ISerializable
{
    if (obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();

    bf.Serialize(ms, obj);
    return ms.ToArray();
}

唯一的问题是,现在我又回到了原点,所有对象都需要可序列化才能获得字节数组。

嗯。不好。

因此,我组合了一个技巧来迭代对象的所有属性并生成一个字符串表示形式,我可以从中构建一个字节数组。虽然它丑陋且低效,但它确实起到了作用。

public static string ComputeMD5Hash<T>(this T input)
{
    StringBuilder sb = new StringBuilder();

    Type t = input.GetType();
    PropertyInfo[] properties = t.GetProperties();

    foreach (var property in properties)
    {
        sb.Append(property.Name);
        sb.Append("|");
        object value = property.GetValue(input, null);
        if (value != null)
        {
            sb.Append(value);
        }
        sb.Append("|");
    }

    return MD5HashGenerator.GenerateKey(sb.ToString());
}

但是...

毕竟,我仍然真正希望能够有效且正确地从类未标记为可序列化的对象创建字节数组。实现这一目标的最佳方法是什么?

先感谢您!

I have a scenario where I am synchronizing data between multiple VERY dissimilar systems. (The data itself is similar but the tables on the different systems have quite different formats.) To assist with this synchronization, I have a database table which stores object hashes from each of the systems along with item keys and other relevant information. When the hash of an object from either system changes, I update the other.

My database table looks something like this.

CREATE TABLE [dbo].[SyncHashes](
    [SyncHashId] [int] IDENTITY(1,1) NOT NULL,
    [ObjectName] [nvarchar](50) NULL,
    [MappingTypeValue] [nvarchar](25) NULL,
    [MappingDirectionValue] [nvarchar](25) NULL,
    [SourceSystem] [nvarchar](50) NULL,
    [SourceKey] [nvarchar](200) NULL,
    [SourceHash] [nvarchar](50) NULL,
    [TargetSystem] [nvarchar](50) NULL,
    [TargetKey] [nvarchar](200) NULL,
    [TargetHash] [nvarchar](50) NULL,
    [UpdateNeededValue] [nvarchar](max) NULL,
    [CreatedOn] [datetime] NULL,
    [ModifiedOn] [datetime] NULL,
    [Version] [timestamp] NOT NULL, 
    [IsActive] [bit] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [SyncHashId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

So far so good. But...

In order to effectively compute a hash (such as an MD5 hash (which is what i am using)) for an object, you need to be able to convert it to a byte array.

And...

It seems that in order to convert an object to a byte array, it must be serializable. (At least that's what I have read, and the errors I am getting from .NET seem to indicate that is true.)

For one of the systems, I have the ability to make all of my database objects serializable so that's great. Hashes get generated, everything gets synchronized, and the world is beautiful!

For another system, things are not so great. I am passed a database context from entity framework 4 (code first) model and the entities are NOT serialized.

When I attempt to cast as byte using something like the following, .NET complains and throws a minor tantrum--all the while refusing to give me the nice little byte array I so politely asked for.

foreach(var dataItem in context.TableName)
{
    var byteArray = (byte[]) dataItem;
}

Ok. No problem.

I have myself a nice little extension method which I thought might do the trick.

public static byte[] ObjectToByteArray<T>(this T obj)
{
    if (obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();

    bf.Serialize(ms, obj);
    return ms.ToArray();
}

But oh no! If the object (the Entity) is not serializable, this routine throws me another nice little (and totally expected) exception.

So... I modify the routine and add a where clause to the method definition like so.

public static byte[] ObjectToByteArray<T>(this T obj) where T : ISerializable
{
    if (obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();

    bf.Serialize(ms, obj);
    return ms.ToArray();
}

The only problem is that now I am back to square one where all of my objects need to be serializable to get a byte array.

Hmmm. Not good.

So I put together a hack to iterate through all of the object's properties and generate a string representation from which I could build a byte array. It was UGLY and INEFFICIENT but it kind of sort of did the trick.

public static string ComputeMD5Hash<T>(this T input)
{
    StringBuilder sb = new StringBuilder();

    Type t = input.GetType();
    PropertyInfo[] properties = t.GetProperties();

    foreach (var property in properties)
    {
        sb.Append(property.Name);
        sb.Append("|");
        object value = property.GetValue(input, null);
        if (value != null)
        {
            sb.Append(value);
        }
        sb.Append("|");
    }

    return MD5HashGenerator.GenerateKey(sb.ToString());
}

But...

After all that, what I still really would like to be able to is efficiently and properly to create a byte array from an object whose class is not marked as serializable. What is the best way to accomplish this?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2024-12-19 10:57:05

从类未标记为可序列化的对象创建字节数组

您可以使用 protobuf-net v2 来完成此任务。下载 zip 文件,然后引用 protobuf-net 程序集。

考虑我们要序列化的这个简单的类定义:

public class Person
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public int Age { get; set; }
}

然后您可以将其序列化为字节数组:

var person = new Person {Firstname = "John", Surname = "Smith", Age = 30};
var model = ProtoBuf.Meta.TypeModel.Create();
//add all properties you want to serialize. 
//in this case we just loop over all the public properties of the class
//Order by name so the properties are in a predictable order
var properties = typeof (Person).GetProperties().Select(p => p.Name).OrderBy(name => name).ToArray();
model.Add(typeof(Person), true).Add(properties);

byte[] bytes;

using (var memoryStream = new MemoryStream())
{
    model.Serialize(memoryStream, person);
    bytes = memoryStream.GetBuffer();
}

protobuf-net 序列化器的序列化速度快得多,并生成比 BinaryFormatter< 更小的 byte[] 数组/code>

警告 1 这只会(以其当前形式)序列化类的公共属性,这看起来适合您的使用。
警告 2 这被认为是脆弱的,因为向 Person 添加新属性可能意味着您无法反序列化使用先前序列化的 Person 对象类型模型

create a byte array from an object whose class is not marked as serializable

You can use protobuf-net v2 to accomplish this. Download the zip then reference the protobuf-net assembly.

Consider this simple class definition we want to serialize:

public class Person
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public int Age { get; set; }
}

You can then serialize this as a byte array:

var person = new Person {Firstname = "John", Surname = "Smith", Age = 30};
var model = ProtoBuf.Meta.TypeModel.Create();
//add all properties you want to serialize. 
//in this case we just loop over all the public properties of the class
//Order by name so the properties are in a predictable order
var properties = typeof (Person).GetProperties().Select(p => p.Name).OrderBy(name => name).ToArray();
model.Add(typeof(Person), true).Add(properties);

byte[] bytes;

using (var memoryStream = new MemoryStream())
{
    model.Serialize(memoryStream, person);
    bytes = memoryStream.GetBuffer();
}

The protobuf-net serializer will serialize much faster and produce a smaller byte[] array than BinaryFormatter

caveat 1 This will only (in its current form) serialize the public properties of your class, which looks ok for your usage.
caveat 2 This is considered brittle because adding a new property to Person may mean you are unable to deserialize a Person object that was serialized with the prior TypeModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文