/// <summary> /// Gets the value of the property with the given name for the given container. /// </summary> /// <param name="container">The container hosting the data.</param> /// <param name="name">The property name to get.</param> /// <param name="value">Contains the value if the property is found and the type can be converted; otherwise this is set to default.</param> /// <returns>True if the property was found and the value was converted.</returns> public static bool TryGetValue <TContainer, TValue>(ref TContainer container, string name, out TValue value) { var propertyBag = PropertyBagResolver.Resolve <TContainer>(); if (null == propertyBag) { value = default; return(false); } var changeTracker = new ChangeTracker(); var action = new GetValueAction <TContainer, TValue>(); if (!PropertyBagResolver.Resolve <TContainer>().FindProperty(name, ref container, ref changeTracker, ref action)) { value = default; return(false); } value = action.DstValue; return(action.Result == k_ResultSuccess); }
/// <summary> /// Gets the value of the property with the given name for the given container. /// </summary> /// <param name="container">The container hosting the data.</param> /// <param name="name">The property name to get.</param> /// <returns>The value of the property converted to the given type.</returns> public static TValue GetValue <TContainer, TValue>(ref TContainer container, string name) { var propertyBag = PropertyBagResolver.Resolve <TContainer>(); if (null == propertyBag) { throw new Exception($"Failed to resolve property bag for ContainerType=[{typeof(TContainer)}]"); } var changeTracker = new ChangeTracker(); var action = new GetValueAction <TContainer, TValue>(); if (!PropertyBagResolver.Resolve <TContainer>().FindProperty(name, ref container, ref changeTracker, ref action)) { throw new Exception($"Failed to find property Name=[{name}] for ContainerType=[{typeof(TContainer)}]"); } if (action.Result == k_ResultErrorConvert) { throw new Exception($"Failed get ValueType=[{typeof(TValue)}] from property Name=[{name}] for ContainerType=[{typeof(TContainer)}]"); } return(action.DstValue); }