//[Test]
        public void TestReflection()
        {
            TransformerObserver  a  = new TransformerObserver();
            ITransformerResolver aa = a;

            ISourceMapper <Person, PersonaGiuridica> mapper0 = FactoryMapper.DynamicResolutionMapper <Person, PersonaGiuridica>();
            SourceMapper <Person, PersonaGiuridica>  mapper  = new SourceMapper <Person, PersonaGiuridica>(new List <IPropertyMapper <Person, PersonaGiuridica> >(), null, null);
            //aa.Register<ISourceMapper<Person, PersonaGiuridica>>(mapper);
            //aa.Register(mapper0);


            object obj1 = new Person();
            object obj2 = new Person();

            Type t1 = typeof(IPersonHeader);
            Type t2 = typeof(Person);

            try
            {
                object instance = 5.5;
                //int i = (int) instance;

                byte bb = Convert.ToByte(instance);

                byte   b = (byte)instance;
                double d = (double)instance;
            }
            catch (Exception)
            {
                Assert.IsFalse(true, "Cast invalid");
            }

            Compare <IPersonHeader>(obj1, obj2);
            //Compare<long>(1, 10);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Makes a dynamic action in order to associate property source with property destination using a dynamic resolver.
        /// </summary>
        /// <typeparam name="TSource">The type of the source.</typeparam>
        /// <typeparam name="TDestination">The type of the destination.</typeparam>
        /// <param name="srcProperty">The source property.</param>
        /// <param name="destProperty">The dest property.</param>
        /// <param name="resolver">The resolver.</param>
        /// <returns></returns>
        /// <exception cref="MissingAccessorException">
        /// No getter method available for retrieving value for setting property destination.
        /// or
        /// No setter method available for setting property destination.
        /// </exception>
        public static Action <TSource, TDestination> DynamicPropertyMap <TSource, TDestination>
            (PropertyInfo srcProperty, PropertyInfo destProperty, ITransformerResolver resolver)
        {
            Type srcPropType = srcProperty.PropertyType;
            Type dstPropType = destProperty.PropertyType;

            Type funcType = FunctionGetter.MakeGenericType(srcProperty.DeclaringType, srcPropType);
            Type actType  = ActionSetter.MakeGenericType(destProperty.DeclaringType, dstPropType);

            MethodInfo getterMethod = srcProperty.GetGetMethod() ?? srcProperty.GetGetMethod(true);
            MethodInfo setterMethod = destProperty.GetSetMethod() ?? destProperty.GetSetMethod(true);

            if (getterMethod == null)
            {
                throw new MissingAccessorException("No getter method available for retrieving value for setting property destination.");
            }

            if (setterMethod == null)
            {
                throw new MissingAccessorException("No setter method available for setting property destination.");
            }

            Delegate getter = Delegate.CreateDelegate(funcType, null, getterMethod);
            Delegate setter = Delegate.CreateDelegate(actType, null, setterMethod);

            Action <TSource, TDestination> action
                = (source, destination) => setter.DynamicInvoke(destination, resolver.TryToMap(getter.DynamicInvoke(source), destProperty.PropertyType));

            return(action);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyMapper{TSource, TDestination}"/> class.
        /// </summary>
        /// <param name="srcProperty">The source property.</param>
        /// <param name="destProperty">The dest property.</param>
        /// <param name="resolver">The resolver.</param>
        /// <exception cref="LambdaSetterException">The setter action for property mapper cannot be null.</exception>
        public PropertyMapper(PropertyInfo srcProperty, PropertyInfo destProperty, ITransformerResolver resolver)
            : base("[resolver]", destProperty.Name)
        {
            Action <TSource, TDestination> action = FactoryMapper.DynamicPropertyMap <TSource, TDestination>(srcProperty, destProperty, resolver);

            if (action == null)
            {
                throw new LambdaSetterException("The setter action for property mapper cannot be null.");
            }

            this.setter = action;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get all default property mappers for the given types.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TDestination"></typeparam>
        /// <returns></returns>
        public static IEnumerable <IPropertyMapper <TSource, TDestination> > GetDefaultPropertyMappers
        <TSource, TDestination>(ITransformerResolver resolver)
            where TSource : class
            where TDestination : class
        {
            IEnumerable <KeyValuePair <PropertyInfo, PropertyInfo> > matchedProperties = FactoryMapper.GetSuitedPropertiesOf(typeof(TSource), typeof(TDestination));
            HashSet <IPropertyMapper <TSource, TDestination> >       mappers           = new HashSet <IPropertyMapper <TSource, TDestination> >();

            foreach (var current in matchedProperties)
            {
                try
                {
                    Action <TSource, TDestination> action = DynamicPropertyMap <TSource, TDestination>(current.Key,
                                                                                                       current.Value);
                    mappers.Add(new PropertyMapper <TSource, TDestination>(action, current.Key.Name, current.Value.Name));
                }
                catch (InconsistentMappingException)
                {
                    // exceptions must be managed..
                    // so in this means that properties are not compatibles..
                    try
                    {
                        if (resolver != null)
                        {
                            Action <TSource, TDestination> action = DynamicPropertyMap <TSource, TDestination>(current.Key, current.Value, resolver);
                            mappers.Add(new PropertyMapper <TSource, TDestination>(action));
                        }
                    }
                    catch
                    {
                    }
                }
                catch
                {
                }
            }
            return(mappers);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the default property mappers.
        /// </summary>
        /// <param name="sourceType">Type of the source.</param>
        /// <param name="destinationType">Type of the destination.</param>
        /// <param name="resolver">The resolver.</param>
        /// <returns></returns>
        public static IEnumerable <IPropertyMapper> GetDefaultPropertyMappers(Type sourceType, Type destinationType, ITransformerResolver resolver)
        {
            IEnumerable <KeyValuePair <PropertyInfo, PropertyInfo> > matchedProperties = FactoryMapper.GetSuitedPropertiesOf(sourceType, destinationType);
            HashSet <IPropertyMapper> mappers = new HashSet <IPropertyMapper>();

            foreach (var current in matchedProperties)
            {
                try
                {
                    mappers.Add(new PropertyMapper(current.Key, current.Value));
                }
                catch (InconsistentMappingException)
                {
                    // exceptions must be managed..
                    // so in this means that properties are not compatibles..
                    try
                    {
                        if (resolver != null)
                        {
                            mappers.Add(new PropertyMapper(current.Key, current.Value, resolver));
                        }
                    }
                    catch
                    {
                    }
                }
                catch
                {
                }
            }

            return(mappers);
        }