using System; using System.Globalization; using System.Reflection; using DPumpHydr.WinFrmUI.WenSkin.Json.Utilities; namespace DPumpHydr.WinFrmUI.WenSkin.Json.Serialization { /// /// Get and set values for a using reflection. /// public class ReflectionValueProvider : IValueProvider { private readonly MemberInfo _memberInfo; /// /// Initializes a new instance of the class. /// /// The member info. public ReflectionValueProvider(MemberInfo memberInfo) { ValidationUtils.ArgumentNotNull(memberInfo, "memberInfo"); _memberInfo = memberInfo; } /// /// Sets the value. /// /// The target to set the value on. /// The value to set on the target. public void SetValue(object target, object value) { try { ReflectionUtils.SetMemberValue(_memberInfo, target, value); } catch (Exception innerException) { throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), innerException); } } /// /// Gets the value. /// /// The target to get the value from. /// The value. public object GetValue(object target) { try { return ReflectionUtils.GetMemberValue(_memberInfo, target); } catch (Exception innerException) { throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), innerException); } } } }