Reading custom attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • filipk69@gmail.com

    Reading custom attributes

    Could someone point me in the right direction, please.

    I have an enum something like this:
    enum TextFieldType
    {
    [Description("Fi eld ABC"), FieldLabel("abc field")] FieldABC = 3000,
    [Description("Fi eld XYZ"), FieldLabel("xyz field")] FieldXYZ = 3001,
    …..
    …..
    }
    Where FieldLabel is a custom attribute.

    I have a class TextEntry that contains properties: TextFieldType and Content.

    I have a generic list of TextEntry objects that I'm looping through and outputting
    info to the screen and what I would like to do is to display the text inside
    the FieldLabel attribute and the value in "Content".

    abc field: Here is the content of the ABC Type field...
    xyz field: Here is the content of the XYZ Type field…
    …
    ....

    Getting the "Content" is not an issue, but how do I get the value from the
    "FieldLabel ". I know I have to use reflection here but the only samples I
    found show retrieving custom attribute from a class or a method. I tried
    to tweek the 'method' example with no success.

    Any help would be appreciated.
    Thanks Filip



  • Larry Lard

    #2
    Re: Reading custom attributes

    filipk69@gmail. com wrote:
    Could someone point me in the right direction, please.
    I have an enum something like this: enum TextFieldType {
    [Description("Fi eld ABC"), FieldLabel("abc field")] FieldABC = 3000,
    [Description("Fi eld XYZ"), FieldLabel("xyz field")] FieldXYZ = 3001,
    ….. ….. } Where FieldLabel is a custom attribute.
    So

    [System.Attribut eUsage(System.A ttributeTargets .Field)]
    public class FieldLabel : System.Attribut e
    {
    public string text;
    public FieldLabel(stri ng text)
    {
    this.text = text;
    }
    }

    public enum TextFieldType
    {
    [Description("Fi eld ABC"), FieldLabel("abc field")]
    FieldABC = 3000,
    [Description("Fi eld XYZ"), FieldLabel("xyz field")]
    FieldXYZ = 3001
    }
    I have a class TextEntry that contains properties: TextFieldType and
    Content.
    public class TextEntry
    {
    public TextFieldType TextFieldType;
    public string Content;

    public TextEntry(TextF ieldType type, string content)
    {
    this.TextFieldT ype = type;
    this.Content = content;
    }
    }
    I have a generic list of TextEntry objects that I'm looping through and
    outputting info to the screen
    List<TextEntryl ist = new List<TextEntry> ();

    list.Add(new TextEntry(TextF ieldType.FieldA BC, "foo"));
    list.Add(new TextEntry(TextF ieldType.FieldX YZ, "bar"));

    foreach (TextEntry t in list)


    and what I would like to do is to display
    the text inside the FieldLabel attribute and the value in "Content".
    abc field: Here is the content of the ABC Type field... xyz field: Here
    is the content of the XYZ Type field… … ....
    Getting the "Content" is not an issue, but how do I get the value from
    the "FieldLabel ". I know I have to use reflection here but the only
    samples I found show retrieving custom attribute from a class or a
    method. I tried to tweek the 'method' example with no success.
    {
    FieldInfo f = typeof
    (TextFieldType) .GetField(t.Tex tFieldType.ToSt ring());
    object[] customatts =
    f.GetCustomAttr ibutes(typeof(F ieldLabel), false);
    FieldLabel fl = (FieldLabel)cus tomatts[0];
    Console.WriteLi ne("Descriptio n {0} Label {1} content
    {2}", t.TextFieldType .ToString(), fl.text,
    t.Content);
    }
    >
    Any help would be appreciated. Thanks Filip
    However - and this is a stylistic point, so your opinion may vary - this
    is pretty horrible. If you have arbitrary data of type U (here string)
    to associate one-to-one with data of type T (here TextFieldType), you
    don't really need to mess about with attributes and reflection.
    (Personally I have always interpreted the need for reflection as a
    warning that something is 'not quite right' with the design). How about
    this:

    // better
    private static Dictionary<Text FieldType, stringfieldlabe ls =
    new Dictionary<Text FieldType, string>();
    static Program()
    {
    fieldlabels.Add (TextFieldType. FieldABC, "label for abc");
    fieldlabels.Add (TextFieldType. FieldXYZ, "label for xyz");
    }

    // then
    private static void info(List<TextE ntrylist)
    {
    foreach (TextEntry t in list)
    {
    string label = fieldlabels[t.TextFieldType];
    Console.WriteLi ne("Descriptio n {0} Label {1} content
    {2}", t.TextFieldType .ToString(), label,
    t.Content);
    }

    }


    --
    Larry Lard
    larrylard@googl email.com
    The address is real, but unread - please reply to the group
    For VB and C# questions - tell us which version

    Comment

    • Laurent Bugnion [MVP]

      #3
      Re: Reading custom attributes

      Hi,

      Larry Lard wrote:
      filipk69@gmail. com wrote:
      >Could someone point me in the right direction, please.
      >I have an enum something like this: enum TextFieldType {
      >[Description("Fi eld ABC"), FieldLabel("abc field")] FieldABC = 3000,
      >[Description("Fi eld XYZ"), FieldLabel("xyz field")] FieldXYZ = 3001,
      >….. ….. } Where FieldLabel is a custom attribute.
      >
      So
      >
      [System.Attribut eUsage(System.A ttributeTargets .Field)]
      public class FieldLabel : System.Attribut e
      By convention, classes deriving from System.Attribut e should have the
      "Attribute" suffix. This suffix can be omitted when you use the
      attribute, so it makes its usage easier.

      See
      Design your own custom attributes in .NET. Custom attributes are essentially classes derived directly or indirectly from System.Attribute.

      (scroll down until "Declaring the Attribute Class").

      HTH,
      Laurent
      --
      Laurent Bugnion [MVP ASP.NET]
      Software engineering: http://www.galasoft-LB.ch
      PhotoAlbum: http://www.galasoft-LB.ch/pictures
      Support children in Calcutta: http://www.calcutta-espoir.ch

      Comment

      Working...