public static IEnumerable <MemberInfo> GetMembers(this Type type, bool includeProperties = true, bool includeFields = false, bool includeNonPublic = false) { BindingFlags flags = (BindingFlags.Public | BindingFlags.Instance); if (includeNonPublic) { flags |= BindingFlags.NonPublic; } if (includeProperties && includeFields) { MemberInfo[] properties = type.GetProperties(flags).Where(x => x.CanRead).ToArray <MemberInfo>(); MemberInfo[] fields = type.GetNonStaticFields(flags); MemberInfo[] allValues = ArrayExt.Combine(properties, fields); return(allValues); } if (includeProperties) { MemberInfo[] properties = type.GetProperties(flags).Where(x => x.CanRead).ToArray <MemberInfo>(); return(properties); } if (includeFields) { MemberInfo[] fields = type.GetNonStaticFields(flags); return(fields); } return(null); }
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); }
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); }
/// <summary> /// 将当前类型转换器提供者与指定的 <see cref="ConverterProvider"/> 合并。 /// </summary> /// <param name="provider">要合并的类型转换器提供者。</param> private void CombineWith(ConverterProvider provider) { Contract.Requires(provider != null && OriginType == provider.OriginType); foreach (var pair in provider.fromDict) { fromDict.Add(pair.Key, pair.Value); } foreach (var pair in provider.toDict) { toDict.Add(pair.Key, pair.Value); } if (subProvider.Length == 0) { subProvider = provider.subProvider; } else if (provider.subProvider.Length > 0) { subProvider = ArrayExt.Combine(subProvider, provider.subProvider); } }
/// <summary> /// 将当前类型转换器提供者与指定的 <see cref="ConverterProvider"/> 合并。 /// </summary> /// <param name="provider">要合并的类型转换器提供者。</param> private void CombineWith(ConverterProvider provider) { Contract.Requires(provider != null && this.OriginType == provider.OriginType); foreach (KeyValuePair <Type, Delegate> pair in provider.fromDict) { this.fromDict.Add(pair.Key, pair.Value); } foreach (KeyValuePair <Type, Delegate> pair in provider.toDict) { this.toDict.Add(pair.Key, pair.Value); } if (this.subProvider.Length == 0) { this.subProvider = provider.subProvider; } else if (provider.subProvider.Length > 0) { this.subProvider = ArrayExt.Combine(this.subProvider, provider.subProvider); } }