示例#1
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule rule)
        {
            rule = null;
            //check that it is a class and not an array as arrays should be handled by other rule
            if (!destinationType.IsClass || destinationType.IsArray || !sourceType.IsClass || sourceType.IsArray)
                return false;

            //it source and the destinations must implement IEnumerable
            if (!destinationType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) ||
                !sourceType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)))
                return false;

            //and must have one generic type parameter (that will exclude dictionaries and othe special
            //IEnumerable implementations)
            if (destinationType.GetGenericArguments().Count() != 1 || sourceType.GetGenericArguments().Count() != 1)
                return false;

            Type genericSourceType = sourceType.GetGenericArguments().Single();
            Type genericDestinationType = destinationType.GetGenericArguments().Single();

            IMemberMappingRule innerRule;
            bool canMapGenerics = RuleProvider.GetApplicableRule(genericSourceType, genericDestinationType, out innerRule);
            if (canMapGenerics)
            {
                rule = new EnumerableRule(sourceType, destinationType, innerRule);
            }

            return canMapGenerics;
        }
示例#2
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;
            if ((destinationType.IsClass || destinationType.IsValueType) &&
                (sourceType.IsClass || sourceType.IsValueType))
            {
                mappingRule = new MapClassRule(sourceType, destinationType);
                return true;
            }

            return false;
        }
示例#3
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;
            if ((destinationType.IsClass || destinationType.IsValueType) &&
                (sourceType.IsClass || sourceType.IsValueType))
            {
                mappingRule = new MapClassRule(sourceType, destinationType);
                return(true);
            }

            return(false);
        }
示例#4
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule rule)
        {
            rule = null;
            if (sourceType.Equals(destinationType) ||
                IsAssignableFrom(sourceType, destinationType) ||
                destinationType.IsAssignableFrom(sourceType)) //TODO: decide if this line is valid or not
            {
                rule = new SimpleRule();
                return true;
            }

            return false;
        }
示例#5
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule rule)
        {
            rule = null;
            if (sourceType.Equals(destinationType) ||
                IsAssignableFrom(sourceType, destinationType) ||
                destinationType.IsAssignableFrom(sourceType))                 //TODO: decide if this line is valid or not
            {
                rule = new SimpleRule();
                return(true);
            }

            return(false);
        }
示例#6
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;

            if ((destinationType.IsEnum && sourceType.IsEnum) ||
                (destinationType.Equals(typeof(decimal)) && sourceType.IsPrimitive))
            {
                mappingRule = new CastRule(destinationType);
                return(true);
            }

            return(false);
        }
示例#7
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;

            if ((destinationType.IsEnum && sourceType.IsEnum) ||
                (destinationType.Equals(typeof(decimal)) && sourceType.IsPrimitive))
            {
                mappingRule = new CastRule(destinationType);
                return true;
            }

            return false;
        }
示例#8
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;

            //for primitive types
            if (destinationType.IsPrimitive && sourceType.IsGenericType &&
                destinationType.IsAssignableFrom(sourceType.GetGenericArguments()[0]))
            {
                mappingRule = new FromNullableRule(sourceType);
                return true;
            }

            //for value types
            if (destinationType.IsValueType && sourceType.IsGenericType &&
                destinationType.IsAssignableFrom(sourceType.GetGenericArguments()[0]))
            {
                mappingRule = new FromNullableRule(sourceType);
                return true;
            }

            return false;
        }
示例#9
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;

            //for primitive types
            if (destinationType.IsPrimitive && sourceType.IsGenericType &&
                destinationType.IsAssignableFrom(sourceType.GetGenericArguments()[0]))
            {
                mappingRule = new FromNullableRule(sourceType);
                return(true);
            }

            //for value types
            if (destinationType.IsValueType && sourceType.IsGenericType &&
                destinationType.IsAssignableFrom(sourceType.GetGenericArguments()[0]))
            {
                mappingRule = new FromNullableRule(sourceType);
                return(true);
            }

            return(false);
        }
示例#10
0
        public static bool TryApplyRule(Type sourceType, Type destinationType, out IMemberMappingRule rule)
        {
            rule = null;
            //check that it is a class and not an array as arrays should be handled by other rule
            if (!destinationType.IsClass || destinationType.IsArray || !sourceType.IsClass || sourceType.IsArray)
            {
                return(false);
            }

            //it source and the destinations must implement IEnumerable
            if (!destinationType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) ||
                !sourceType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)))
            {
                return(false);
            }

            //and must have one generic type parameter (that will exclude dictionaries and othe special
            //IEnumerable implementations)
            if (destinationType.GetGenericArguments().Count() != 1 || sourceType.GetGenericArguments().Count() != 1)
            {
                return(false);
            }

            Type genericSourceType      = sourceType.GetGenericArguments().Single();
            Type genericDestinationType = destinationType.GetGenericArguments().Single();

            IMemberMappingRule innerRule;
            bool canMapGenerics = RuleProvider.GetApplicableRule(genericSourceType, genericDestinationType, out innerRule);

            if (canMapGenerics)
            {
                rule = new EnumerableRule(sourceType, destinationType, innerRule);
            }

            return(canMapGenerics);
        }
