/// <summary> /// Sets the value of the property with provided property metadata. /// </summary> /// <typeparam name="T">The public type of this property.</typeparam> public void Set <T>(PropertyBagMetadata metadata, T value) { // In order to set a property in the property bag // we need to convert the value to the destination type var previousValue = Get(metadata); var newValue = previousValue == null ? value : Convert.ChangeType(value, previousValue.GetType()); // Set the property Write(1, new[] { metadata }, new[] { new Variant { Value = newValue } }); }
/// <summary> /// Gets the value of the property with provided property metadata. /// </summary> /// <returns>Value of the property</returns> public object Get(PropertyBagMetadata metadata) { var value = new Variant[1]; var error = new Result[1]; // Gets the property Read(1, new[] { metadata }, null, value, error); if (error[0].Failure) { throw new InvalidOperationException( string.Format( CultureInfo.InvariantCulture, "Property with name [{0}] is not valid for this instance", metadata.Name ) ); } return(value[0].Value); }
/// <summary> /// Gets the value of the property with provided property metadata. /// </summary> /// <typeparam name="T">The public type of this property.</typeparam> /// <returns>Value of the property</returns> public T Get <T>(PropertyBagMetadata metadata) => (T)Convert.ChangeType(Get(metadata), typeof(T));