Ejemplo n.º 1
0
        /// <summary>
        /// 【Extends】Gets a dictionary with all members on the given <paramref name="type"/> and their associated attributes.
        /// Only members of the given <paramref name="memberTypes"/> and matching <paramref name="bindingFlags"/> will
        /// be included in the result.
        /// The list of attributes associated with each member can optionally be filtered by supplying a list of
        /// <paramref name="attributeTypes"/>, in which case only members with at least one of these will be
        /// included in the result.
        /// </summary>
        /// <returns>An dictionary mapping all matching members to their associated attributes. This value
        /// will never be null. The attribute list associated with each member in the dictionary will likewise
        /// never be null.</returns>
        public static Dictionary <MemberInfo, List <Attribute> > MembersAndAttributes(this Type type,
                                                                                      MemberTypes memberTypes,
                                                                                      Flags bindingFlags,
                                                                                      params Type[] attributeTypes)
        {
            var members = from m in type.Members(memberTypes, bindingFlags)
                          let a = m.Attributes(attributeTypes)
                                  where a.Count() > 0
                                  select new { Member = m, Attributes = a.ToList() };

            return(members.ToDictionary(m => m.Member, m => m.Attributes));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 【Extends】Gets all members on the given <paramref name="type"/> that match the specified
        /// <paramref name="bindingFlags"/>.
        /// The resulting list of members can optionally be filtered by supplying a list of
        /// <paramref name="attributeTypes"/>, in which case only members decorated with at least one of
        /// these will be included.
        /// </summary>
        /// <param name="type">The type on which to reflect.</param>
        /// <param name="memberTypes">The <see href="MemberTypes"/> to include in the search.</param>
        /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="Flags"/> combination
        /// used to define the search behavior and result filtering.</param>
        /// <param name="attributeTypes">The optional list of attribute types with which members should
        /// be decorated. If this parameter is <c>null</c> or empty then all fields and properties
        /// matching the given <paramref name="bindingFlags"/> will be included in the result.</param>
        /// <returns>A list of all matching members on the type. This value will never be null.</returns>
        public static List <MemberInfo> MembersWith(this Type type, MemberTypes memberTypes, Flags bindingFlags, params Type[] attributeTypes)
        {
            bool hasTypes = attributeTypes != null && attributeTypes.Length > 0;
            var  query    = from m in type.Members(memberTypes, bindingFlags)
                            where !hasTypes || m.HasAnyAttribute(attributeTypes)
                            select m;

            return(query.ToList());
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 【Extends】Gets all fields and properties on the given <paramref name="type"/> that match the specified
 /// <paramref name="bindingFlags"/>.
 /// The resulting list of members can optionally be filtered by supplying a list of
 /// <paramref name="attributeTypes"/>, in which case only members decorated with at least one of
 /// these will be included.
 /// </summary>
 /// <param name="type">The type on which to reflect.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="Flags"/> combination
 /// used to define the search behavior and result filtering.</param>
 /// <param name="attributeTypes">The optional list of attribute types with which members should
 /// be decorated. If this parameter is <c>null</c> or empty then all fields and properties
 /// matching the given <paramref name="bindingFlags"/> will be included in the result.</param>
 /// <returns>A list of all matching fields and properties on the type. This value will never be null.</returns>
 public static List <MemberInfo> FieldsAndPropertiesWith(this Type type, Flags bindingFlags, params Type[] attributeTypes)
 {
     return(type.MembersWith(MemberTypes.Field | MemberTypes.Property, bindingFlags, attributeTypes));
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 【Extends】Gets all members of the given <paramref name="memberTypes"/> on the given <paramref name="type"/>
 /// that match the specified <paramref name="bindingFlags"/> and are decorated with an
 /// <see href="Attribute"/> of the given type <typeparamref name="T"/>.
 /// </summary>
 /// <param name="type">The type on which to reflect.</param>
 /// <param name="memberTypes">The <see href="MemberTypes"/> to include in the search.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="Flags"/> combination
 /// used to define the search behavior and result filtering.</param>
 /// <returns>A list of all matching members on the type. This value will never be null.</returns>
 public static List <MemberInfo> MembersWith <T>(this Type type, MemberTypes memberTypes, Flags bindingFlags)
 {
     return(type.MembersWith(memberTypes, bindingFlags, typeof(T)));
 }