Adf.Base.Domain.ValueObjectPropertyParser.SetValue C# (CSharp) Method

SetValue() public method

Sets the new value for a specific property on an object.
public SetValue ( object instance, PropertyInfo pi, object newvalue, CultureInfo culture = null ) : void
instance object Target object to set property at.
pi System.Reflection.PropertyInfo Property info for the property to set.
newvalue object Value to try to parse.
culture System.Globalization.CultureInfo
return void
        public void SetValue(object instance, PropertyInfo pi, object newvalue, CultureInfo culture = null)
        {
            culture = culture ?? CultureInfo.CurrentCulture;

            Type propertyType = pi.PropertyType;

            IValueObject parsedValue = null;

            var args = new object[] {newvalue.ToString(), culture, parsedValue};
            var isValid = (bool) propertyType.InvokeMember("TryParse", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, args, culture);

            parsedValue = args[2] as IValueObject;  // get returned out param from args array

            if (isValid)
            {
                pi.SetValue(instance, parsedValue, null);
            }
            else
            {
                ValidationManager.AddError(pi, "Adf.Business.NotInstantiable", newvalue, pi.Name);
            }
        }