gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:27:16 26 4
gpt4 key购买 nike

在实现自定义设置提供程序时,我注意到访问设置属性的值会将其 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

Reflector 给了我一个问题的答案,为什么 PropertyValue getter 的行为是这样的 - 它包含如下语句:

if (_Value != null && !Property.PropertyType.IsPrimitive && !(_Value is string) && !(_Value is DateTime))
{
_UsingDefaultValue = false;
_ChangedSinceLastSerialized = true;
_IsDirty = true;
}

谁能解释一下这个乍一看奇怪的行为?

最佳答案

documentation指出这是为了考虑访问复杂类型时的值的任何潜在副作用,以及在不重新分配值的情况下改变值(例如,在修改列表中的项目时):

The IsDirty property is set to true under the following conditions:

  1. [...]

  2. The value contained in the SettingsPropertyValue object is accessed, and the value is not a string or a primitive type such as int, float, real, or DateTime. When the value managed by a SettingsPropertyValue object is a complex type (for example an ArrayList), there is no way for a SettingsPropertyValue object to detect when changes have been made. As a result, the SettingsPropertyValue object pessimistically assumes that a complex type is dirty once it has been accessed from the PropertyValue property.

关于c# - 为什么读取设置属性值会使 SettingsPropertyValue 对象变脏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33262027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com