Exemple #1
0
        public HashSetTypeMapper()
        {
            if (DesctinationType.IsInterface)
            {
                if (DesctinationType.IsGenericType && DesctinationType.GetGenericTypeDefinition() == typeof(ISet <>))
                {
                    instanceType = typeof(HashSet <>).MakeGenericType(DesctinationType.GetGenericArguments()[0]);
                    hashSetType  = DesctinationType;
                }
            }
            else if (DesctinationType.IsAbstract)
            {
                throw new MapperException(string.Format("{0}不是一个有效的实例类型", DesctinationType.FullName));
            }

            if (instanceType == null)
            {
                instanceType = DesctinationType;
            }

            if (hashSetType == null)
            {
                hashSetType = DesctinationType.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ISet <>));
            }

            if (hashSetType == null)
            {
                throw new MapperException(string.Format("{0}未实现接口ISet<>", DesctinationType.FullName));
            }

            var param1 = Expression.Parameter(typeof(IEnumerable));
            var caller = Expression.Call(Expression.Constant(this), hashSetType.GetMethod("Convert").MakeGenericMethod(hashSetType.GetGenericArguments()), param1);

            hashSetConvert = Expression.Lambda <Func <IEnumerable, T> >(caller, param1).Compile();
        }
        public CollectionTypeMaper()
        {
            if (DesctinationType.IsInterface)
            {
                if (DesctinationType.IsGenericType)
                {
                    if (DesctinationType.GetGenericTypeDefinition() == typeof(IList <>) ||
                        DesctinationType.GetGenericTypeDefinition() == typeof(ICollection <>) ||
                        DesctinationType.GetGenericTypeDefinition() == typeof(IEnumerable <>))
                    {
                        instanceType    = typeof(List <>).MakeGenericType(DesctinationType.GetGenericArguments()[0]);
                        iCollectionType = DesctinationType;
                    }
                }
                else
                {
                    if (DesctinationType == typeof(IList) ||
                        DesctinationType == typeof(ICollection) ||
                        DesctinationType == typeof(IEnumerable))
                    {
                        instanceType    = typeof(ArrayList);
                        iCollectionType = DesctinationType;
                    }
                }
            }
            else if (!DesctinationType.IsClass)
            {
                throw new MapperException(string.Format("类型{0}不是一个有效的实例类型", DesctinationType.FullName));
            }

            if (instanceType == null)
            {
                instanceType = DesctinationType;
            }

            if (iCollectionType == null)
            {
                iCollectionType = DesctinationType.GetInterfaces().FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICollection <>));
            }

            if (iCollectionType == null)
            {
                iCollectionType = DesctinationType.GetInterfaces().FirstOrDefault(x => x == typeof(IList));
            }

            if (iCollectionType == null)
            {
                throw new MapperException(string.Format("实现类型{0}未实现接口:IList/ICollection<>", DesctinationType.FullName));
            }

            if (iCollectionType.IsGenericType)
            {
                var param1 = Expression.Parameter(typeof(IEnumerable));
                var caller = Expression.Call(Expression.Constant(this), this.GetType().GetMethod("Convert", new Type[] { typeof(IEnumerable) }).MakeGenericMethod(iCollectionType.GetGenericArguments()[0]), param1);
                genericCollectionConvert = Expression.Lambda <Func <IEnumerable, T> >(caller, param1).Compile();
            }
        }
        public DictionaryTypeMapperBase()
        {
            bool validated = false;

            if (DesctinationType.IsGenericType && DesctinationType.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                validated = true;
            }
            else if (DesctinationType == typeof(IDictionary))
            {
                validated = true;
            }
            else if (DesctinationType.GetInterfaces().Any(x => (x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IDictionary <,>)) || x == typeof(IDictionary)))
            {
                validated = true;
            }

            if (!validated)
            {
                throw new MapperException(string.Format("类型{0}未实现字典接口:IDictionary/IDictionary<TKey,TValue>。", typeof(T).FullName));
            }
        }