private static object GetNestedObjectInstance(object component, string propertyPath, bool autoCreate) { string propertyName; if (propertyPath.Contains(".")) { propertyName = propertyPath.Substring(0, propertyPath.IndexOf(".")); } else { return(component); } IDynamicAccessor dynamicAccessor = DynamicAccessorFactory.GetDynamicAccessor(component.GetType()); object value = dynamicAccessor.GetPropertyValue(component, propertyName); if (value == null) { if (autoCreate) { PropertyDescriptor descriptor = ReflectionHelper.GetPropertyDescriptorFromPath(component.GetType(), propertyName); value = Activator.CreateInstance(descriptor.PropertyType); dynamicAccessor.SetPropertyValue(component, propertyName, value); } else { return(value); } } return(GetNestedObjectInstance(value, propertyPath.Substring(propertyPath.IndexOf(".") + 1), autoCreate)); }
/// <summary> /// Sets the value of the component to a different value. /// </summary> /// <param name="component">The component with the property value that is to be set.</param> /// <param name="value">The new value.</param> public override void SetValue(object component, object value) { object instance = GetNestedObjectInstance(component, _propertyPath, _autoCreateObjects); if (instance != null) { DynamicAccessorFactory.GetDynamicAccessor(instance.GetType()).SetPropertyValue(instance, _originalPropertyDescriptor.Name, value); } }
/// <summary> /// Determines whether the specified instance is valid. /// </summary> /// <param name="instance">The instance declaring the property to be validated</param> /// <returns> /// <c>true</c> if the specified instance is valid; otherwise, <c>false</c>. /// </returns> public bool IsValid(object instance) { if (instance == null) { throw new ArgumentNullException("instance"); } IDynamicAccessor DynamicAccessor = DynamicAccessorFactory.GetDynamicAccessor(instance.GetType()); object value = DynamicAccessor.GetPropertyValue(instance, _propertyName); return(Validate(value)); }
/// <summary> /// Gets the current value of the property on a component /// </summary> /// <param name="component">The component with the property for which to retrieve the value.</param> /// <returns>The value of a property for a given component.</returns> public override object GetValue(object component) { object instance = GetNestedObjectInstance(component, _propertyPath, false); if (instance != null) { return(DynamicAccessorFactory.GetDynamicAccessor(instance.GetType()).GetPropertyValue(instance, _originalPropertyDescriptor.Name)); } else { return(null); } }