共用方式為


DataContractBase.SetProperty Method

Definition

Overloads

SetProperty<T>(T, T, String)

Changes the property to the given value.
Returns whether the new value is different from the old value.

SetProperty<T>(T, T, String, Boolean)

Changes the property to the given value.
Returns whether the new value is different from the old value.

SetProperty<T>(T, T, String)

Changes the property to the given value.
Returns whether the new value is different from the old value.

protected void SetProperty<T> (ref T propertyBackingField, T newValue, string propertyName = default);
member this.SetProperty : 'T * 'T * string -> unit
Protected Sub SetProperty(Of T) (ByRef propertyBackingField As T, newValue As T, Optional propertyName As String = Nothing)

Type Parameters

T

The property type.

Parameters

propertyBackingField
T

The backing field of the property to change.

newValue
T

The new value to give to the property.

propertyName
String

The name of the property. It is recommended that this value be hardcoded by the method caller, rather than using a less performant (but more refactor-friendly) approach like using reflection or lambda expressions to get the property name. The downside of hardcoding is vulnerability to typo and copy-paste type errors. However, this vulnerability is mitigated by unit tests that use reflection to verify this parameter for all data contract class properties. The idea being that it is better to pay the reflection performance penalty in the unit tests, rather than in production code.

Applies to

SetProperty<T>(T, T, String, Boolean)

Changes the property to the given value.
Returns whether the new value is different from the old value.

protected bool SetProperty<T> (ref T propertyBackingField, T newValue, string propertyName = default, bool emptyStringIsNull = false);
member this.SetProperty : 'T * 'T * string * bool -> bool
Protected Function SetProperty(Of T) (ByRef propertyBackingField As T, newValue As T, Optional propertyName As String = Nothing, Optional emptyStringIsNull As Boolean = false) As Boolean

Type Parameters

T

The property type.

Parameters

propertyBackingField
T

The backing field of the property to change.

newValue
T

The new value to give to the property.

propertyName
String

The name of the property. It is recommended that this value be hardcoded by the method caller, rather than using a less performant (but more refactor-friendly) approach like using reflection or lambda expressions to get the property name. The downside of hardcoding is vulnerability to typo and copy-paste type errors. However, this vulnerability is mitigated by unit tests that use reflection to verify this parameter for all data contract class properties. The idea being that it is better to pay the reflection performance penalty in the unit tests, rather than in production code.

emptyStringIsNull
Boolean

A flag that indicates what to do if newValue happens to be an empty string. If this value is set to true, empty strings are treated the same as null values. That is to say that if the existing value is null and an empty string is set with this flag on, it will have no effect.

Returns

Whether the property was changed. I.e, whether the given new value is different from the property's old value.

Applies to