i have enum members marked 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 { { return _treataspublictype; } set { _treataspublictype = value; } } #endregion }
what best way of getting values of members marked custom attribute list?
try this
var values = f in typeof(videocliptypeenum).getfields() let attr = f.getcustomattributes(typeof(cliptypedisplayattribute)) .cast<cliptypedisplayattribute>() .firstordefault() attr != null select f;
this return fieldinfo
enum value. raw value, try this.
var values = ... // same above select (videocliptypeenum)f.getvalue(null);
if want filter property of attribute, can too. this
var values = ... // same above attr != null && attr.treataspublictype ... // same above
note: works because enum values (e.g. videocliptypeenum.tv
) implemented static, constant fields of videocliptypeenum
internally.
to list<int>
use this
var values = (from f in typeof(videocliptypeenum).getfields() let attr = f.getcustomattributes(typeof(cliptypedisplayattribute)) .cast<cliptypedisplayattribute>() .firstordefault() attr != null select (int)f.getvalue(null)) .tolist();
Comments
Post a Comment