internal static IEnumerable <IAttribute> GetAttributes(IMember member)
        {
            HashSet <IMember> visitedMembers = new HashSet <IMember>();

            do
            {
                member = member.MemberDefinition;                 // it's sufficient to look at the definitions
                if (!visitedMembers.Add(member))
                {
                    // abort if we seem to be in an infinite loop (cyclic inheritance)
                    break;
                }
                foreach (var attr in member.GetAttributes())
                {
                    yield return(attr);
                }
            } while (member.IsOverride && (member = InheritanceHelper.GetBaseMember(member)) != null);
        }
Beispiel #2
0
 /// <summary>
 /// Gets the attributes on the entity.
 /// </summary>
 /// <param name="entity">The entity on which the attributes are declared.</param>
 /// <param name="inherit">
 /// Specifies whether attributes inherited from base classes and base members
 /// (if the given <paramref name="entity"/> in an <c>override</c>)
 /// should be returned.
 /// </param>
 /// <returns>
 /// Returns the list of attributes that were found.
 /// If inherit is true, attributes from the entity itself are returned first;
 /// followed by attributes inherited from the base entity.
 /// </returns>
 public static IEnumerable <IAttribute> GetAttributes(this IEntity entity, bool inherit)
 {
     if (inherit)
     {
         if (entity is ITypeDefinition td)
         {
             return(InheritanceHelper.GetAttributes(td));
         }
         else if (entity is IMember m)
         {
             return(InheritanceHelper.GetAttributes(m));
         }
         else
         {
             throw new NotSupportedException("Unknown entity type");
         }
     }
     else
     {
         return(entity.GetAttributes());
     }
 }