【问题标题】:Some fields are empty after deserialization in Silverlight projectSilverlight项目中反序列化后某些字段为空
【发布时间】:2012-01-21 11:01:59
【问题描述】:

我的 silverlight 项目中的反序列化存在问题。我有这个方法和变量的类 Obj。

public class Obj
{
    private string _name;
    private Uri _iconUri;
    private string _stringUri;
    private List<ObjItem> _items = new List<ObjItem>();

    public List<ObjItem> Items
    {
        get { return _items; }
    }
    public string Name 
    { 
        get { return _name; } 
        set { _name = value; } 
    }
    public Uri IconUri 
    { 
        get 
        { 
            return _iconUri; 
        } 
    }
    public string StringUri { get { return _stringUri; } }
    public int Count
    {
        get { return _items.Count; }
    }

    public Obj(string name,string uriString = null)
    {
        _name = name;
        if (uriString == null)
        {
            _iconUri = null;
        }
        else
        {
            _iconUri = new Uri(uriString, UriKind.Relative);
        }
        _stringUri = uriString;
    }
    // for deserialization
    public Obj()
    {
    }

}

在序列化之前所有字段都不为空! 反序列化后,所有字段都不是空的,除了 _iconUri 和 _stringUri 字段。 为什么会这样? 我会等待你的回复。 谢谢!

【问题讨论】:

  • 因为 Uri 类不可序列化,我猜,你使用的是哪个序列化?将类型更改为字符串,并保留另一个转换并返回 Uri 的属性

标签: c# silverlight serialization deserialization


【解决方案1】:

这两个人没有二传手。 Silverlight 有一个受限的安全模型,你不能通过访问私有字段来作弊;只能访问公共成员。所以:如果您希望它们与 SL 上的大多数序列化程序一起使用,请将公共设置器添加到这些属性。

具体来说:_name 是由 Name 设置器设置的,_items 是通过字段初始化器在构造函数中设置的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多