Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET XmlSerializer:当属性设置程序具有其他代码时序列化_C#_.net_Xml_Serialization - Fatal编程技术网

C# .NET XmlSerializer:当属性设置程序具有其他代码时序列化

C# .NET XmlSerializer:当属性设置程序具有其他代码时序列化,c#,.net,xml,serialization,C#,.net,Xml,Serialization,我有一个(非常缩写的)类,如下所示: public class Widget { public List<Widget> SubWidgets { get; set; } public Widget ParentWidget { get; set; } private double _ImportantValue; public double ImportantValue { get { return _ImportantValue; } set

我有一个(非常缩写的)类,如下所示:

public class Widget
{
  public List<Widget> SubWidgets { get; set; }
  public Widget ParentWidget { get; set; }

  private double _ImportantValue;
  public double ImportantValue
  {
    get { return _ImportantValue; }
    set
    {
      _ImportantValue = value;
      RecalculateSubWidgets();
    }
  }
}
公共类小部件
{
公共列表子部件{get;set;}
公共小部件ParentWidget{get;set;}
私人双重价值;
公共双重重要价值
{
获取{return\u ImportantValue;}
设置
{
_重要性价值=价值;
重新计算subwidgets();
}
}
}

反序列化时,我不想重新计算子部件。处理这种情况的最佳方法是什么?到目前为止,我唯一能想到的是设置一个“全局”变量,该变量表示我正在反序列化,在这种情况下跳过对RecreacteSubWidgets()的调用,但这似乎太麻烦了。

一个简单的方法是忽略当前属性,使用另一个属性获取反序列化的值:

private double _ImportantValue;

[XmlElement("ImportantValue")]
public double ImportantValueFromXml
{
    get { return _ImportantValue; }
    set
    {
        _ImportantValue = value;
    }
}

[XmlIgnore]
public double ImportantValue
{
    get { return _ImportantValue; }
    set
    {
        _ImportantValue = value;
        RecalculateSubWidgets();
    }
}

当您反序列化时,将不会调用RecreacteSubWidgets()方法,但您的私有字段仍将具有该值。当然,为了避免这种情况,您可能需要稍微更改一下设计,并去掉setter中的函数调用,但这可能是一个短期解决方案

您可以通过实现ISerializable来使用自定义序列化:这似乎是合理的。非常感谢。