I have read on Attributes and think this might solve a problem.
I would like to look through a list of class objects, and get the specific name and value of particular properties decorated with some attribute.
Is there a basic attribute that I can mark my properties with, ideally giving me the option for a 'friendly' property name when the actual propername is ugly?
Something like this:
public class SomeClass
{
[SomeAttribute(Name="City")]
public string City { get; set; }
[SomeAttribute(Name = "State Abbreviation")]
public string StateAbbrev { get; set; }
}
Then I can read through each item in a list and get the property names and values.
Google or Bing for information on how to use Reflection (it involves calling methods such as GetProperties(), GetMethods(), etc.) from the System.Type object for your class (which you can always find for any object just by calling obj.GetType(), or for a
class by using typeof(classname).
When you get the properties you can then query for the attributes (GetCustomAttributes() from the returned MemberInfo), and then read the values of the attribute's properties.
agent_smith
Contributor
2030 Points
571 Posts
How to create a custom attribute for reading a property name and value?
Mar 16, 2012 02:31 PM|LINK
I have read on Attributes and think this might solve a problem.
I would like to look through a list of class objects, and get the specific name and value of particular properties decorated with some attribute.
Is there a basic attribute that I can mark my properties with, ideally giving me the option for a 'friendly' property name when the actual propername is ugly?
Something like this:
public class SomeClass { [SomeAttribute(Name="City")] public string City { get; set; } [SomeAttribute(Name = "State Abbreviation")] public string StateAbbrev { get; set; } }AGENT_SMITH
DMW
All-Star
15943 Points
2353 Posts
Re: How to create a custom attribute for reading a property name and value?
Mar 16, 2012 05:10 PM|LINK
You could use the DisplayNameAttribute, see: http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.aspx
Or you could define your own, if you prefer.
Dave
agent_smith
Contributor
2030 Points
571 Posts
Re: How to create a custom attribute for reading a property name and value?
Mar 16, 2012 06:50 PM|LINK
But can I read this from outside of the class, and also how can I read the value of each property?
I do not know what the class is (in my code).
I want to iterate through all properties that have the attribute, get their name, and their value from an outside class.
Thanks.
AGENT_SMITH
DMW
All-Star
15943 Points
2353 Posts
Re: How to create a custom attribute for reading a property name and value?
Mar 16, 2012 07:00 PM|LINK
Yes.
That's exactly what reflection does.
Google or Bing for information on how to use Reflection (it involves calling methods such as GetProperties(), GetMethods(), etc.) from the System.Type object for your class (which you can always find for any object just by calling obj.GetType(), or for a class by using typeof(classname).
When you get the properties you can then query for the attributes (GetCustomAttributes() from the returned MemberInfo), and then read the values of the attribute's properties.
Dave