1

This is not a duplicate of this question. I have to serialize the Property which is "ReadOnly". I can't do anything on this class, because this is System.Web.Security.MembershipUser class, of course this is not sealed class.

[WebGet]
public string GetAllUsers()
{
    List<MembershipUser> membershipList = new List<MembershipUser>();

    MembershipUserCollection userCollection = Membership.GetAllUsers();

    foreach (MembershipUser user in userCollection)
        membershipList.Add(user);

    string memberCollection = SerializeToString(membershipList, typeof(List<MembershipUser>));

    List<MembershipUser> users = Deserialize(memberCollection, typeof(List<MembershipUser>)) as List<MembershipUser>;

    return memberCollection;
}

Above code is what I used,

MembershipUserCollection userCollection = Membership.GetAllUsers();

GetAllUsers method returns MembershipUserCollection, but this does not have default accessor. So while serializing I get exception. That is the reason I went with List<MembershipUser>. Here too I face trouble. This is eating my day, what could solve this?.

Edit: I'm using XmlSerializer.

Community
  • 1
  • 1
Mohanavel
  • 1,763
  • 3
  • 22
  • 44

2 Answers2

2

Serializing will only serialize public fields as well as public properties that you can both get and set. The reason for the latter is that if you cannot set it, then when you go to deserialize it, how do you set the property?

Since the class isn't sealed, you could inherit from it, define a setter, but have it do nothing, i.e.

public string Name
{
  get {return _name;}
  set { }
}

The thing to look out for is when you deserialize to that class, the data will be lost.

HTH, Brian

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • Depends on the serializer - the WCF DataContractSerializer will happily serialize private members, as long as you put a [DataMember] attribute on them..... – marc_s Dec 07 '10 at 17:03
  • True; I was thinking about the Xml Serializer. The question didn't specify which one is being used, could be helpful information. – Brian Ball Dec 07 '10 at 17:06
  • You can set the serializer, I didn't know about this, It can be DataContractSerializer or not, If it can, may be It's a solution, but also you can write your own serializer, which can access private fields, but in first should check those are private fields or will be created with other data. – Saeed Amiri Dec 07 '10 at 17:09
  • Another edge cases; lists etc can be readonly as long as they **don't have** a set; a private set causes #fail – Marc Gravell Dec 07 '10 at 17:19
  • Yes i inherited, still all the stuffs are done through base class, so there is no way to create object by derived class, So i used the reflection property and copied all the properties. I derived i suppressed the properties which holds only getter. – Mohanavel Dec 08 '10 at 08:54
0

I would suggest creating a wrapper over MembershipUserCollection for your serialization/deserialization purpose.

Also, Are you sure that above code is throwing exception because fields have private members? It may be because of missing Serializableattribute or default constructor!!!

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103