2

I have an enum with some members marked by custom attribute like:

public enum VideoClipTypeEnum : int
{
    Exhibitions = 1,

    TV = 2,

    [ClipTypeDisplayAttribute(false)]
    Content = 3
}

My attribute is:

public class ClipTypeDisplayAttribute : DescriptionAttribute
    {
        #region Private Variables

        private bool _treatAsPublicType;

        #endregion

        #region Ctor

        public ClipTypeDisplayAttribute(bool treatAsPublicType)
        {
            _treatAsPublicType = treatAsPublicType;
        }

        #endregion

        #region Public Props

        public bool TreatAsPublicType
        {
            get
            {
                return _treatAsPublicType;
            }
            set
            {
                _treatAsPublicType = value;
            }
        }

        #endregion
    }

What is the best way of getting the values of members marked with my custom attribute into List?

2
  • Can you be more specific? There are no fields in your example - are you talking about enum members? Do you want to receive only enum members, that are marked with attributes? Commented Mar 21, 2013 at 6:35
  • Exactly! Members... sorry for that trap ) Commented Mar 21, 2013 at 6:39

1 Answer 1

2

Try this

var values = 
    from f in typeof(VideoClipTypeEnum).GetFields()
    let attr = f.GetCustomAttributes(typeof(ClipTypeDisplayAttribute))
                .Cast<ClipTypeDisplayAttribute>()
                .FirstOrDefault()
    where attr != null
    select f;

This will actually return the FieldInfo for the enum value. To get the raw value, try this.

var values = 
    ... // same as above
    select (VideoClipTypeEnum)f.GetValue(null);

If you also want to filter by some property of the attribute, you can do that too. Like this

var values = 
    ... // same as above
    where attr != null && attr.TreatAsPublicType
    ... // same as above

Note: This works because enum values (e.g. VideoClipTypeEnum.TV) are actually implemented as static, constant fields of the VideoClipTypeEnum internally.

To get a List<int> use this

var values = 
    (from f in typeof(VideoClipTypeEnum).GetFields()
     let attr = f.GetCustomAttributes(typeof(ClipTypeDisplayAttribute))
                 .Cast<ClipTypeDisplayAttribute>()
                 .FirstOrDefault()
     where attr != null
     select (int)f.GetValue(null))
    .ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! But what if I need to get the List<int> of values? I need that list to filter the videoClips in my repo.
GetCustomAttributes<T>() is an extension that wraps up two of your steps and makes it more readable, IMO.
@Damien_The_Unbeliever seems like that's only available in 4.5, but good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.