如何在接口上实现xml序列化 [英] How to implement xml serialization on interfaces

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

问题描述

你好,



我有一个接口,它定义了一些属性。

Hello,

I have an Interface, which is defining some properties.

Public Interface ITest
{
string MemberfirstName {get; set; }
string MemberLastName {get; set; }
}

[NonPersistent]
public class Member :  BaseObject, ITest
{

public Member (Session session) : base(session) { }

      private string memberfirstName ;
      public string MemberfirstName 
       {
           get { return memberfirstName ; }
           set { SetPropertyValue("MemberfirstName ", ref memberfirstName , value); }
       }
      
      private string memberLastName ;
      public string MemberLastName 
       {
           get { return memberLastName ; }
           set { SetPropertyValue("MemberLastName ", ref memberLastName , value); }
       }

      private string memberGender ;
      public string MemberGender
       {
           get { return memberGender  ; }
           set { SetPropertyValue("MemberGender", ref memberGender , value); }
       }

    
    public List<contacts> RawContacts
       {
           get;
           set;
       }

}

[NonPersistent]
public class Contacts : BaseObject, ITest
{
      private string residenceAddress;
      public string ResidenceAddress
       {
           get { return residenceAddress; }
           set { SetPropertyValue("ResidenceAddress", ref residenceAddress, value); }
       }

      private string mailingAddress;
      public string MailingAddress
       {
           get { return mailingAddress; }
           set { SetPropertyValue("MailingAddress", ref mailingAddress, value); }
       }

      private string mobileNumber
      public string mobileNumber
       {
           get { return mobileNumber; }
           set { SetPropertyValue("MobileNumber", ref mobileNumber, value); }
       }

      private Member memberDetails;
      public Member MemberDetails
       {
           get { return memberDetails; }
           set { SetPropertyValue("MemberDetails", ref memberDetails, value); }
       }
}



这里,我正在序列化成员和联系人这些正在使用xml序列化构建xml文件,即使是继承BaseObject的类也是如此因为上面的类是非持久的。



在序列化Member和Contacts类时,成员类中的所有属性(必需属性和非必需属性)都是序列化的。



这里我想序列化属性,这些属性仅在接口中定义。

即仅使用MemberFirstName和MemberLastName我不想序列化在MemberClass中定义的MemberGender属性。



我可以知道是否可以序列化仅在接口中定义的属性。


Here, I am serializing the Member and Contacts these are constructing the xml file by using xml serialization even the classes inheriting BaseObject because the above classes are non - persistent.

while serializing Member and Contacts class, all of the properties in member class(required and non required properties) are serializing.

Here I want to serialize the properties, which are defined in Interface only.
i.e. MemberFirstName and MemberLastName only i dont want to serialize the MemberGender Property defined in MemberClass.

May i know is it possible to serialize the properties defined in Interface only.

推荐答案

这不是序列化的工作方式。



从理论上讲,它是很有可能专门用接口创建序列化。 为此,您需要创建自己的序列化机制。这也是可能的。我可以告诉你涉及到什么。您可以使用反射挖掘出每种类型的成员。特别是,它将允许您检查实现哪些接口,以及哪些成员正在实现接口成员。然后,您可以获取基元,字符串和枚举类型的所有属性/字段的值。对于其他类型,您可以递归反映其类型。最终,您可以从某些对象图中收集所有数据,并按照您希望的方式保留它。您可以选择要保留的数据部分以及您不想要的方式。



问题是:反射很慢。使用 System.Reflection.Emit 可以大大提高性能。这就是.NET FCL中实现的反射的工作原理。当第一次序列化某些数据时,会动态创建序列化程序集,以后再重复使用。编写Emit代码并不是一件容易的事。它需要很好的IL知识,很难调试。我做到了。是的,对于我自己的序列化系统。是的,它有效,但我想改进它。



我建议你做这个工作吗?我的强烈建议是:不要这样做。为什么?不,不仅仅是因为使用 System.Reflection.Emit 进行编程真的很困难。因为你的想法不值得。这是一个糟糕的,临时的,非灵活的想法。但是,你决定。



我建议什么?请使用运行良好的可用机制:数据合同。请参阅: http://msdn.microsoft.com/en-us /library/ms733127%28v=vs.110%29.aspx [ ^ ]。



-SA
This is not how serialization works.

Theoretically speaking, it's quite possible to create serialization specifically with interfaces. To do so, you would need to create your own serialization mechanism. This is possible, too. I can tell you what's involved. You can dig out what members are in each type involved using reflection. In particular, it will allow you to check up which interfaces are implemented, and which members are implementing interface members. Then you can get values of all properties/fields of primitive, string and enumeration types. For other types, you can recursively reflect their types. Ultimately, you can collect all the data from some object graph and persist it the way you want. You can choose what part of data to persist and what not the way you want.

The trouble is: reflection is slow. You can greatly improve performance using System.Reflection.Emit. This is how reflection implemented in .NET FCL works. When some data is serialized for the first time, a serialization assembly is created on the fly and is reused later. Writing Emit code is not so easy task. It requires good knowledge of IL and is hard to debug. Yes, I did it. Yes, for my own serialization system. Yes, it works, but I would like to improve it.

Would I advise you to do this job? My strong advice is: don't do it. Why? No, not just because programming with System.Reflection.Emit is really difficult. Because your idea doesn't worth it. This is a bad, ad-hoc, non-flexible idea. However, you decide.

What would I advise? Use the well-working available mechanism instead: Data Contract. Please see: http://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

—SA


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

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