示例#1
0
        /// <summary>
        /// Sets the member value. Handles field or property type.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="value">The value.</param>
        public void SetMemberValue(object instance, object value)
        {
            var finalMember = MemberPath.GetLastMember(ref instance);

            var type = finalMember.Type;

            if (value != null && type != ScriptType.Null && type != value.GetType())
            {
                // Convert value to ensure it matches the member type (eg. undo that uses json serializer might return different value type for some cases)
                if (type.IsEnum)
                {
                    value = Convert.ChangeType(value, Enum.GetUnderlyingType(type.Type));
                }
                else if (type.Type == typeof(byte))
                {
                    value = Convert.ToByte(value);
                }
                else if (type.Type == typeof(sbyte))
                {
                    value = Convert.ToSByte(value);
                }
                else if (type.Type == typeof(short))
                {
                    value = Convert.ToInt16(value);
                }
                else if (type.Type == typeof(int))
                {
                    value = Convert.ToInt32(value);
                }
                else if (type.Type == typeof(long))
                {
                    value = Convert.ToInt64(value);
                }
                else if (type.Type == typeof(int))
                {
                    value = Convert.ToUInt16(value);
                }
                else if (type.Type == typeof(uint))
                {
                    value = Convert.ToUInt32(value);
                }
                else if (type.Type == typeof(ulong))
                {
                    value = Convert.ToUInt64(value);
                }
                else if (type.Type == typeof(float))
                {
                    value = Convert.ToSingle(value);
                }
                else if (type.Type == typeof(double))
                {
                    value = Convert.ToDouble(value);
                }
            }

            finalMember.SetValue(instance, value);
        }
示例#2
0
        /// <summary>
        /// Sets the member value. Handles field or property type.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="value">The value.</param>
        public void SetMemberValue(object instance, object value)
        {
            var finalMember = MemberPath.GetLastMember(ref instance);

            var type = finalMember.Type;

            if (value != null)
            {
                if (type.IsEnum)
                {
                    value = Convert.ChangeType(value, Enum.GetUnderlyingType(type));
                }
                else if (value is long && type == typeof(int))
                {
                    value = (int)(long)value;
                }
                else if (value is double && type == typeof(float))
                {
                    value = (float)(double)value;
                }
            }

            finalMember.SetValue(instance, value);
        }