Ejemplo n.º 1
0
        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;
            }
            }
        }
Ejemplo n.º 2
0
 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));
 }
Ejemplo n.º 3
0
        /// <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")
            });
Ejemplo n.º 4
0
 public static IEnumerable <PropertyInfo> GetProperties <T>(this T x, PropertyAccessOptions accessOptions = PropertyAccessOptions.Both)
 {
     return(TypeVisit.GetProperties(typeof(T), accessOptions));
 }
Ejemplo n.º 5
0
 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));
 }