Home > Net >  Retrieve value from field attribute C#
Retrieve value from field attribute C#

Time:08-09

I created some custom attribute to apply it on a class member:

    [
        System.AttributeUsage(
            AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true
        )
    ]
    internal class ActionAttribute : Attribute
    {
        private Action action;

        public ActionAttribute(Action action)
        {
            this.action = action;
        }

        public Action getThis()
        {
            return this.action;
        }

    }

But am struggling on how to retrieve it's value using reflection.

This is my attempt:

public static Device Serialize(string deviceName, Dictionary<string, dynamic> fields)
{
    var itce = devices[deviceName];
    Type objectType = itce.GetType();
    MemberInfo[] fieldsInfo = objectType.GetMembers();

    foreach (var field in fieldsInfo.Where(p => p.MemberType == MemberTypes.Property))
    {
        Console.WriteLine(field.Name);
        object[] actionAttributes = field.GetCustomAttributes(typeof(ActionAttribute), false);
        foreach (var cAttr in actionAttributes)
        {
            Console.WriteLine("Attrs: "   cAttr.GetType());
        }
    }
    return itce;
}

Where in the variable itce I just retrieve a previously allocated instance of a type that contains those attributes using a factory pattern.

The thing I want it's is actual value, but I only can read it's class definition full name. It's obvious, I am asking for it to the GetType() method, but I have only four opts available like ToString() and things like that. I am imagine that I am missing some type cast probably? Don't know. Hope someone could help me with this.

BTW, Action type it's just an enum:

enum Action
{
    Read,
    Write
}

and, a simple example of the usage of the attributes:

    public class Device : Display
    {
        [Action(Action.Read)]
        [Action(Action.Write)]
        public string device_name { get; set; }

        public Device(string device_name)
        {
            this.device_name = device_name;
        }
    }

So, the idea, it's to retrieve the value of the attribute whenever a type has a field annotated. Above, Device has two annotations, Read and Write. I want to recover with reflection the actual value or values attached to that field.

device_name has two attributes, so I need to recover Action.Read and Action.Write.

Thanks.

CodePudding user response:

As @freakish pointed, solution it's pretty straightfoward.

object[] actionAttributes = field.GetCustomAttributes(typeof(ActionAttribute), false);
foreach (var cAttr in actionAttributes)   
{
    var attr = (ActionAttribute)cAttr;   
    Console.WriteLine("Attrs: "   attr.getThis());
}

Casting the var cAttr to the attribute type allows me to easily access the info that holds the field attribute.

CodePudding user response:

Instead of using var for your loop control variable and it being object, specify the type as your attribute:

object[] actionAttributes = field.GetCustomAttributes(typeof(ActionAttribute), false);
foreach (ActionAttribute cAttr in actionAttributes)
{
    Console.WriteLine("Attrs: "   cAttr.getThis());
}

Of course, you really ought to be using a public read-only property, rather than a private field and a method, to access that value.

  • Related