Exemple #1
0
        public static List <MemberInfo> GetFieldsAndProperties(Type type, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            List <MemberInfo> targetMembers = new List <MemberInfo>();

            targetMembers.AddRange(GetFields(type, bindingAttr));
            targetMembers.AddRange(GetProperties(type, bindingAttr));

            // for some reason .NET returns multiple members when overriding a generic member on a base class
            // http://forums.msdn.microsoft.com/en-US/netfxbcl/thread/b5abbfee-e292-4a64-8907-4e3f0fb90cd9/
            // filter members to only return the override on the topmost class
            // update: I think this is fixed in .NET 3.5 SP1 - leave this in for now...
            List <MemberInfo> distinctMembers = new List <MemberInfo>(targetMembers.Count);

            foreach (var groupedMember in targetMembers.GroupBy(m => m.Name))
            {
                int count = groupedMember.Count();
                IList <MemberInfo> members = groupedMember.ToList();

                if (count == 1)
                {
                    distinctMembers.Add(members.First());
                }
                else
                {
                    var resolvedMembers = members.Where(m => !IsOverridenGenericMember(m, bindingAttr) || m.Name == "Item");

                    distinctMembers.AddRange(resolvedMembers);
                }
            }

            return(distinctMembers);
        }
Exemple #2
0
        private static bool IsOverridenGenericMember(MemberInfo memberInfo, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            MemberTypes memberType = memberInfo.MemberType();

            if (memberType != MemberTypes.Field && memberType != MemberTypes.Property)
            {
                throw new ArgumentException("Member must be a field or property.");
            }

            Type declaringType = memberInfo.DeclaringType;

            if (!declaringType.IsGenericType())
            {
                return(false);
            }
            Type genericTypeDefinition = declaringType.GetGenericTypeDefinition();

            if (genericTypeDefinition == null)
            {
                return(false);
            }
            MemberInfo[] members = genericTypeDefinition.GetMember(memberInfo.Name, bindingAttr);
            if (members.Length == 0)
            {
                return(false);
            }
            Type memberUnderlyingType = GetMemberUnderlyingType(members[0]);

            if (!memberUnderlyingType.IsGenericParameter)
            {
                return(false);
            }

            return(true);
        }
        public static IEnumerable <PropertyInfo> GetProperties(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            IList <PropertyInfo> properties = (bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.DeclaredOnly))
                          ? type.GetTypeInfo().DeclaredProperties.ToList()
                          : type.GetTypeInfo().GetPropertiesRecursive();

            return(properties.Where(p => TestAccessibility(p, bindingFlags)));
        }
Exemple #4
0
        public static IEnumerable <FieldInfo> GetFields(Type targetType, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            ValidationUtils.ArgumentNotNull(targetType, "targetType");

            List <MemberInfo> fieldInfos = new List <MemberInfo>(targetType.GetFields(bindingAttr));

            return(fieldInfos.Cast <FieldInfo>());
        }
        public static IEnumerable <FieldInfo> GetFields(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            IList <FieldInfo> fields = (bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.DeclaredOnly))
                          ? type.GetTypeInfo().DeclaredFields.ToList()
                          : type.GetTypeInfo().GetFieldsRecursive();

            return(fields.Where(f => TestAccessibility(f, bindingFlags)).ToList());
        }
        private static bool TestAccessibility(MethodBase member, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            bool visibility = (member.IsPublic && bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.Public)) ||
                              (!member.IsPublic && bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.NonPublic));

            bool instance = (member.IsStatic && bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.Static)) ||
                            (!member.IsStatic && bindingFlags.HasFlag(PlayFab.Json.Utilities.BindingFlags.Instance));

            return(visibility && instance);
        }
