コード例 #1
0
        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));
                }

                reflectionMember.MemberType = ReflectionUtils.GetMemberUnderlyingType(member);

                d.Members[memberName] = reflectionMember;
            }

            return(d);
        }
コード例 #2
0
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            ReflectionObject          obj2 = new ReflectionObject();
            ReflectionDelegateFactory reflectionDelegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            if (creator != null)
            {
                obj2.Creator = reflectionDelegateFactory.CreateParameterizedConstructor(creator);
            }
            else if (ReflectionUtils.HasDefaultConstructor(t, false))
            {
                Func <object> ctor = reflectionDelegateFactory.CreateDefaultConstructor <object>(t);
                obj2.Creator = args => ctor();
            }
            foreach (string str in memberNames)
            {
                MethodInfo   info2;
                MemberInfo[] source = t.GetMember(str, BindingFlags.Public | BindingFlags.Instance);
                if (source.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, str));
                }
                MemberInfo       memberInfo = source.Single <MemberInfo>();
                ReflectionMember member     = new ReflectionMember();
                MemberTypes      types      = memberInfo.MemberType();
                if (types != MemberTypes.Field)
                {
                    if (types == MemberTypes.Method)
                    {
                        goto Label_0107;
                    }
                    if (types != MemberTypes.Property)
                    {
                        throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith(CultureInfo.InvariantCulture, memberInfo.MemberType(), memberInfo.Name));
                    }
                }
                if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
                {
                    member.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
                }
                if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
                {
                    member.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                }
                goto Label_01F3;
Label_0107:
                info2 = (MethodInfo)memberInfo;
                if (info2.IsPublic)
                {
                    ParameterInfo[] parameters = info2.GetParameters();
                    if ((parameters.Length == 0) && (info2.ReturnType != typeof(void)))
                    {
                        MethodCall <object, object> call = reflectionDelegateFactory.CreateMethodCall <object>(info2);
                        member.Getter = target => call(target, new object[0]);
                    }
                    else if ((parameters.Length == 1) && (info2.ReturnType == typeof(void)))
                    {
                        MethodCall <object, object> call1 = reflectionDelegateFactory.CreateMethodCall <object>(info2);
                        member.Setter = delegate(object target, object arg) {
                            object[] args = new object[] { arg };
                            call1(target, args);
                        };
                    }
                }
Label_01F3:
                if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
                {
                    member.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
                }
                if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
                {
                    member.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                }
                member.MemberType = ReflectionUtils.GetMemberUnderlyingType(memberInfo);
                obj2.Members[str] = member;
            }
            return(obj2);
        }
コード例 #3
0
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            MemberInfo                memberInfo;
            ReflectionMember          reflectionMember;
            MemberTypes               memberType;
            ReflectionObject          reflectionObject          = new ReflectionObject();
            ReflectionDelegateFactory reflectionDelegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            if (creator != null)
            {
                reflectionObject.Creator = reflectionDelegateFactory.CreateParameterizedConstructor(creator);
            }
            else if (ReflectionUtils.HasDefaultConstructor(t, false))
            {
                Func <object> func = reflectionDelegateFactory.CreateDefaultConstructor <object>(t);
                reflectionObject.Creator = (object[] args) => func();
            }
            string[] strArrays = memberNames;
            for (int i = 0; i < (int)strArrays.Length; i++)
            {
                string       str    = strArrays[i];
                MemberInfo[] member = t.GetMember(str, BindingFlags.Instance | BindingFlags.Public);
                if ((int)member.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith(CultureInfo.InvariantCulture, str));
                }
                memberInfo       = ((IEnumerable <MemberInfo>)member).Single <MemberInfo>();
                reflectionMember = new ReflectionMember();
                memberType       = memberInfo.MemberType();
                if (memberType != MemberTypes.Field)
                {
                    if (memberType != MemberTypes.Method)
                    {
                        goto Label1;
                    }
                    MethodInfo methodInfo = (MethodInfo)memberInfo;
                    if (methodInfo.IsPublic)
                    {
                        ParameterInfo[] parameters = methodInfo.GetParameters();
                        if (parameters.Length == 0 && methodInfo.ReturnType != typeof(void))
                        {
                            MethodCall <object, object> methodCall = reflectionDelegateFactory.CreateMethodCall <object>(methodInfo);
                            reflectionMember.Getter = (object target) => methodCall(target, new object[0]);
                        }
                        else if ((int)parameters.Length == 1 && methodInfo.ReturnType == typeof(void))
                        {
                            MethodCall <object, object> methodCall1 = reflectionDelegateFactory.CreateMethodCall <object>(methodInfo);
                            reflectionMember.Setter = (object target, object arg) => methodCall1(target, new object[] { arg });
                        }
                    }
                }
                else
                {
                    goto Label0;
                }
