KGy SOFT

ObservableObjectBase Class

KGy SOFT Core Libraries Help
Provides a base class for component model classes, which can notify their consumer about property changes.
See the Remarks section for details and examples.
Inheritance Hierarchy

SystemObject
  KGySoft.ComponentModelObservableObjectBase
    KGySoft.ComponentModelPersistableObjectBase

Namespace:  KGySoft.ComponentModel
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0-rc.1
Syntax

public abstract class ObservableObjectBase : INotifyPropertyChanged, 
	IDisposable, ICloneable

The ObservableObjectBase type exposes the following members.

Constructors

  NameDescription
Protected methodObservableObjectBase
Initializes a new instance of the ObservableObjectBase class
Top
Properties

  NameDescription
Public propertyIsModified
Gets whether this instance has been modified. Modified state can be set by the SetModified method.
Public propertyStatic memberMissingProperty
Represents the value of a missing property value. Can be returned in PropertyChangedExtendedEventArgs by the PropertyChanged event if the stored value of the property has just been created and had no previous value, or when a property has been removed from the inner storage.
Top
Methods

  NameDescription
Protected methodAffectsModifiedState
Gets whether the change of the specified propertyName affects the IsModified property.
The ObservableObjectBase implementation excludes the IsModified property itself.
Protected methodCanGetProperty
Gets whether the specified property can be get.
The base implementation allows to get the actual instance properties in this instance.
Protected methodCanSetProperty
Gets whether the specified property can be set.
The base implementation allows to set the actual instance properties in this instance if the specified value is compatible with the property type.
Public methodClone
Creates a new object that is a copy of the current instance.
The base implementation clones the internal property storage, the IsModified property and if clonePropertyChanged is , then also the subscribers of the PropertyChanged event.
Public methodDispose
Releases the resources held by this instance.
Protected methodDispose(Boolean)
Releases the resources held by this instance.
The base implementation removes the subscribers of the PropertyChanged event.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Protected methodGetT(FuncT, String)
Gets the value of a property, or - if it was not set before -, then creates its initial value. The created initial value will be stored in the internal property storage without triggering the PropertyChanged event. For constant or simple expressions, or to return a default value for a non-existing property without storing it internally use the other Get overload.
For an example, see the Remarks section of the ObservableObjectBase class.
Protected methodGetT(T, String)
Gets the value of a property or defaultValue if no value is stored for it. No new value will be stored if the property does not exist. If the default initial value is too complex and should not be evaluated every time when the property is get, or to throw an exception for an uninitialized property use the other Get overload.
For an example, see the Remarks section of the ObservableObjectBase class.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Protected methodOnPropertyChanged
Raises the PropertyChanged event.
Protected methodResetProperty
Resets the property of the specified name, meaning, it will be removed from the underlying storage so the getter methods will return the default value again.
Protected methodResumeChangedEvent
Resumes the raising of the PropertyChanged event suspended by the SuspendChangeEvents method.
Protected methodSet
Sets the value of a property.
For an example, see the Remarks section of the ObservableObjectBase class.
Public methodSetModified
Sets the modified state of this ObservableObjectBase instance represented by the IsModified property.
Protected methodSuspendChangedEvent
Suspends the raising of the PropertyChanged event until ResumeChangeEvents method is called. Supports nested calls.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Events

  NameDescription
Public eventPropertyChanged
Occurs when a property value changed. The actual type of the event argument is PropertyChangedExtendedEventArgs.
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodCreatePropertyBinding(String, String, Object)Overloaded.
Creates a special binding for the PropertyChanged event of the specified source, which allows to update the specified targetPropertyName in the targets, when the property of sourcePropertyName changes in the source.
(Defined by Command.)
Public Extension MethodCreatePropertyBinding(String, String, FuncObject, Object, Object)Overloaded.
Creates a special binding for the PropertyChanged event of the specified source, which allows to update the specified targetPropertyName in the targets, when the property of sourcePropertyName changes in the source.
(Defined by Command.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions.)
Top
Remarks

Implementers can use the Get and Set methods in the property accessors to manage event raising automatically.

Consumers can subscribe the PropertyChanged event to get notification about the property changes.

Accessing properties can be fine tuned by overriding the CanGetProperty and CanSetProperty methods. By default they allow accessing the instance properties in the implementer class.

Note Notes to Inheritors
Do not use CanGetProperty and CanSetProperty methods for property validation. To be able to validate property values consider to use the ValidatingObjectBase or ModelBase classes.

Examples

The following example shows a possible implementation of a derived class.
C#
public class MyModel : ObservableObjectBase
{
    // A simple integer property (with zero default value). Until the property is set no value is stored internally.
    public int IntProperty { get => Get<int>(); set => Set(value); }

    // An int property with a specified default value. Until the property is set the default will be returned.
    public int IntPropertyCustomDefault { get => Get(-1); set => Set(value); }

    // If the default value is a complex one, which should not be evaluated each time you can provide a factory for it:
    // When this property is read for the first time without setting it before, the provided delegate will be invoked
    // and the returned default value is stored without triggering the PropertyChanged event.
    public MyComplexType ComplexProperty { get => Get(() => new MyComplexType()); set => Set(value); }

    // You can use regular properties to prevent raising the events and not to store the value in the internal storage.
    // The OnPropertyChanged method still can be called explicitly to raise the PropertyChanged event.
    public int UntrackedProperty { get; set; }
}
Thread Safety

Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.
See Also

Reference