コード例 #1
0
        public void RegisterMappingIgnore <Source, Destination>(params string[] propertiesToIgnore)
        {
            string key = NameCreator.CacheKey(typeof(Source), typeof(Destination));

            MapperCore.IgnoreList = new System.Collections.Concurrent.ConcurrentDictionary <string, string[]>();
            MapperCore.IgnoreList.TryAdd(key, propertiesToIgnore);
        }
コード例 #2
0
        public static void MapValue(object source, object destination, string propertyName)
        {
            Type sourceObjectType      = source.GetType();
            Type destinationObjectType = destination.GetType();
            var  cacheKey = NameCreator.CacheKey(sourceObjectType, destinationObjectType, propertyName);
            ReflectionMapObject reflectionObject = null;

            MapperCore.ReflectionMapObjectList.TryGetValue(cacheKey, out reflectionObject);
            if (reflectionObject != null)
            {
                var val = reflectionObject.MethodInfoGet.Invoke(source, null);

                reflectionObject.MethodInfoSet.Invoke(destination, new object[] { val });
            }
            else
            {
                var sourceType      = sourceObjectType.GetProperty(propertyName).PropertyType;
                var destinationType = destinationObjectType.GetProperty(propertyName).PropertyType;

                if (sourceType == destinationType)
                {
                    MethodInfo methodInfoGet = sourceObjectType.GetProperty(propertyName).GetGetMethod();

                    MethodInfo methodInfoSet = destinationObjectType.GetProperty(propertyName).GetSetMethod();

                    MapperCore.ReflectionMapObjectList.TryAdd(cacheKey, new ReflectionMapObject {
                        MethodInfoGet = methodInfoGet, MethodInfoSet = methodInfoSet
                    });

                    var val = methodInfoGet.Invoke(source, null);
                    methodInfoSet.Invoke(destination, new object[] { val });
                }
            }
        }
コード例 #3
0
        public static object MapByReflection(Mapper mapper, object source, Type destinationType, string propertyName)
        {
            Type       sourceType = source.GetType();
            var        cacheKey   = NameCreator.CacheKey(sourceType, destinationType, propertyName);
            MethodInfo generic    = null;

            MapperCore.MapByReflectionList.TryGetValue(cacheKey, out generic);
            if (generic != null)
            {
                var dest = generic.Invoke(mapper, new object[] { source });

                return(dest);
            }
            else
            {
                MethodInfo method = typeof(Mapper).GetMethod("Map");

                generic = method.MakeGenericMethod(sourceType, destinationType);

                var dest = generic.Invoke(mapper, new object[] { source });

                MapperCore.MapByReflectionList.TryAdd(cacheKey, generic);

                return(dest);
            }
            //    var itemObject = item.GetValue(source);

            //    var destItemObject = Activator.CreateInstance(destItem.PropertyType);

            //    destItem.SetValue(destination, destItemObject);

            //    object[] parameters = new object[] { itemObject, destItemObject };

            //    var result = generic.Invoke(null, parameters);
        }
コード例 #4
0
        private static ProfileFunction <Source, Destination> GetFunction <Source, Destination>() where Source : class where Destination : class
        {
            var    key             = NameCreator.CacheKey(typeof(Source), typeof(Destination));
            object profileFunction = null;

            MapperCore.ProfileFunctionList.TryGetValue(key, out profileFunction);
            return((ProfileFunction <Source, Destination>)profileFunction);
        }
コード例 #5
0
        private static ProfileFunction <Source, Destination> AddFunction <Source, Destination>(Func <Source, Destination, Destination> func) where Source : class where Destination : class
        {
            var key             = NameCreator.CacheKey(typeof(Source), typeof(Destination));
            var profileFunction = new ProfileFunction <Source, Destination>();

            profileFunction.Function = func;
            MapperCore.ProfileFunctionList.TryAdd(key, profileFunction);
            return(profileFunction);
        }
コード例 #6
0
        public static bool GetIgnoreList(Type sourceType, Type destinationType, out string[] ignoreList)
        {
            ignoreList = null;
            bool isAnyItemIgnored = false;

            string ignoreKey = NameCreator.CacheKey(sourceType, destinationType);

            if (MapperCore.IgnoreList != null)
            {
                isAnyItemIgnored = MapperCore.IgnoreList.TryGetValue(ignoreKey, out ignoreList);
            }
            return(isAnyItemIgnored);
        }
コード例 #7
0
        private Destination SetValues <Source, Destination>(Source source) where Source : class where Destination : class, new()
        {
            string[] ignoreList;

            bool    isAnyItemIgnoredAtAll = IgnoreProvider.GetIgnoreList(typeof(Source), typeof(Destination), out ignoreList);
            var     key     = NameCreator.CacheKey(typeof(Source), typeof(Destination));
            MapInfo mapInfo = null;

            MapperCore.MapInfoList.TryGetValue(key, out mapInfo);
            Destination destination = new Destination();
            Type        sourceType  = null;

            Type destinationType = null;

            PropertyInfo[] propertiesSource      = null;
            PropertyInfo[] propertiesDestination = null;

            if (mapInfo == null)
            {
                sourceType            = typeof(Source);
                destinationType       = typeof(Destination);
                propertiesSource      = sourceType.GetProperties();
                propertiesDestination = destinationType.GetProperties();
                MapperCore.MapInfoList.TryAdd(key, new MapInfo {
                    SourceType              = sourceType,
                    DestinationType         = destinationType,
                    SourcePropertyInfo      = propertiesSource,
                    DestinationPropertyInfo = propertiesDestination
                });
            }
            else
            {
                sourceType            = mapInfo.SourceType;
                destinationType       = mapInfo.DestinationType;
                propertiesSource      = mapInfo.SourcePropertyInfo;
                propertiesDestination = mapInfo.DestinationPropertyInfo;
            }

            foreach (var propertyInfo in propertiesSource)
            {
                if (isAnyItemIgnoredAtAll)
                {
                    var ignored = ignoreList.Where(d => d == propertyInfo.Name);

                    if (ignored.Any())
                    {
                        continue;
                    }
                }

                if (propertyInfo.PropertyType.IsClass && !propertyInfo.PropertyType.FullName.StartsWith("System."))
                {
                    var destPropertyInfo = propertiesDestination.First(d => d.Name == propertyInfo.Name);

                    var sourceVal = propertyInfo.GetValue(source);

                    var dest = ReflectionProvider.MapByReflection(this, sourceVal, destPropertyInfo.PropertyType, propertyInfo.Name);

                    destPropertyInfo.SetValue(destination, dest);
                }
                else
                {
                    ReflectionProvider.MapValue(source, destination, propertyInfo.Name);
                }
            }

            return(destination);
        }