Esempio 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);
            }
        }
        Action <object> IAutoConfigurationResolver.ResolvePostProcessors(Type type)
        {
            Action <object> global = null;

            if (configuration != null)
            {
                global = configuration.ResolvePostProcessors(type);
            }
            string key = type.CreateKey();
            List <MulticastDelegate> actions;

            if (postProcessors.TryGetValue(key, out actions))
            {
                return(o =>
                {
                    if (global != null)
                    {
                        global(o);
                    }
                    actions.ForEach(i => i.Method.Invoke(i.Target, new[] { o }));
                });
            }
            else if (global != null)
            {
                return(o => global(o));
            }
            return(null);
        }