Click here to Skip to main content
14,930,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

I have an Interface, which is defining some properties.
Public Interface ITest
string MemberfirstName {get; set; }
string MemberLastName {get; set; }

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


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); }

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.
Updated 10-Nov-14 4:59am
Sergey Alexandrovich Kryukov 10-Nov-14 11:29am
C++? Where?

1 solution

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:[^].


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900