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.
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.
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.
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.
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.
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; }
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) } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With