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:
if (_Value != null && !Property.PropertyType.IsPrimitive && !(_Value is string) && !(_Value is DateTime))
{
_UsingDefaultValue = false;
_ChangedSinceLastSerialized = true;
_IsDirty = true;
}