序列化只读成员数据

发布于 2024-10-06 11:03:52 字数 1124 浏览 4 评论 0原文

这不是此问题的重复项。我必须序列化“只读”属性。我无法对这个类执行任何操作,因为这是 System.Web.Security.MembershipUser 类,当然这不是密封类。

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

上面的代码是我使用的,

MembershipUserCollection userCollection = Membership.GetAllUsers();

GetAllUsers方法返回MembershipUserCollection,但这没有默认访问器。所以在序列化时我遇到了异常。这就是我选择 List 的原因。我在这里也遇到麻烦。这让我很沮丧,有什么办法可以解决这个问题?

编辑: 我正在使用 XmlSerializer。

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.

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

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

发布评论

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

评论(2

苦妄 2024-10-13 11:03:52

序列化只会序列化公共字段以及您可以获取和设置的公共属性。后者的原因是,如果你不能设置的话,那么你去反序列化的时候,属性怎么设置呢?

由于该类不是密封的,因此您可以继承它,定义一个setter,但让它不执行任何操作,即

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

需要注意的是,当您反序列化到该类时,数据将丢失。

哈特哈,
布莱恩

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

幻想少年梦 2024-10-13 11:03:52

我建议为序列化/反序列化目的创建一个 MembershipUserCollection 包装器。

另外,您确定上面的代码会抛出异常,因为字段具有私有成员吗?这可能是因为缺少 Serialized 属性或默认构造函数! !

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!!!

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