/// <summary> /// 判断对象是否具有指定名称的成员。 /// </summary> /// <param name="instance">被搜索的对象。</param> /// <param name="name">要搜索的成员名称。</param> /// <param name="binding">指定搜索对象中那些类型的成员。</param> /// <param name="ignoreCase">指定是否忽略成员名称的大小写。</param> /// <returns>如果对象中指定类型的成员中存在指定名称的成员,返回 true;否则返回 false。</returns> public static object Contains(this INamedAccessable instance, string name, MemberTypeBinding binding = MemberTypeBinding.All, bool ignoreCase = false) { if (instance == null) { throw new ArgumentNullException("instance"); } var proxy = TypeProxy.GetProxy(instance.GetType()); return(proxy.Contains(name, binding, ignoreCase)); }
/// <summary> /// 判断当前类型中,是否存在指定名称的成员。 /// </summary> /// <param name="name">要搜索的成员名称。</param> /// <param name="binding">指定搜索那些类型的成员。</param> /// <param name="ignoreCase">指定是否忽略大小写。</param> /// <returns>如果在指定的成员类型中搜索到该成员名称,返回 true;否则返回 false。</returns> public bool Contains(string name, MemberTypeBinding binding = MemberTypeBinding.All, bool ignoreCase = false) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullException("name"); } var comparison = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; return((binding & MemberTypeBinding.Property) == MemberTypeBinding.Property && props.Any(pi => pi.Name.Equals(name, comparison)) || (binding & MemberTypeBinding.Field) == MemberTypeBinding.Field && fields.Any(fi => fi.Name.Equals(name, comparison)) || (binding & MemberTypeBinding.Method) == MemberTypeBinding.Method && methods.Any(mi => mi.Name.Equals(name, comparison)) || (binding & MemberTypeBinding.Event) == MemberTypeBinding.Event && events.Any(ei => ei.Name.Equals(name, comparison))); }
/// <summary> /// 获取指定类型的成员名称。 /// </summary> /// <param name="typeBinding">成员类型。</param> /// <param name="includeStatic">指定返回的名称中,是否包含静态成员。</param> /// <param name="ignoreCase">是否忽略成员名称大小写。</param> /// <returns>返回指定类型的所有成员的名称。</returns> public IEnumerable <string> GetMemberNames(MemberTypeBinding typeBinding = MemberTypeBinding.All, bool includeStatic = true, bool ignoreCase = false) { var result = Enumerable.Empty <string>(); var comparer = ignoreCase ? StringComparer.CurrentCultureIgnoreCase : StringComparer.CurrentCulture; if ((typeBinding & MemberTypeBinding.Property) == MemberTypeBinding.Property) { result = result.Concat(props.Where(pi => includeStatic || !pi.IsStatic()).Select(pi => pi.Name)); } if ((typeBinding & MemberTypeBinding.Field) == MemberTypeBinding.Field) { result = result.Concat(fields.Where(fi => includeStatic || !fi.IsStatic).Select(fi => fi.Name)); } if ((typeBinding & MemberTypeBinding.Method) == MemberTypeBinding.Method) { result = result.Concat(methods.Where(mi => includeStatic || !mi.IsStatic).Select(mi => mi.Name)); } if ((typeBinding & MemberTypeBinding.Event) == MemberTypeBinding.Event) { result = result.Concat(events.Select(ei => ei.Name)); } return(Enumerable.Distinct(result, comparer).ToArray()); }