Пример #1
0
        IEnumerable <MemberInfoWithMeta> GetIndexedTypeMembers(Type type)
        {
            List <KeyValuePair <int, MemberInfoWithMeta> > indexedMemberInfo = new List <KeyValuePair <int, MemberInfoWithMeta> >(20);

            var flags = (BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);

            MemberInfo[] properties = type.GetProperties(flags).Where(x => x.CanRead).ToArray();
            MemberInfo[] fields     = type.GetFields(flags).Where(x => !x.Name.EndsWith("k__BackingField") && x.IsStatic == false).ToArray();
            MemberInfo[] allValues  = ArrayExt.Combine(properties, fields);
            foreach (MemberInfo property in allValues)
            {
                object[] attributes = property.GetCustomAttributes(true);
                int      index      = -1;
                bool     skipType   = false;
                bool     skipIsNull = false;
                foreach (Attribute eachAttr in attributes)
                {
                    if (eachAttr is Ignore)
                    {
                        index = -1;
                        break;
                    }

                    Index indexAttr = eachAttr as Index;
                    if (indexAttr != null)
                    {
                        index = indexAttr.Value;
                        continue;
                    }

                    SkipMetaData skipAttr = eachAttr as SkipMetaData;
                    if (skipAttr != null)
                    {
                        skipType   = skipAttr.Type;
                        skipIsNull = skipAttr.IsNull;
                    }
                }

                if (index != -1)
                {
                    MemberInfoWithMeta memberInfo = new MemberInfoWithMeta();
                    memberInfo.Info       = property;
                    memberInfo.SkipIsNull = skipIsNull;
                    memberInfo.SkipType   = skipType;
                    indexedMemberInfo.Add(new KeyValuePair <int, MemberInfoWithMeta>(index, memberInfo));
                }
            }

            indexedMemberInfo.Sort((x, y) => x.Key.CompareTo(y.Key));
            var members = new List <MemberInfoWithMeta>(indexedMemberInfo.Count);

            foreach (KeyValuePair <int, MemberInfoWithMeta> kvp in indexedMemberInfo)
            {
                members.Add(kvp.Value);
            }

            return(members);
        }
Пример #2
0
        IEnumerable <MemberInfoWithMeta> GetTypeMembers(Type type, bool includeFields = false, bool includeNonPublic = false)
        {
            var members = new List <MemberInfoWithMeta>();

            var flags = (BindingFlags.Public | BindingFlags.Instance);

            if (IncludeNonPublicProperties)
            {
                flags |= BindingFlags.NonPublic;
            }

            MemberInfo[] properties = type.GetProperties(flags).Where(x => x.CanRead).ToArray();
            MemberInfo[] fields     = type.GetFields(flags).Where(x => !x.Name.EndsWith("k__BackingField") && x.IsStatic == false).ToArray();
            MemberInfo[] allValues  = ArrayExt.Combine(properties, fields);

            foreach (MemberInfo property in allValues)
            {
                object[]           attributes = property.GetCustomAttributes(true);
                MemberInfoWithMeta memberInfo = new MemberInfoWithMeta();
                memberInfo.Info = property;
                bool ignore = false;
                foreach (Attribute eachAttr in attributes)
                {
                    if (eachAttr is Ignore)
                    {
                        ignore = true;
                        break;
                    }
                    SkipMetaData skipAttr = eachAttr as SkipMetaData;
                    if (skipAttr == null)
                    {
                        continue;
                    }

                    memberInfo.SkipType   = skipAttr.Type;
                    memberInfo.SkipIsNull = skipAttr.IsNull;
                }
                if (ignore)
                {
                    continue;
                }

                members.Add(memberInfo);
            }

            return(members);
        }