By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,996 Members | 1,095 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,996 IT Pros & Developers. It's quick & easy.

Reading custom attributes

P: n/a
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

Jan 25 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
fi******@gmail.com wrote:
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.Fiel d)]
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<TextEntrylist = 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, stringfieldlabels =
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<TextEntrylist)
{
foreach (TextEntry t in list)
{
string label = fieldlabels[t.TextFieldType];
Console.WriteLine("Description {0} Label {1} content
{2}", t.TextFieldType.ToString(), label,
t.Content);
}

}
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 25 '07 #2

P: n/a
Hi,

Larry Lard wrote:
fi******@gmail.com wrote:
>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.Fiel d)]
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
--
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
Jan 25 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.