public static void CheckPropertyAccess(PropertyInfo propertyInfo, PropertyAccessOptions accessOptions) { ArgumentException CreatePropertyNotMatchAccessException() => new($"The property ({propertyInfo.Name}) does not match accessibility restrictions."); switch (accessOptions) { case PropertyAccessOptions.Getters: { if (!(propertyInfo.GetMethod != null && !propertyInfo.GetMethod.IsStatic && propertyInfo.GetMethod.IsPublic)) { throw CreatePropertyNotMatchAccessException(); } break; } case PropertyAccessOptions.Setters: { if (!(propertyInfo.SetMethod != null && !propertyInfo.SetMethod.IsStatic && propertyInfo.SetMethod.IsPublic)) { throw CreatePropertyNotMatchAccessException(); } break; } } }
public static IEnumerable <PropertyInfo> GetProperties <T>(this T x, PropertyAccessOptions accessOptions, params Expression <Func <T, object> >[] propertySelectors) { if (x is null) { throw new ArgumentNullException(nameof(x)); } return(TypeVisit.GetProperties(accessOptions, propertySelectors)); }
/// <summary> /// Get all properties from the given Type.<br /> /// 从给定的 Type 中获得所有属性。 /// </summary> /// <param name="type"></param> /// <param name="accessOptions"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="InvalidOperationException"></exception> public static IEnumerable <PropertyInfo> GetProperties(Type type, PropertyAccessOptions accessOptions) { if (type is null) { throw new ArgumentNullException(nameof(type)); } return(accessOptions switch { PropertyAccessOptions.Getters => PropertyReflectionHelper.GetPropertiesWithPublicInstanceGetters(type), PropertyAccessOptions.Setters => PropertyReflectionHelper.GetPropertiesWithPublicInstanceSetters(type), PropertyAccessOptions.Both => PropertyReflectionHelper.GetPropertiesWithPublicInstance(type), _ => throw new InvalidOperationException("Invalid operation for unknown access type") });
public static IEnumerable <PropertyInfo> GetProperties <T>(this T x, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both) { return(TypeVisit.GetProperties(typeof(T), accessOptions)); }
public static PropertyInfo GetProperty <T, TProperty>(this T x, Expression <Func <T, TProperty> > propertySelector, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both) { if (x is null) { throw new ArgumentNullException(nameof(x)); } if (propertySelector is null) { throw new ArgumentNullException(nameof(propertySelector)); } return(TypeVisit.GetProperty(propertySelector, accessOptions)); }