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