Пример #1
0
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            var d = new ReflectionObject();
            var delegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            if (creator != null)
            {
                d.Creator = delegateFactory.CreateParametrizedConstructor(creator);
            }
            else
            {
                if (ReflectionUtils.HasDefaultConstructor(t, false))
                {
                    var ctor = delegateFactory.CreateDefaultConstructor <object>(t);
                    d.Creator = args => ctor();
                }
            }

            foreach (var memberName in memberNames)
            {
                var 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));
                }

                var member           = members.Single();
                var 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:
                    var method = (MethodInfo)member;
                    if (method.IsPublic)
                    {
                        var parameters = method.GetParameters();
                        if (parameters.Length == 0 && method.ReturnType != typeof(void))
                        {
                            var call = delegateFactory.CreateMethodCall <object>(method);
                            reflectionMember.Getter = target => call(target);
                        }
                        else if (parameters.Length == 1 && method.ReturnType == typeof(void))
                        {
                            var 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);
        }
Пример #2
0
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            var d = new ReflectionObject();
            var delegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            if (creator != null)
                d.Creator = delegateFactory.CreateParametrizedConstructor(creator);
            else
            {
                if (ReflectionUtils.HasDefaultConstructor(t, false))
                {
                    var ctor = delegateFactory.CreateDefaultConstructor<object>(t);
                    d.Creator = args => ctor();
                }
            }

            foreach (var memberName in memberNames)
            {
                var 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));

                var member = members.Single();
                var 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:
                        var method = (MethodInfo)member;
                        if (method.IsPublic)
                        {
                            var parameters = method.GetParameters();
                            if (parameters.Length == 0 && method.ReturnType != typeof(void))
                            {
                                var call = delegateFactory.CreateMethodCall<object>(method);
                                reflectionMember.Getter = target => call(target);
                            }
                            else if (parameters.Length == 1 && method.ReturnType == typeof(void))
                            {
                                var 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;
        }