Example #1
0
        protected override bool InvokeInitMethod(InitMethodInfo initInfo)
        {
            var ctor = initInfo.InitalizerMethod as ConstructorInfo;

            if (ctor == null)
            {
                return(false);
            }
            var createdInstance = ctor.Invoke(initInfo.MappingArgs);

            initInfo.PropInfo.SetValue(createdInstance);
            return(true);
        }
Example #2
0
 protected virtual bool InvokeInitMethod(InitMethodInfo initInfo)
 {
     initInfo.InitalizerMethod.Invoke(initInfo.Instance, initInfo.MappingArgs);
     return(true);
 }
Example #3
0
        public bool MapPropery(ITypeMapper mapper, IPropertyMappingInfo propInfo, object sourceValue, IList <Attribute> metadata = null)
        {
            if (!mapper.CanMap(sourceValue, propInfo.Type))
            {
                return(false);
            }
            Exception  exception;
            MethodBase initalizerMethod = null;

            try
            {
                initalizerMethod = GetInitMethod(propInfo);
            }
            catch (AmbiguousMatchException ex) { exception = ex; }
            catch (ArgumentNullException ex) { exception = ex; }

            if (initalizerMethod == null)
            {
                return(false);
            }
            try
            {
                var parameters = initalizerMethod.GetParameters();
                if (parameters.Length != 1)
                {
                    return(false);
                }
                //throw new ArgumentException("Only initalizers with single argument are supported.");

                var paramType = parameters[0].ParameterType;
                if (!mapper.CanMap(sourceValue, paramType))
                {
                    return(false);
                }

                IOperationResult mappingResult;
                if (mapper is ITypeInfoMapper)
                {
                    mappingResult = ((ITypeInfoMapper)mapper).Map(new SourceInfo(sourceValue)
                    {
                        Attributes = metadata
                    }, paramType);
                }
                else
                {
                    mappingResult = mapper.Map(sourceValue, paramType);
                }
                if (mappingResult.Success)
                {
                    var invokationInfo = new InitMethodInfo {
                        InitalizerMethod = initalizerMethod,
                        PropInfo         = propInfo,
                        Instance         = propInfo.SourceInstance,
                        MappingArgs      = new[] { mappingResult.Value }
                    };

                    return(InvokeInitMethod(invokationInfo));
                }
                return(false);
            }
            catch (TargetException ex) { exception = ex; }
            catch (ArgumentException ex) { exception = ex; }
            catch (TargetInvocationException ex) { exception = ex; }
            catch (TargetParameterCountException ex) { exception = ex; }
            catch (MethodAccessException ex) { exception = ex; }
            catch (InvalidOperationException ex) { exception = ex; }
            catch (NotSupportedException ex) { exception = ex; }
            throw exception;
        }