Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom attribute on property - Getting type and value of attributed property

I have the following custom attribute, which can be applied on properties:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] public class IdentifierAttribute : Attribute { } 

For example:

public class MyClass {     [Identifier()]     public string Name { get; set; }      public int SomeNumber { get; set; }     public string SomeOtherProperty { get; set; } } 

There will also be other classes, to which the Identifier attribute could be added to properties of different type:

public class MyOtherClass {     public string Name { get; set; }      [Identifier()]     public int SomeNumber { get; set; }      public string SomeOtherProperty { get; set; } } 

I then need to be able to get this information in my consuming class. For example:

public class TestClass<T> {     public void GetIDForPassedInObject(T obj)     {         var type = obj.GetType();         //type.GetCustomAttributes(true)???     } } 

What's the best way of going about this? I need to get the type of the [Identifier()] field (int, string, etc...) and the actual value, obviously based on the type.

like image 675
Alex Avatar asked Jul 20 '10 10:07

Alex


People also ask

How do I get custom attribute values?

Retrieving a custom attribute is a simple process. First, declare an instance of the attribute you want to retrieve. Then, use the Attribute. GetCustomAttribute method to initialize the new attribute to the value of the attribute you want to retrieve.

How do you write a custom attribute?

Creating Custom Attributes (C#)The class name AuthorAttribute is the attribute's name, Author , plus the Attribute suffix. It is derived from System. Attribute , so it is a custom attribute class. The constructor's parameters are the custom attribute's positional parameters.

How can you specify target for a custom attribute?

Which of the following are correct ways to specify the targets for a custom attribute? A. By applying AttributeUsage to the custom attribute's class definition.

What are custom attributes?

Custom attributes. A custom attribute is a property that you can define to describe assets. Custom attributes extend the meaning of an asset beyond what you can define with the standard attributes. You can create a custom attribute and assign to it a value that is an integer, a range of integers, or a string.


2 Answers

Something like the following,, this will use only the first property it comes accross that has the attribute, of course you could place it on more than one..

    public object GetIDForPassedInObject(T obj)     {         var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)                    .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1);         object ret = prop !=null ?  prop.GetValue(obj, null) : null;          return ret;     }   
like image 169
Richard Friend Avatar answered Sep 18 '22 12:09

Richard Friend


public class TestClass<T> {     public void GetIDForPassedInObject(T obj)     {         PropertyInfo[] properties =             obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);                      PropertyInfo IdProperty = (from PropertyInfo property in properties                            where property.GetCustomAttributes(typeof(Identifier), true).Length > 0                            select property).First();           if(null == IdProperty)              throw new ArgumentException("obj does not have Identifier.");           Object propValue = IdProperty.GetValue(entity, null)     } } 
like image 29
this. __curious_geek Avatar answered Sep 18 '22 12:09

this. __curious_geek