Ejemplo n.º 1
0
        private static Func <object[], object> GetCreator(Type type)
        {
            Func <object> defaultConstructor = (ReflectionUtils.HasDefaultConstructor(type, false))
                ? ReflectionDelegateFactory.CreateDefaultConstructor <object>(type)
                : null;

            return((parameters) =>
            {
                try
                {
                    if (parameters != null)
                    {
                        Type[] paramTypes = parameters.Select(param => param.GetType()).ToArray();
                        ConstructorInfo parameterizedConstructorInfo = type.GetConstructor(paramTypes);

                        if (null != parameterizedConstructorInfo)
                        {
                            ObjectConstructor <object> parameterizedConstructor = ReflectionDelegateFactory.CreateParameterizedConstructor(parameterizedConstructorInfo);
                            return parameterizedConstructor(parameters);
                        }
                        else
                        {
                            throw new JsonException("No matching parameterized constructor found for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
                        }
                    }

                    if (defaultConstructor == null)
                    {
                        throw new JsonException("No parameterless constructor defined for '{0}'.".FormatWith(CultureInfo.InvariantCulture, type));
                    }

                    return defaultConstructor();
                }
                catch (Exception ex)
                {
                    throw new JsonException("Error creating '{0}'.".FormatWith(CultureInfo.InvariantCulture, type), ex);
                }
            });
        }
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            ReflectionDelegateFactory delegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            ObjectConstructor <object> creatorConstructor = null;

            if (creator != null)
            {
                creatorConstructor = delegateFactory.CreateParameterizedConstructor(creator);
            }
            else
            {
                if (ReflectionUtils.HasDefaultConstructor(t, false))
                {
                    Func <object> ctor = delegateFactory.CreateDefaultConstructor <object>(t);

                    creatorConstructor = args => ctor();
                }
            }

            ReflectionObject d = new ReflectionObject(creatorConstructor);

            foreach (string memberName in memberNames)
            {
                MemberInfo[] members = t.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public);
                if (members.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, memberName));
                }

                MemberInfo member = members.Single();

                ReflectionMember reflectionMember = new ReflectionMember();

                switch (member.MemberType())
                {
                case MemberTypes.Field:
                case MemberTypes.Property:
                    if (ReflectionUtils.CanReadMemberValue(member, false))
                    {
                        reflectionMember.Getter = delegateFactory.CreateGet <object>(member);
                    }

                    if (ReflectionUtils.CanSetMemberValue(member, false, false))
                    {
                        reflectionMember.Setter = delegateFactory.CreateSet <object>(member);
                    }
                    break;

                case MemberTypes.Method:
                    MethodInfo method = (MethodInfo)member;
                    if (method.IsPublic)
                    {
                        ParameterInfo[] parameters = method.GetParameters();
                        if (parameters.Length == 0 && method.ReturnType != typeof(void))
                        {
                            MethodCall <object, object> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Getter = target => call(target);
                        }
                        else if (parameters.Length == 1 && method.ReturnType == typeof(void))
                        {
                            MethodCall <object, object> call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Setter = (target, arg) => call(target, arg);
                        }
                    }
                    break;

                default:
                    throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith(CultureInfo.InvariantCulture, member.MemberType(), member.Name));
                }

                if (ReflectionUtils.CanReadMemberValue(member, false))
                {
                    reflectionMember.Getter = delegateFactory.CreateGet <object>(member);
                }

                if (ReflectionUtils.CanSetMemberValue(member, false, false))
                {
                    reflectionMember.Setter = delegateFactory.CreateSet <object>(member);
                }

                reflectionMember.MemberType = ReflectionUtils.GetMemberUnderlyingType(member);

                d.Members[memberName] = reflectionMember;
            }

            return(d);
        }