Esempio n. 1
0
        /// <summary>
        /// Internally used to know type of set operation to use.
        /// </summary>
        public UpdateOperationType GetSetOperationType()
        {
            if (MemberType.GetTypeInfo().IsValueType)
            {
                if (BlittableHelper.IsBlittable(MemberType))
                {
                    if (Size == 4)
                    {
                        return(UpdateOperationType.ConditionalSetBlittableField4);
                    }
                    if (Size == 8)
                    {
                        return(UpdateOperationType.ConditionalSetBlittableField8);
                    }
                    if (Size == 12)
                    {
                        return(UpdateOperationType.ConditionalSetBlittableField12);
                    }
                    if (Size == 16)
                    {
                        return(UpdateOperationType.ConditionalSetBlittableField16);
                    }

                    return(UpdateOperationType.ConditionalSetBlittableField);
                }

                return(UpdateOperationType.ConditionalSetStructField);
            }
            else
            {
                return(UpdateOperationType.ConditionalSetObjectField);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a method that gets the property from the object.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <returns>An accessor function.</returns>
        public Func <TObject, TValue> CreateGetMethod <TObject, TValue>()
        {
            var dm = new DynamicMethod(
                String.Format(CultureInfo.InvariantCulture, "Get-{0}-{1}-{2}", typeof(TObject).FullName, Name, Guid.NewGuid()),
                typeof(TValue),
                new Type[] { typeof(TObject) },
                true);
            var il = dm.GetILGenerator();

            il.Emit(OpCodes.Ldarg_0);
            if (FieldInfo != null)
            {
                il.Emit(OpCodes.Ldfld, FieldInfo);
            }
            else
            {
                il.Emit(OpCodes.Callvirt, GetMethodInfo);
            }

            if (typeof(TValue) == typeof(object) && MemberType.GetTypeInfo().IsValueType)
            {
                il.Emit(OpCodes.Box, MemberType);
            }

            il.Emit(OpCodes.Ret);

            return((Func <TObject, TValue>)dm.CreateDelegate(typeof(Func <TObject, TValue>)));
        }
Esempio n. 3
0
        public DuplicatedField(EnumStorage enumStorage, MemberInfo[] memberPath) : base(memberPath)
        {
            _enumStorage = enumStorage;
            _dbType      = TypeMappings.ToDbType(MemberType);


            ColumnName = MemberName.ToTableAlias();

            if (MemberType.GetTypeInfo().IsEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };

                _dbType = NpgsqlDbType.Varchar;
                PgType  = "varchar";
            }
            else if (MemberType.IsDateTime())
            {
                PgType  = "timestamp with time zone";
                _dbType = NpgsqlDbType.TimestampTZ;
            }
        }
Esempio n. 4
0
        public JsonLocatorField(EnumStorage enumStyle, MemberInfo[] members) : base(members)
        {
            var locator = "d.data";


            if (members.Length == 1)
            {
                locator += $" ->> '{members.Single().Name}'";
            }
            else
            {
                for (int i = 0; i < members.Length - 1; i++)
                {
                    locator += $" -> '{members[i].Name}'";
                }

                locator += $" ->> '{members.Last().Name}'";
            }



            SqlLocator = MemberType == typeof(string) ? locator : locator.ApplyCastToLocator(enumStyle, MemberType);


            var isStringEnum = MemberType.GetTypeInfo().IsEnum&& enumStyle == EnumStorage.AsString;

            if (isStringEnum)
            {
                _parseObject = expression =>
                {
                    var raw = expression.Value();
                    return(Enum.GetName(MemberType, raw));
                };
            }
        }
Esempio n. 5
0
 /// <inheritdoc/>
 internal override UpdateOperationType GetEnterOperationType()
 {
     if (MemberType.GetTypeInfo().IsValueType)
     {
         return(UpdateOperationType.EnterStructPropertyBase);
     }
     else
     {
         return(UpdateOperationType.EnterObjectCustom);
     }
 }
Esempio n. 6
0
        /// <inheritdoc/>
        internal override UpdateOperationType GetSetOperationType()
        {
            if (MemberType.GetTypeInfo().IsValueType)
            {
                if (BlittableHelper.IsBlittable(MemberType))
                {
                    return(UpdateOperationType.ConditionalSetBlittablePropertyBase);
                }

                return(UpdateOperationType.ConditionalSetStructPropertyBase);
            }
            else
            {
                return(UpdateOperationType.ConditionalSetObjectCustom);
            }
        }
Esempio n. 7
0
        internal bool ResolveMapTypes(out Type dictionaryType, out Type keyType, out Type valueType)
        {
            dictionaryType = keyType = valueType = null;
            try
            {
#if COREFX || PROFILE259
                var info = MemberType.GetTypeInfo();
#else
                var info = MemberType;
#endif
                if (ImmutableCollectionDecorator.IdentifyImmutable(MemberType, out _, out _, out _, out _, out _, out _))
                {
                    return(false);
                }
                if (info.IsInterface && info.IsGenericType && info.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                {
#if PROFILE259
                    var typeArgs = MemberType.GetGenericTypeDefinition().GenericTypeArguments;
#else
                    var typeArgs = MemberType.GetGenericArguments();
#endif
                    if (IsValidMapKeyType(typeArgs[0]))
                    {
                        keyType        = typeArgs[0];
                        valueType      = typeArgs[1];
                        dictionaryType = MemberType;
                    }
                    return(false);
                }
#if PROFILE259
                foreach (var iType in MemberType.GetTypeInfo().ImplementedInterfaces)
#else
                foreach (var iType in MemberType.GetInterfaces())
#endif
                {
#if COREFX || PROFILE259
                    info = iType.GetTypeInfo();
#else
                    info = iType;
#endif

                    if (info.IsGenericType && info.GetGenericTypeDefinition() == typeof(IDictionary <,>))
                    {
                        if (dictionaryType != null)
                        {
                            throw new InvalidOperationException("Multiple dictionary interfaces implemented by type: " + MemberType.FullName);
                        }
#if PROFILE259
                        var typeArgs = iType.GetGenericTypeDefinition().GenericTypeArguments;
#else
                        var typeArgs = iType.GetGenericArguments();
#endif
                        if (IsValidMapKeyType(typeArgs[0]))
                        {
                            keyType        = typeArgs[0];
                            valueType      = typeArgs[1];
                            dictionaryType = MemberType;
                        }
                    }
                }
                if (dictionaryType == null)
                {
                    return(false);
                }

                // (note we checked the key type already)
                // not a map if value is repeated
                Type itemType = null, defaultType = null;
                model.ResolveListTypes(valueType, ref itemType, ref defaultType);
                if (itemType != null)
                {
                    return(false);
                }

                return(dictionaryType != null);
            }
            catch
            {
                // if it isn't a good fit; don't use "map"
                return(false);
            }
        }