Example #1
0
        private Func <ParameterExpression, Expression> CloneListElementDelegate(IExpressionBuilder expressionBuilder, CloneExpressionContext context)
        {
            Type elementType = context
                               .SourceEntity
                               .Type
                               .GenericTypeArguments
                               .Single();
            PropertyInfo indexProperty = typeof(IList <>)
                                         .MakeGenericType(elementType)
                                         .GetProperty(IndexPropertyName);
            MethodInfo addMethod = typeof(ICollection <>)
                                   .MakeGenericType(elementType)
                                   .GetMethod(nameof(ICollection <object> .Add));

            if (PrimitiveTypes.Contains(elementType))
            {
                return (iterator) =>
                       {
                           Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator);
                           return ExpressionFactory.Call(context.TargetEntity, addMethod, getElementExpression);
                       }
            }
            ;
            else
            {
                return (iterator) =>
                       {
                           Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator);
                           MethodInfo cloneMethod          = CloneMethod.MakeGenericMethod(elementType);
                           Expression clonedElement        = ExpressionFactory.Call(context.CloneFactory, cloneMethod, getElementExpression);
                           return ExpressionFactory.Call(context.TargetEntity, addMethod, clonedElement);
                       }
            };
        }
Example #2
0
        private Func <ParameterExpression, Expression> CloneArrayElementDelegate(CloneExpressionContext context)
        {
            Type elementType = context
                               .SourceEntity
                               .Type
                               .GetElementType();
            PropertyInfo indexProperty = typeof(IList <>)
                                         .MakeGenericType(elementType)
                                         .GetProperty(IndexPropertyName);

            if (PrimitiveTypes.Contains(elementType))
            {
                return (iterator) =>
                       {
                           Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator);
                           return ExpressionFactory.Call(context.TargetEntity, indexProperty.SetMethod, iterator, getElementExpression);
                       }
            }
            ;
            else
            {
                return (iterator) =>
                       {
                           Expression getElementExpression = ExpressionFactory.Call(context.SourceEntity, indexProperty.GetMethod, iterator);
                           MethodInfo cloneMethod          = CloneMethod.MakeGenericMethod(elementType);
                           Expression clonedElement        = ExpressionFactory.Call(context.CloneFactory, cloneMethod, getElementExpression);
                           return ExpressionFactory.Call(context.TargetEntity, indexProperty.SetMethod, iterator, clonedElement);
                       }
            };
        }
Example #3
0
 /// <summary>
 /// True if the type is a primitive (nullable or non-nullable) type.
 /// This class' PrimitiveTypes or PrimitiveNullableTypes contain the types against which the passed-in type is compared.
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsPrimitive(Type type)
 {
     return
         (PrimitiveTypes.Contains(type)
          ||
          PrimitiveNullableTypes.Contains(type)
         );
 }
Example #4
0
        public static List <PropertyInfo> GetComplexProps(Type type)
        {
            if (!_complexProps.Keys.Contains(type.FullName))
            {
                _complexProps.Add(type.FullName, GetProps(type).Where(p => !PrimitiveTypes.Contains(p.PropertyType) && !PrimitiveNullableTypes.Contains(p.PropertyType)).ToList());
            }

            return(_complexProps[type.FullName]);
        }
Example #5
0
        /// <summary>
        /// Tests whether the type is a primitive one from the business logic view.
        /// (Business logic view sees structures such as DateTime, Guid or enumerations as primitives)
        /// </summary>
        /// <param name="type">The type to be tested</param>
        /// <returns>true if the type is a primitive according to business logic view, false otherwise</returns>
        public static bool IsPrimitive(this Type type)
        {
            if (type.IsNullable()) // Unbox the nullable type
            {
                type = type.GetNullableType();
            }

            return(type.IsEnum ||
                   PrimitiveTypes.Contains(type));
        }
