/// <summary> /// Sets the value to the property of the target object /// </summary> /// <param name="target">The object to set the property</param> /// <param name="propertyName">The name of the property to set</param> /// <param name="value">The value to set</param> public void SetValue(object target, string propertyName, object value) { if (propertyName.Contains(".")) // Support nested properties { var propertyNames = propertyName.Split('.'); var typeAccessor = this; // The type accessor needs to change for each new target foreach (var name in propertyNames) { if (!typeAccessor.PropertyAccessors.ContainsKey(name)) { throw new InvalidOperationException($"Property: '{name}' not found for object of type: '{target.GetType().FullName}'"); } var propertyAccessor = typeAccessor.PropertyAccessors[name]; if (!propertyAccessor.CanSet) { throw new InvalidOperationException($"Can not set the value of property: '{propertyAccessor.PropertyName}'.Verify that the declaring type is not a value type(such as struct)"); } if (name == propertyNames.Last()) // Assume the last property as a scalar (primitive) value { propertyAccessor.SetValue(target, value); } else { var t = propertyAccessor.GetValue(target); if (t == null) // Nested property instance does not exist { t = Activator.CreateInstance(propertyAccessor.PropertyType); propertyAccessor.SetValue(target, t); } target = t; typeAccessor = t.GetTypeAccessor(); } } } else { if (!PropertyAccessors.ContainsKey(propertyName)) { throw new InvalidOperationException($"Property: '{propertyName}' not found for object of type: '{target.GetType().FullName}'"); } var propertyAccessor = PropertyAccessors[propertyName]; if (!propertyAccessor.CanSet) { throw new InvalidOperationException($"Can not set the value of property: '{propertyAccessor.PropertyName}'.Verify that the declaring type is not a value type(such as struct)"); } propertyAccessor.SetValue(target, value); } }
/// <summary> /// Retrieves a value /// </summary> /// <param name="target">The object to retrieve the value from</param> /// <param name="propertyName">The name of the property to get the value from</param> /// <returns>The value of the property</returns> public object GetValue(object target, string propertyName) { if (propertyName.Contains(".")) // Support nested properties { var propertyNames = propertyName.Split('.'); var typeAccessor = this; // The type accessor needs to change for each new target PropertyAccessor propertyAccessor = null; foreach (var name in propertyNames) { if (!typeAccessor.PropertyAccessors.ContainsKey(name)) { throw new InvalidOperationException($"Property: '{name}' not found for object of type: '{target.GetType().FullName}'"); } propertyAccessor = typeAccessor.PropertyAccessors[name]; if (!propertyAccessor.CanGet) { throw new InvalidOperationException($"Can not get the value of property: '{propertyAccessor.PropertyName}'"); } if (name != propertyNames.Last()) // Assume the last property as a scalar (primitive) value { var t = propertyAccessor.GetValue(target); if (t == null) // Nested property instance does not exist { return(null); } target = t; typeAccessor = t.GetTypeAccessor(); } } return(propertyAccessor.GetValue(target)); } else { if (!PropertyAccessors.ContainsKey(propertyName)) { throw new InvalidOperationException($"Property: '{propertyName}' not found for object of type: '{target.GetType().FullName}'"); } PropertyAccessor propertyAccessor = PropertyAccessors[propertyName]; if (!propertyAccessor.CanGet) { throw new InvalidOperationException($"Can not get the value of property: '{propertyAccessor.PropertyName}'"); } return(propertyAccessor.GetValue(target)); } }
/// <summary> /// Testst whether the type accessor has the property name /// </summary> /// <param name="propertyName">The name of the property to test for</param> /// <returns>true if the type accessor contains the property, false otherwise</returns> public bool Contains(string propertyName) { return(PropertyAccessors.ContainsKey(propertyName)); }