如何序列化属性 [英] How to Serialize an Attribute

查看:73
本文介绍了如何序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端服务器应用程序,该应用程序通过反射支持附加组件.

有时,用于加载项的库使用.NET自定义属性来声明诸如需求,安全问题之类的信息.等

我想将这些信息传递给客户端.但是,由于客户端不知道这些对象,因此无法使用它们.是否可以使用常规XML序列化程序将它们序列化为XML?

I have a client -server application which support add-ons via Reflection.

The libraries used for Add-Ons some-times use .NET custom attributes to declare information such as requirements, security concerns. etc.

I want to pass these information to the client. However, since the client is not aware of these objects, I cannot use them. Is it possible to serialize them into XML using normal XML serializer ?

推荐答案

我不认为您需要序列化该属性:从某种意义上说,它不是对象的一部分,它是对象类型(其元数据)的一部分.

这就是为什么没有意义的原因.考虑您有9000个相同类型的对象. ("9000"是一个流行的模因.)您将不得不在流中保留尽可能多的实例,这是正确的,因为每个实例都可能是唯一的,但是为什么要重复元数据部分那么多次呢?关于持久性,持久性数据和元数据之间存在更多的根本区别.您有问题的某个对象图中的内存中有数据.借助持久性,您可以将其放入流中,但是唯一的原因是能够在同一台或不同计算机上的某个位置读取和还原它.您可以用元数据恢复"什么?无论如何,它始终与您的代码一起使用;即使您读取它,内存中也没有任何内容可以根据您从流中读取的元数据进行修改.从这个意义上说,元数据不能被序列化".我会说只能是"已记录".

无论如何,您可以通过某些自定义方式进行操作.首先,您需要编写一些所需属性的序列化(它们都是您的吗?).然后,在要归属的类中实现ISerializeable.使用反射,您可以收集有关所应用属性的信息,然后在ISerializeable的实现中使用此数据.

我认为这已经足够了,因为1)您可以使用此想法和常见的Microsoft实现轻松实现此目标; 2)您应该认真看待这个问题,考虑上面我给您的原因,然后再考虑其他事情,例如,您可以根据自己的实际情况,在所有单独的实体中记录文档目的.

如果您仍要对此做某事并有一些疑问,请询问他们.
I don''t think you need to serialize the attribute: in a way, it is not a part of the object, it is a part of object''s type, its meta-data.

That''s why it makes little sense. Consider you have 9000 objects of the same type. ("9000" is a popular meme.) You will have to persist as many instances in your stream, which is all right because every instance is potentially unique, but why repeating as many times the meta-data part? When it comes to persistence, there is more fundamental difference between persistent data and meta-data. You have data in memory in some object graph in question. With persistence you can put it in stream, but the only reason is to be able to read and restore it somewhere on the same or different machine. What can you "restore" with meta-data? It is always with your code anyway; even when you read it, there is nothing in memory that can be modified based on the meta-data you read from the stream. In that sense, meta-data cannot be "serialized". I would say it can be only be "documented".

Anyway, you can do it in some custom way. First, you need to write some serialization of the attributes you need (are they all yours?). Then you implement ISerializeable in the class which is to be attributed. Using Reflection, you collect information on the applied attributes and then use this data in your implementation of ISerializeable.

I think this is enough said, because 1) you can easily implement this using this idea and common Microsoft implementation; 2) you should critically look at this problem, consider the reasons I give you above and think about something else, for example, you can document all the meta-data in a separate entity, depending in your real purpose.

If you still want to do something about it and have some questions, please ask them.


这篇关于如何序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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