Exemplo n.º 1
0
        private void FillInstance(object o)
        {
            Type type = o.GetType();

            parser.ParseMembers(type, p =>
            {
                if (p.CanPropertyOrFieldWrite())
                {
                    var propType = p.PropertyOrFieldType();
                    object value;
                    var getter = config.ResolveMemberByName(p, type, false)
                                 ?? config.ResolveMemberByType(p, type, false)
                                 ?? config.ResolveMemberByName(p, type)
                                 ?? config.ResolveMemberByType(p, type);
                    if (getter != null)
                    {
                        value = getter(p);
                    }
                    else
                    {
                        value = builder.CreateObject(propType);
                    }
                    if (value != null)
                    {
                        if (propType.IsAssignableFrom(value.GetType()))
                        {
                            object[] args = null;
                            if (p is PropertyInfo)
                            {
                                var property = p as PropertyInfo;
                                args         =
                                    property.GetIndexParameters().Select(
                                        pp => builder.CreateObject(pp.ParameterType)).
                                    ToArray();
                            }
                            try
                            {
                                p.SetPropertyOrFieldValue(o, value, args);
                            }
                            catch (TargetInvocationException)
                            {
                            }
                        }
                        else if (!propType.IsAssignableFrom(value.GetType()))
                        {
                            throw new InvalidCastException(
                                string.Format("Expected {0}, but was {1}: {2}",
                                              propType.ToString(),
                                              value.GetType().ToString(), p.Name));
                        }
                    }
                }
            });
            var actions = config.ResolvePostProcessors(type);

            if (actions != null)
            {
                actions(o);
            }
        }
        Func <MemberInfo, object> IAutoConfigurationResolver.ResolveMemberByType(MemberInfo prop, Type type, bool cascade)
        {
            var key = prop.PropertyOrFieldType().CreateKey();
            Func <MemberInfo, object> value;

            if (resolverDictionary.TryGetValue(key, out value))
            {
                return(value);
            }
            Func <Type, object> factory;

            if (factoryDictionary.TryGetValue(key, out factory))
            {
                return(m => factory(prop.PropertyOrFieldType()));
            }
            if (cascade && configuration != null)
            {
                return(configuration.ResolveMemberByType(prop, type));
            }
            return(null);
        }