Example #6
0
 private void CloneProperty(IExpressionBuilder expressionBuilder, CloneExpressionContext parameters, PropertyInfo property)
 {
     if (PrimitiveTypes.Contains(property.PropertyType))
     {
         ClonePrimitiveProperty(expressionBuilder, parameters, property);
     }
     else
     {
         CloneComplexProperty(expressionBuilder, parameters, property);
     }
 }
Example #7
0
 protected override IEnumerable <PropertyInfo> FilterComplexTypes(IEnumerable <PropertyInfo> properties)
 {
     foreach (var propertyInfo in properties)
     {
         Type type = propertyInfo.PropertyType;
         type = Nullable.GetUnderlyingType(type) ?? type;
         if (type.IsPrimitive || type.IsEnum || PrimitiveTypes.Contains(type))
         {
             yield return(propertyInfo);
         }
     }
 }
        public static bool IsPrimitiveType(Type type)
        {
            if (PrimitiveTypes.Contains(type) ||
                type.IsEnum)
            {
                return(true);
            }
            var fullName = type.FullName;

            if (fullName.Length < 8 || fullName[7] != 'F' && fullName[7] != 'A')
            {
                return(false);
            }
            return(fullName.StartsWith("System.Func`") || type.FullName.StartsWith("System.Action`"));
        }
        public static bool IsPrimitive(this Type type)
        {
            Contract.Requires(type != null);

            return(type.GetTypeInfo().IsPrimitive || PrimitiveTypes.Contains(type));
        }
Example #10
0
 public static bool IsPrimitiveType(Type t)
 {
     return(PrimitiveTypes.Contains(t));
 }
Example #11
0
        public TypeRef GetTypeRef(Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            TypeRef theReturn;

            //lock (cacheLocker)
            //{
            //    if (namesByTypeCache.TryGetValue(type, out theReturn))
            //    {
            //        return theReturn;
            //    }
            //}
            if (type.IsArray)
            {
                var wrappedType = GetTypeRef(type.GetElementType());
                theReturn = wrappedType.Clone();
                theReturn.ArrayDimensions = type.GetArrayRank();
            }
            else
            {
                theReturn = new TypeRef()
                {
                    Namespace = type.Namespace,
                    IsConstructedGenericType = type.IsConstructedGenericType && !type.IsEnum,
                    IsGenericTypeDefinition  = type.IsGenericTypeDefinition && !type.IsEnum,
                    IsGenericParameter       = type.IsGenericParameter && !type.IsEnum,
                    IsEnum      = type.IsEnum,
                    IsInterface = type.IsInterface
                };
                var indexOfAccent = type.Name.IndexOf('`');
                if (indexOfAccent > 0)
                {
                    theReturn.Name = type.Name.Substring(0, indexOfAccent);
                }
                else
                {
                    theReturn.Name = type.Name;
                }

                if (type.IsNested && !type.IsGenericParameter)
                {
                    theReturn.NestedIn = GetTypeRef(type.DeclaringType);
                }

                if (type.IsGenericTypeDefinition)
                {
                    theReturn.GenericParameters = type.GetGenericArguments().Select(t => GetTypeRef(t)).ToArray();
                }
                else if (type.IsConstructedGenericType)
                {
                    theReturn.Wrapped = type.GetGenericArguments().Select(GetTypeRef).ToArray();
                }

                theReturn.IsPrimitive = PrimitiveTypes.Contains(type);
                theReturn.IsWellKnown = WellKnownAssemblies.Contains(type.Assembly);
            }
            lock (cacheLocker)
            {
                typesByNameCache[theReturn] = type;
                //namesByTypeCache[type] = theReturn;
            }
            return(theReturn);
        }
Example #12
0
        /// <inheritdoc/>
        protected override IEnumerable <NonaProperty> FilterComplexTypes(Type type)
        {
            foreach (var property in type.GetProperties())
            {
                var propertyType = property.PropertyType;
                propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;

                if (propertyType.GetTypeInfo().IsPrimitive || propertyType.GetTypeInfo().IsEnum || PrimitiveTypes.Contains(propertyType))
                {
                    yield return(new NonaProperty(type, property));
                }
            }
        }