示例#11
0
        public static bool GetApplicableRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;
            //first check for primitive type
            #region primitives
            if (destinationType.GetNullableType().IsPrimitive)
            {
                //first check if the types are assignable
                //if you update the rules here consider updating them for assignable value types
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                    FromNullableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion Primitives
            //mapping to decimal requires special handling, mapping from decimal is not supported at this moment
            #region mapping to decimal
            else if (destinationType.Equals(typeof(decimal)))
            {
                CastRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion mapping to decimal
            //mapping for String members
            #region String
            else if (destinationType.Equals(typeof(string)) && sourceType.Equals(typeof(string)))
            {
                SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion String
            //mapping enums
            #region Enums
            else if (destinationType.IsEnum && sourceType.IsEnum)
            {
                //if enum types are same apply the simple rule
                //otherwise cast to destination
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                    CastRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion Enums
            //assignable value types
            #region assignable value types
            else if (destinationType.GetNullableType().IsValueType && sourceType.GetNullableType().IsValueType &&
                destinationType.GetNullableType().IsAssignableFrom(sourceType.GetNullableType()))
            {
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                    FromNullableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion same value tyeps
            //for source and destination being objects or structures
            #region object/structure
            else if ((destinationType.IsClass || destinationType.IsValueType) && (sourceType.IsClass || sourceType.IsValueType))
            {
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                    if (destinationType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) &&
                        sourceType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)))
                    {
                        //GetElementType()

                        if (destinationType.GetInterfaces().Contains(typeof(System.Collections.IDictionary)))
                        {
                            throw new NotImplementedException("Dictionary is not supported yet.");
                        }

                        EnumerableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                    }
                    else
                    {
                        //will setup the Map<> for this types, but unfortunately we can't check them at this point
                        MapClassRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                    }
                }
            #endregion object/structure

            if (mappingRule != null) return true;
            return false;
        }
示例#12
0
 protected EnumerableRule(Type sourceType, Type destinationType, IMemberMappingRule innerRule)
 {
     SourceType = sourceType;
     DestinationType = destinationType;
     this.innerRule = innerRule;
 }
示例#13
0
        public static bool GetApplicableRule(Type sourceType, Type destinationType, out IMemberMappingRule mappingRule)
        {
            mappingRule = null;
            //first check for primitive type
            #region primitives
            if (destinationType.GetNullableType().IsPrimitive)
            {
                //first check if the types are assignable
                //if you update the rules here consider updating them for assignable value types
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                {
                    FromNullableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                }
            }
            #endregion Primitives
            //mapping to decimal requires special handling, mapping from decimal is not supported at this moment
            #region mapping to decimal
            else if (destinationType.Equals(typeof(decimal)))
            {
                CastRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion mapping to decimal
            //mapping for String members
            #region String
            else if (destinationType.Equals(typeof(string)) && sourceType.Equals(typeof(string)))
            {
                SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule);
            }
            #endregion String
            //mapping enums
            #region Enums
            else if (destinationType.IsEnum && sourceType.IsEnum)
            {
                //if enum types are same apply the simple rule
                //otherwise cast to destination
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                {
                    CastRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                }
            }
            #endregion Enums
            //assignable value types
            #region assignable value types
            else if (destinationType.GetNullableType().IsValueType&& sourceType.GetNullableType().IsValueType&&
                     destinationType.GetNullableType().IsAssignableFrom(sourceType.GetNullableType()))
            {
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                {
                    FromNullableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                }
            }
            #endregion same value tyeps
            //for source and destination being objects or structures
            #region object/structure
            else if ((destinationType.IsClass || destinationType.IsValueType) && (sourceType.IsClass || sourceType.IsValueType))
            {
                if (!SimpleRule.TryApplyRule(sourceType, destinationType, out mappingRule))
                {
                    if (destinationType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) &&
                        sourceType.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)))
                    {
                        //GetElementType()

                        if (destinationType.GetInterfaces().Contains(typeof(System.Collections.IDictionary)))
                        {
                            throw new NotImplementedException("Dictionary is not supported yet.");
                        }

                        EnumerableRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                    }
                    else
                    {
                        //will setup the Map<> for this types, but unfortunately we can't check them at this point
                        MapClassRule.TryApplyRule(sourceType, destinationType, out mappingRule);
                    }
                }
            }
            #endregion object/structure

            if (mappingRule != null)
            {
                return(true);
            }
            return(false);
        }
示例#14
0
 protected EnumerableRule(Type sourceType, Type destinationType, IMemberMappingRule innerRule)
 {
     SourceType      = sourceType;
     DestinationType = destinationType;
     this.innerRule  = innerRule;
 }