Attributes

M

Martin

Hi All,

I'm trying to get at the data in a variable marked with my custom
attribute. I'm not entirely sure where to begin. I've created my
attribute class which is okay but at runtime I want to be able to access
all the fields in an object instance that are marked with my attribute
and output the data in the field/variable. Anyone provide any assistance?

Regards,

Martin
 
S

Stefan Holdermans

Martin,

MC> I'm trying to get at the data in a variable marked with my custom
MC> attribute.

Use reflection:

using System;
using System.Collections;
using System.Reflection;

[AttributeUsage(AttributeTargets.Field)]
public class TaggedAttribute : Attribute {
public static object[] GetValuesOfTaggedFields(object value) {
ArrayList vs = new ArrayList();

foreach (FieldInfo fi in value.GetType().GetFields()) {
if (Attribute.IsDefined(fi, typeof(TaggedAttribute))) {
vs.Add(fi.GetValue(value));
}
}

return vs.ToArray();
}
}

public class Point {
[Tagged]
public int X;

[Tagged]
public int Y;

public static void Main() {
Point p = new Point();

p.X = 0;
p.Y = 1;

foreach (object o in TaggedAttribute.GetValuesOfTaggedFields(p)) {
Console.WriteLine(o);
}
}
}

HTH,

Stefan
 
R

Richard Blewett [DevelopMentor]

void PrintAllValues(object o)
{
Type t = o.GetType();
Type attrType = typeof(MyCustomAttribute);
foreach(FieldInfo fi in t.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) )
{
if( fi.IsDefined(attrType, false) )
{
Console.WriteLine( fi.GetValue(o) );
}
}
}

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi All,

I'm trying to get at the data in a variable marked with my custom
attribute. I'm not entirely sure where to begin. I've created my
attribute class which is okay but at runtime I want to be able to access
all the fields in an object instance that are marked with my attribute
and output the data in the field/variable. Anyone provide any assistance?

Regards,

Martin
 
N

Norfy

Quick template using reflection to first get the member you're
interested in and then querying it for a specific attribute.
FieldInfo field =
anObject.GetType().GetField(...);
YourCustomAttribute attribute = Attribute.GetCustomAttribute(
field,
typeof(YourCustomAttribute)) as
YourCustomAttribute;
if (attribute != null)
{ ... }

If you're after other Attributes as well then consider using
GetCustomAttributes instead.

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
M

Martin

Hi Guys,

Thanks for all the pointers, I'm much wiser now :) Here's what I ended
up with, (much the same as Stefan suggested)

Regards,

Martin

using System;
using System.Collections;
using System.Reflection;

namespace TestAttributes
{
/// <summary>
/// Summary description for GraphMLDataAttribute.
/// </summary>
[AttributeUsage(AttributeTargets.All,AllowMultiple = true)]
public class GraphMLDataAttribute : System.Attribute
{
private String _Key;
public GraphMLDataAttribute(String Key)
{
_Key = Key;
}

public String Key
{
get
{
return _Key;
}
set
{
_Key = value;
}
}

public static Hashtable GetValuesOfTaggedFields(object value)
{
Hashtable vs = new Hashtable();
String sKeyName = "";

foreach (PropertyInfo fi in value.GetType().GetProperties())
{
if (Attribute.IsDefined(fi,
typeof(GraphMLDataAttribute)))
{
sKeyName = fi.Name;
Object[] attributes =
fi.GetCustomAttributes(
typeof(GraphMLDataAttribute),false);
foreach(Object o in attributes)
{
if ( o.GetType() ==
typeof(GraphMLDataAttribute) )
{
sKeyName = ((GraphMLDataAttribute)o).Key;
break;
}

}
vs.Add(sKeyName,fi.GetValue(value,null));
}
}

return vs;
}
}
}
 

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