Reading custom attributes

F

filipk69

Could someone point me in the right direction, please.

I have an enum something like this:
enum TextFieldType
{
[Description("Field ABC"), FieldLabel("abc field")] FieldABC = 3000,
[Description("Field 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
 
L

Larry Lard

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

So

[System.AttributeUsage(System.AttributeTargets.Field)]
public class FieldLabel : System.Attribute
{
public string text;
public FieldLabel(string text)
{
this.text = text;
}
}

public enum TextFieldType
{
[Description("Field ABC"), FieldLabel("abc field")]
FieldABC = 3000,
[Description("Field 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(TextFieldType type, string content)
{
this.TextFieldType = type;
this.Content = content;
}
}
I have a generic list of TextEntry objects that I'm looping through and
outputting info to the screen

List<TextEntry> list = new List<TextEntry>();

list.Add(new TextEntry(TextFieldType.FieldABC, "foo"));
list.Add(new TextEntry(TextFieldType.FieldXYZ, "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.TextFieldType.ToString());
object[] customatts =
f.GetCustomAttributes(typeof(FieldLabel), false);
FieldLabel fl = (FieldLabel)customatts[0];
Console.WriteLine("Description {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<TextFieldType, string> fieldlabels =
new Dictionary<TextFieldType, string>();
static Program()
{
fieldlabels.Add(TextFieldType.FieldABC, "label for abc");
fieldlabels.Add(TextFieldType.FieldXYZ, "label for xyz");
}

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

}
 
L

Laurent Bugnion [MVP]

Hi,

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

So

[System.AttributeUsage(System.AttributeTargets.Field)]
public class FieldLabel : System.Attribute

By convention, classes deriving from System.Attribute should have the
"Attribute" suffix. This suffix can be omitted when you use the
attribute, so it makes its usage easier.

See
http://msdn2.microsoft.com/en-us/library/84c42s56.aspx
(scroll down until "Declaring the Attribute Class").

HTH,
Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top