Beispiel #1
0
 private void EnsureReflectionObject(Type t)
 {
     if (_reflectionObject == null)
         _reflectionObject = ReflectionObject.Create(t, t.GetConstructor(new[] { typeof(byte[]) }), BinaryToArrayName);
 }
 private static void EnsureReflectionObject(Type objectType)
 {
     if (_reflectionObject == null)
         _reflectionObject = ReflectionObject.Create(objectType, KeyPropertyName, ValuePropertyName);
 }
Beispiel #3
0
 // Token: 0x06000E17 RID: 3607 RVA: 0x000515D8 File Offset: 0x0004F7D8
 public static ReflectionObject Create(Type t, params string[] memberNames)
 {
     return(ReflectionObject.Create(t, null, memberNames));
 }
Beispiel #4
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);
        }
        public static ReflectionObject Create(Type t, MethodBase creator, params string[] memberNames)
        {
            ReflectionObject d = new ReflectionObject();

            ReflectionDelegateFactory delegateFactory = JsonTypeReflector.ReflectionDelegateFactory;

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

                    d.Creator = args => ctor();
                }
            }

            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;
        }
Beispiel #6
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;
            }
        }
 // Token: 0x06000CC3 RID: 3267
 // RVA: 0x00049058 File Offset: 0x00047258
 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.CreateParametrizedConstructor(creator);
     }
     else if (ReflectionUtils.HasDefaultConstructor(t, false))
     {
         Func<object> ctor = reflectionDelegateFactory.CreateDefaultConstructor<object>(t);
         reflectionObject.Creator = ((object[] args) => ctor());
     }
     int i = 0;
     while (i < memberNames.Length)
     {
         string text = memberNames[i];
         MemberInfo[] member = t.GetMember(text, BindingFlags.Instance | BindingFlags.Public);
         if (member.Length != 1)
         {
             throw new ArgumentException(StringUtils.FormatWith("Expected a single member with the name '{0}'.", CultureInfo.InvariantCulture, text));
         }
         MemberInfo memberInfo = Enumerable.Single<MemberInfo>(member);
         ReflectionMember reflectionMember = new ReflectionMember();
         MemberTypes memberTypes = TypeExtensions.MemberType(memberInfo);
         if (memberTypes == MemberTypes.Field)
         {
             goto IL_151;
         }
         if (memberTypes != MemberTypes.Method)
         {
             if (memberTypes == MemberTypes.Property)
             {
                 goto IL_151;
             }
             throw new ArgumentException(StringUtils.FormatWith("Unexpected member type '{0}' for member '{1}'.", CultureInfo.InvariantCulture, TypeExtensions.MemberType(memberInfo), memberInfo.Name));
         }
         else
         {
             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>(methodInfo);
                     reflectionMember.Getter = ((object target) => call(target, new object[0]));
                 }
                 else if (parameters.Length == 1 && methodInfo.ReturnType == typeof(void))
                 {
                     MethodCall<object, object> call = reflectionDelegateFactory.CreateMethodCall<object>(methodInfo);
                     reflectionMember.Setter = delegate(object target, object arg)
                     {
                         call(target, new object[]
                         {
                             arg
                         });
                     };
                 }
             }
         }
         IL_184:
         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[text] = reflectionMember;
         i++;
         continue;
         IL_151:
         if (ReflectionUtils.CanReadMemberValue(memberInfo, false))
         {
             reflectionMember.Getter = reflectionDelegateFactory.CreateGet<object>(memberInfo);
         }
         if (ReflectionUtils.CanSetMemberValue(memberInfo, false, false))
         {
             reflectionMember.Setter = reflectionDelegateFactory.CreateSet<object>(memberInfo);
             goto IL_184;
         }
         goto IL_184;
     }
     return reflectionObject;
 }
Beispiel #8
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);
        }
        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);
        }