Label2:
                if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
                {
                    reflectionMember.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
                }
                if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
                {
                    reflectionMember.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                }
                reflectionMember.MemberType   = ReflectionUtils.GetMemberUnderlyingType(memberInfo);
                reflectionObject.Members[str] = reflectionMember;
            }
            return(reflectionObject);

Label0:
            if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
            {
                reflectionMember.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
            }
            if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
            {
                reflectionMember.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                goto Label2;
            }
            else
            {
                goto Label2;
            }
Label1:
            if (memberType != MemberTypes.Property)
            {
                throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith(CultureInfo.InvariantCulture, memberInfo.MemberType(), memberInfo.Name));
            }
            else
            {
                goto Label0;
            }
        }
コード例 #4
0
        public static ReflectionObject Create(
            Type t,
            MethodBase creator,
            params string[] memberNames)
        {
            ReflectionObject          reflectionObject          = new ReflectionObject();
            ReflectionDelegateFactory reflectionDelegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

            if (creator != null)
            {
                reflectionObject.Creator = reflectionDelegateFactory.CreateParameterizedConstructor(creator);
            }
            else if (ReflectionUtils.HasDefaultConstructor(t, false))
            {
                Func <object> ctor = reflectionDelegateFactory.CreateDefaultConstructor <object>(t);
                reflectionObject.Creator = (ObjectConstructor <object>)(args => ctor());
            }
            foreach (string memberName in memberNames)
            {
                MemberInfo[] member = t.GetMember(memberName, BindingFlags.Instance | BindingFlags.Public);
                if (member.Length != 1)
                {
                    throw new ArgumentException("Expected a single member with the name '{0}'.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)memberName));
                }
                MemberInfo       memberInfo       = ((IEnumerable <MemberInfo>)member).Single <MemberInfo>();
                ReflectionMember reflectionMember = new ReflectionMember();
                switch (memberInfo.MemberType())
                {
                case MemberTypes.Field:
                case MemberTypes.Property:
                    if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
                    {
                        reflectionMember.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
                    }
                    if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
                    {
                        reflectionMember.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                        break;
                    }
                    break;

                case MemberTypes.Method:
                    MethodInfo methodInfo = (MethodInfo)memberInfo;
                    if (methodInfo.IsPublic)
                    {
                        ParameterInfo[] parameters = methodInfo.GetParameters();
                        if (parameters.Length == 0 && methodInfo.ReturnType != typeof(void))
                        {
                            MethodCall <object, object> call = reflectionDelegateFactory.CreateMethodCall <object>((MethodBase)methodInfo);
                            reflectionMember.Getter = (Func <object, object>)(target => call(target, new object[0]));
                            break;
                        }
                        if (parameters.Length == 1 && methodInfo.ReturnType == typeof(void))
                        {
                            MethodCall <object, object> call = reflectionDelegateFactory.CreateMethodCall <object>((MethodBase)methodInfo);
                            object obj;
                            reflectionMember.Setter = (Action <object, object>)((target, arg) => obj = call(target, new object[1]
                            {
                                arg
                            }));
                            break;
                        }
                        break;
                    }
                    break;

                default:
                    throw new ArgumentException("Unexpected member type '{0}' for member '{1}'.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)memberInfo.MemberType(), (object)memberInfo.Name));
                }
                if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
                {
                    reflectionMember.Getter = reflectionDelegateFactory.CreateGet <object>(memberInfo);
                }
                if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
                {
                    reflectionMember.Setter = reflectionDelegateFactory.CreateSet <object>(memberInfo);
                }
                reflectionMember.MemberType          = ReflectionUtils.GetMemberUnderlyingType(memberInfo);
                reflectionObject.Members[memberName] = reflectionMember;
            }
            return(reflectionObject);
        }