How to create a custom attribute for reading a property name and value?http://forums.asp.net/t/1781304.aspx/1?How+to+create+a+custom+attribute+for+reading+a+property+name+and+value+Fri, 16 Mar 2012 19:00:09 -040017813044884135http://forums.asp.net/p/1781304/4884135.aspx/1?How+to+create+a+custom+attribute+for+reading+a+property+name+and+value+How to create a custom attribute for reading a property name and value? <p>I have read on Attributes and think this might solve a problem.</p> <p>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.&nbsp;</p> <p>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?</p> <p>Something like this:</p> <pre class="prettyprint">public class SomeClass { [SomeAttribute(Name=&quot;City&quot;)] public string City { get; set; } [SomeAttribute(Name = &quot;State Abbreviation&quot;)] public string StateAbbrev { get; set; } }</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">Then I can read through each item in a list and get the property names and values.</pre> 2012-03-16T14:31:00-04:004884366http://forums.asp.net/p/1781304/4884366.aspx/1?Re+How+to+create+a+custom+attribute+for+reading+a+property+name+and+value+Re: How to create a custom attribute for reading a property name and value? <p>You could use the DisplayNameAttribute, see:&nbsp;http://msdn.microsoft.com/en-us/library/system.componentmodel.displaynameattribute.aspx</p> <p>Or you could define your own, if you prefer.</p> 2012-03-16T17:10:49-04:004884548http://forums.asp.net/p/1781304/4884548.aspx/1?Re+How+to+create+a+custom+attribute+for+reading+a+property+name+and+value+Re: How to create a custom attribute for reading a property name and value? <p>But can I read this from outside of the class, and also how can I read the value of each property?</p> <p>I do not know what the class is (in my code).</p> <p>I want to iterate through all properties that have the attribute, get their name, and their value from an outside class.</p> <p>Thanks.</p> <p>&nbsp;</p> 2012-03-16T18:50:16-04:004884564http://forums.asp.net/p/1781304/4884564.aspx/1?Re+How+to+create+a+custom+attribute+for+reading+a+property+name+and+value+Re: How to create a custom attribute for reading a property name and value? <p>Yes.</p> <p>That's exactly what reflection does.</p> <p>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).</p> <p>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.</p> 2012-03-16T19:00:09-04:00