Exemple #7
0
        public static ConstructorInfo GetDefaultConstructor(Type t, bool nonPublic)
        {
            PlayFab.Json.Utilities.BindingFlags bindingFlags = PlayFab.Json.Utilities.BindingFlags.Instance | PlayFab.Json.Utilities.BindingFlags.Public;
            if (nonPublic)
            {
                bindingFlags = bindingFlags | PlayFab.Json.Utilities.BindingFlags.NonPublic;
            }

            return(t.GetConstructors(bindingFlags).SingleOrDefault(c => !c.GetParameters().Any()));
        }
        private static bool TestAccessibility(PropertyInfo member, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            if (member.GetMethod != null && TestAccessibility(member.GetMethod, bindingFlags))
            {
                return(true);
            }

            if (member.SetMethod != null && TestAccessibility(member.SetMethod, bindingFlags))
            {
                return(true);
            }

            return(false);
        }
        public static MethodInfo GetMethod(this Type type, string name, PlayFab.Json.Utilities.BindingFlags bindingFlags, object placeHolder1, IList <Type> parameterTypes, object placeHolder2)
        {
            return(type.GetTypeInfo().DeclaredMethods.Where(m =>
            {
                if (name != null && m.Name != name)
                {
                    return false;
                }

                if (!TestAccessibility(m, bindingFlags))
                {
                    return false;
                }

                return m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes);
            }).SingleOrDefault());
        }
        private static bool TestAccessibility(MemberInfo member, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            if (member is FieldInfo)
            {
                return(TestAccessibility((FieldInfo)member, bindingFlags));
            }
            else if (member is MethodBase)
            {
                return(TestAccessibility((MethodBase)member, bindingFlags));
            }
            else if (member is PropertyInfo)
            {
                return(TestAccessibility((PropertyInfo)member, bindingFlags));
            }

            throw new Exception("Unexpected member type.");
        }
        private static IEnumerable <ConstructorInfo> GetConstructors(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags, IList <Type> parameterTypes)
        {
            return(type.GetTypeInfo().DeclaredConstructors.Where(c =>
            {
                if (!TestAccessibility(c, bindingFlags))
                {
                    return false;
                }

                if (parameterTypes != null && !c.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes))
                {
                    return false;
                }

                return true;
            }));
        }
Exemple #12
0
        public static MemberInfo GetMemberInfoFromType(Type targetType, MemberInfo memberInfo)
        {
            const PlayFab.Json.Utilities.BindingFlags bindingAttr = PlayFab.Json.Utilities.BindingFlags.Instance | PlayFab.Json.Utilities.BindingFlags.Static | PlayFab.Json.Utilities.BindingFlags.Public | PlayFab.Json.Utilities.BindingFlags.NonPublic;

            switch (memberInfo.MemberType())
            {
            case MemberTypes.Property:
                PropertyInfo propertyInfo = (PropertyInfo)memberInfo;

                Type[] types = propertyInfo.GetIndexParameters().Select(p => p.ParameterType).ToArray();

                return(targetType.GetProperty(propertyInfo.Name, bindingAttr, null, propertyInfo.PropertyType, types, null));

            default:
                return(targetType.GetMember(memberInfo.Name, memberInfo.MemberType(), bindingAttr).SingleOrDefault());
            }
        }
Exemple #13
0
        private static void GetChildPrivateFields(IList <MemberInfo> initialFields, Type targetType, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            // fix weirdness with private FieldInfos only being returned for the current Type
            // find base type fields and add them to result
            if ((bindingAttr & PlayFab.Json.Utilities.BindingFlags.NonPublic) != 0)
            {
                // modify flags to not search for public fields
                PlayFab.Json.Utilities.BindingFlags nonPublicBindingAttr = bindingAttr.RemoveFlag(PlayFab.Json.Utilities.BindingFlags.Public);

                while ((targetType = targetType.BaseType()) != null)
                {
                    // filter out protected fields
                    IEnumerable <MemberInfo> childPrivateFields =
                        targetType.GetFields(nonPublicBindingAttr).Where(f => f.IsPrivate).Cast <MemberInfo>();

                    initialFields.AddRange(childPrivateFields);
                }
            }
        }
