Exemplo n.º 1
0
        private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
        {
            var oldValue = handle.DynamicMemberGet(target);

            Type pType = handle.MemberType;

#if NETSTANDARD && !NETSTANDARD2_0
            var isGeneric   = pType.IsGenericType();
            var isPrimitive = pType.IsPrimitive();
            var isValueType = pType.IsValueType();
#else
            var isGeneric   = pType.IsGenericType;
            var isPrimitive = pType.IsPrimitive;
            var isValueType = pType.IsValueType;
#endif
            if (!isGeneric ||
                (isGeneric && pType.GetGenericTypeDefinition() != typeof(Nullable <>)))
            {
                if (isValueType && (isPrimitive || pType == typeof(decimal)) && value == null)
                {
                    value = 0;
                }
            }

            if (value != null)
            {
                Type vType = Utilities.GetPropertyType(value.GetType());
                value = Utilities.CoerceValue(pType, vType, oldValue, value);
            }
            handle.DynamicMemberSet(target, value);
        }
Exemplo n.º 2
0
        private static void SetValueWithCoercion(object target, DynamicMemberHandle handle, object value)
        {
            var oldValue = handle.DynamicMemberGet(target);

            Type pType = handle.MemberType;

            if (!pType.IsGenericType ||
                (pType.IsGenericType && pType.GetGenericTypeDefinition() != typeof(Nullable <>)))
            {
                if (pType.IsValueType && (pType.IsPrimitive || pType == typeof(decimal)) && value == null)
                {
                    value = 0;
                }
            }

            if (value != null)
            {
                Type vType = Utilities.GetPropertyType(value.GetType());
                value = Utilities.CoerceValue(pType, vType, oldValue, value);
            }
            handle.DynamicMemberSet(target, value);
        }