/// <summary> /// Sets the value. /// </summary> /// <param name="target">The target to set the value on.</param> /// <param name="value">The value to set on the target.</param> public void SetValue(object target, object value) { try { if (_setter == null) { _setter = ExpressionReflectionDelegateFactory.Instance.CreateSet <object>(_memberInfo); } #if DEBUG // dynamic method doesn't check whether the type is 'legal' to set // add this check for unit tests if (value == null) { if (!ReflectionUtils.IsNullable(ReflectionUtils.GetMemberUnderlyingType(_memberInfo))) { throw new JsonSerializationException("Incompatible value. Cannot set {0} to null.".FormatWith(CultureInfo.InvariantCulture, _memberInfo)); } } else if (!ReflectionUtils.GetMemberUnderlyingType(_memberInfo).IsAssignableFrom(value.GetType())) { throw new JsonSerializationException("Incompatible value. Cannot set {0} to type {1}.".FormatWith(CultureInfo.InvariantCulture, _memberInfo, value.GetType())); } #endif _setter(target, value); } catch (Exception ex) { throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex); } }
public static void AddObjectClickHandler(this Instance instance, string objectName, Callback callback) { instance.OnObjectClick += (sender, args) => { VpObject obj = args.VpObject; Action action = Action.Parse(obj.Action); string name = action.Create?.GetCommandOfType <NameCommand>()?.Name ?? String.Empty; if (name.Equals(objectName)) { callback(sender, args); } }; }
public static void AddObjectClickHandler <T>(this Instance instance, Action <object, ObjectClickArgsT <Avatar, VpObject>, T> callback) where T : Command { instance.OnObjectClick += (sender, args) => { VpObject obj = args.VpObject; Action action = Action.Parse(obj.Action); T command = action.Activate?.GetCommandOfType <T>(); if (!(command is null)) { callback(sender, args, command); } }; }
public override Serialization.Action <T, object?> CreateSet <T>(PropertyInfo propertyInfo) { ValidationUtils.ArgumentNotNull(propertyInfo, nameof(propertyInfo)); // use reflection for structs // expression doesn't correctly set value if (propertyInfo.DeclaringType.IsValueType()) { return(LateBoundReflectionDelegateFactory.Instance.CreateSet <T>(propertyInfo)); } Type instanceType = typeof(T); Type valueType = typeof(object); ParameterExpression instanceParameter = Expression.Parameter(instanceType, "instance"); ParameterExpression valueParameter = Expression.Parameter(valueType, "value"); Expression readValueParameter = EnsureCastExpression(valueParameter, propertyInfo.PropertyType); MethodInfo?setMethod = propertyInfo.GetSetMethod(true); if (setMethod == null) { throw new ArgumentException("Property does not have a setter."); } Expression setExpression; if (setMethod.IsStatic) { setExpression = Expression.Call(setMethod, readValueParameter); } else { Expression readInstanceParameter = EnsureCastExpression(instanceParameter, propertyInfo.DeclaringType); setExpression = Expression.Call(readInstanceParameter, setMethod, readValueParameter); } LambdaExpression lambdaExpression = Expression.Lambda(typeof(Serialization.Action <T, object?>), setExpression, instanceParameter, valueParameter); Serialization.Action <T, object?> compiled = (Serialization.Action <T, object?>)lambdaExpression.Compile(); return(compiled); }
public override Serialization.Action <T, object?> CreateSet <T>(FieldInfo fieldInfo) { ValidationUtils.ArgumentNotNull(fieldInfo, nameof(fieldInfo)); // use reflection for structs // expression doesn't correctly set value if (fieldInfo.DeclaringType.IsValueType() || fieldInfo.IsInitOnly) { return(LateBoundReflectionDelegateFactory.Instance.CreateSet <T>(fieldInfo)); } ParameterExpression sourceParameterExpression = Expression.Parameter(typeof(T), "source"); ParameterExpression valueParameterExpression = Expression.Parameter(typeof(object), "value"); Expression fieldExpression; if (fieldInfo.IsStatic) { fieldExpression = Expression.Field(null, fieldInfo); } else { Expression sourceExpression = EnsureCastExpression(sourceParameterExpression, fieldInfo.DeclaringType); fieldExpression = Expression.Field(sourceExpression, fieldInfo); } Expression valueExpression = EnsureCastExpression(valueParameterExpression, fieldExpression.Type); BinaryExpression assignExpression = Expression.Assign(fieldExpression, valueExpression); LambdaExpression lambdaExpression = Expression.Lambda(typeof(Serialization.Action <T, object>), assignExpression, sourceParameterExpression, valueParameterExpression); Serialization.Action <T, object?> compiled = (Serialization.Action <T, object?>)lambdaExpression.Compile(); return(compiled); }
public void SetValue(object target, string member, object value) { Serialization.Action <object, object> setter = Members[member].Setter; setter(target, value); }