예제 #1
0
        /// <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);
            }
        }
예제 #2
0
        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);
                }
            };
        }
예제 #3
0
        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);
                }
            };
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
 public void SetValue(object target, string member, object value)
 {
     Serialization.Action <object, object> setter = Members[member].Setter;
     setter(target, value);
 }