Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.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# 为什么读取设置属性值会使SettingsPropertyValue对象变脏?_C#_Settingsprovider - Fatal编程技术网

C# 为什么读取设置属性值会使SettingsPropertyValue对象变脏?

C# 为什么读取设置属性值会使SettingsPropertyValue对象变脏?,c#,settingsprovider,C#,Settingsprovider,在实现自定义设置提供程序时,我注意到访问设置属性的值会将其IsDirty标志更改为true // Arrange var property = new SettingsProperty("property1") { PropertyType = typeof(Color), DefaultValue = "Green" }; // Act var result = new SettingsPropertyValue(property); // Assert Assert.Th

在实现自定义设置提供程序时,我注意到访问设置属性的值会将其
IsDirty
标志更改为
true

// Arrange
var property = new SettingsProperty("property1")
{
    PropertyType = typeof(Color),
    DefaultValue = "Green"
};

// Act
var result = new SettingsPropertyValue(property);

// Assert
Assert.That(result.IsDirty, Is.False);
Assert.That(result.PropertyValue, Is.EqualTo(Color.Green));
Assert.That(result.IsDirty, Is.False); // <-- Assertion fails
有人能对这种乍一看很奇怪的行为有所了解吗?

说明这是为了解释当值是复杂类型时访问该值以及在不重新分配值的情况下更改该值(例如,在修改列表中的项目时)可能产生的任何副作用:

在以下条件下,IsDirty属性设置为true

  • [……]

  • 访问SettingsPropertyValue对象中包含的值,该值不是字符串或基本类型,例如intfloatrealDateTime。当SettingsPropertyValue对象管理的值是复杂类型(例如ArrayList)时,SettingsPropertyValue对象无法检测何时进行了更改。因此,SettingsPropertyValue对象悲观地假设,一旦从PropertyValue属性访问了复杂类型,该类型即为脏类型

  • 这是来源:
    if (_Value != null && !Property.PropertyType.IsPrimitive && !(_Value is string) && !(_Value is DateTime))
    {
        _UsingDefaultValue = false;
        _ChangedSinceLastSerialized = true;
        _IsDirty = true;
    }