Exemple #14
0
        public static IEnumerable <PropertyInfo> GetProperties(Type targetType, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            ValidationUtils.ArgumentNotNull(targetType, "targetType");

            List <PropertyInfo> propertyInfos = new List <PropertyInfo>(targetType.GetProperties(bindingAttr));

            GetChildPrivateProperties(propertyInfos, targetType, bindingAttr);

            // a base class private getter/setter will be inaccessable unless the property was gotten from the base class
            for (int i = 0; i < propertyInfos.Count; i++)
            {
                PropertyInfo member = propertyInfos[i];
                if (member.DeclaringType != targetType)
                {
                    PropertyInfo declaredMember = (PropertyInfo)GetMemberInfoFromType(member.DeclaringType, member);
                    propertyInfos[i] = declaredMember;
                }
            }

            return(propertyInfos);
        }
        public static PropertyInfo GetProperty(this Type type, string name, PlayFab.Json.Utilities.BindingFlags bindingFlags, object placeholder1, Type propertyType, IList <Type> indexParameters, object placeholder2)
        {
            return(type.GetTypeInfo().DeclaredProperties.Where(p =>
            {
                if (name != null && name != p.Name)
                {
                    return false;
                }
                if (propertyType != null && propertyType != p.PropertyType)
                {
                    return false;
                }
                if (indexParameters != null)
                {
                    if (!p.GetIndexParameters().Select(ip => ip.ParameterType).SequenceEqual(indexParameters))
                    {
                        return false;
                    }
                }

                return true;
            }).SingleOrDefault());
        }
 public static PropertyInfo GetProperty(this Type type, string name, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetTypeInfo().GetDeclaredProperty(name));
 }
 public static IEnumerable <ConstructorInfo> GetConstructors(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetConstructors(bindingFlags, null));
 }
        public static IEnumerable <MemberInfo> GetMember(this Type type, string name, MemberTypes memberType, PlayFab.Json.Utilities.BindingFlags bindingFlags)
        {
            return(type.GetTypeInfo().GetMembersRecursive().Where(m =>
            {
                if (name != null && name != m.Name)
                {
                    return false;
                }
                if (m.MemberType() != memberType)
                {
                    return false;
                }
                if (!TestAccessibility(m, bindingFlags))
                {
                    return false;
                }

                return true;
            }));
        }
 public static ConstructorInfo GetConstructor(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags, object placeholder1, IList <Type> parameterTypes, object placeholder2)
 {
     return(type.GetConstructors(bindingFlags, parameterTypes).SingleOrDefault());
 }
 public static MemberInfo[] GetMember(this Type type, string member, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetTypeInfo().GetMembersRecursive().Where(m => m.Name == member && TestAccessibility(m, bindingFlags)).ToArray());
 }
 public static MemberInfo GetField(this Type type, string member, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetTypeInfo().GetDeclaredField(member));
 }
 public static IEnumerable <MethodInfo> GetMethods(this Type type, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetTypeInfo().DeclaredMethods);
 }
Exemple #23
0
 public static PlayFab.Json.Utilities.BindingFlags RemoveFlag(this PlayFab.Json.Utilities.BindingFlags bindingAttr, PlayFab.Json.Utilities.BindingFlags flag)
 {
     return(((bindingAttr & flag) == flag)
       ? bindingAttr ^ flag
       : bindingAttr);
 }
Exemple #24
0
        private static void GetChildPrivateProperties(IList <PropertyInfo> initialProperties, Type targetType, PlayFab.Json.Utilities.BindingFlags bindingAttr)
        {
            // fix weirdness with private PropertyInfos only being returned for the current Type
            // find base type properties and add them to result

            // also find base properties that have been hidden by subtype properties with the same name

            while ((targetType = targetType.BaseType()) != null)
            {
                foreach (PropertyInfo propertyInfo in targetType.GetProperties(bindingAttr))
                {
                    PropertyInfo subTypeProperty = propertyInfo;

                    if (!IsPublic(subTypeProperty))
                    {
                        // have to test on name rather than reference because instances are different
                        // depending on the type that GetProperties was called on
                        int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name);
                        if (index == -1)
                        {
                            initialProperties.Add(subTypeProperty);
                        }
                        else
                        {
                            // replace nonpublic properties for a child, but gotten from
                            // the parent with the one from the child
                            // the property gotten from the child will have access to private getter/setter
                            initialProperties[index] = subTypeProperty;
                        }
                    }
                    else
                    {
                        if (!subTypeProperty.IsVirtual())
                        {
                            int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name &&
                                                                  p.DeclaringType == subTypeProperty.DeclaringType);

                            if (index == -1)
                            {
                                initialProperties.Add(subTypeProperty);
                            }
                        }
                        else
                        {
                            int index = initialProperties.IndexOf(p => p.Name == subTypeProperty.Name &&
                                                                  p.IsVirtual() &&
                                                                  p.GetBaseDefinition() != null &&
                                                                  p.GetBaseDefinition().DeclaringType.IsAssignableFrom(subTypeProperty.DeclaringType));

                            if (index == -1)
                            {
                                initialProperties.Add(subTypeProperty);
                            }
                        }
                    }
                }
            }
        }
 public static MethodInfo GetMethod(this Type type, string name, PlayFab.Json.Utilities.BindingFlags bindingFlags)
 {
     return(type.GetTypeInfo().GetDeclaredMethod(name));
 }