<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Reference &#187; Custom Attribute</title>
	<atom:link href="http://sullivan.net/blog/tag/custom-attribute/feed/" rel="self" type="application/rss+xml" />
	<link>http://sullivan.net/blog</link>
	<description>A collection of code for my reference (and perhaps other people too)</description>
	<lastBuildDate>Wed, 25 Jan 2012 17:53:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Custom Attributes</title>
		<link>http://sullivan.net/blog/2010/03/custom-attributes-2/</link>
		<comments>http://sullivan.net/blog/2010/03/custom-attributes-2/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 14:09:06 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Custom Attribute]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=289</guid>
		<description><![CDATA[If you would like to adorn your classes with some custom meta-data, Custom Attributes are the way to go. First, start with your class. public class MyDumbClass { public MyDumbClass() { // do nothing } } Now we want to say some things about this class... like, is it a good class? We create an [...]]]></description>
			<content:encoded><![CDATA[<p>If you would like to adorn your classes with some custom meta-data, Custom Attributes are the way to go.</p>
<p>First, start with your class.</p>
<pre class="brush: c#">
public class MyDumbClass
{
    public MyDumbClass()
    {
        // do nothing
    }
}
</pre>
<p>Now we want to say some things about this class... like, is it a good class?<br />
We create an attribute where we can specify this info.<br />
Create a class that inherits from 'Attribute'.<br />
Add some fields to the class.</p>
<pre class="brush: c#">
public class MySpecialAttribute : Attribute
{
    public string GoodAdvice { get; set; }
    public bool ThisClassIsGood { get; set; }
}
</pre>
<p>Now you can find in your intellesense the "MySpecial" attribute. </p>
<pre class="brush: c#">
[MySpecial(ThisClassIsGood = true, GoodAdvice=&quot;Do no harm&quot;)]
public class MyDumbClass
{
    public MyDumbClass()
    {
        // do nothing
    }
}
</pre>
<p>The attribute name, as you can see, is the name of the attribute class without the 'Attribute' text. </p>
<p>Because there can never be to much good in the world, you may wish to add the attribute several times.<br />
Doing so will give you an error unless you specify the attribute can be used multiple times.</p>
<p>On the Attribute class, add an AttributeUsage attribute:</p>
<pre class="brush: c#">
[AttributeUsage(AttributeTargets.Class, AllowMultiple=true)]
public class MySpecialAttribute : Attribute
{
    public string GoodAdvice { get; set; }
    public bool ThisClassIsGood { get; set; }
}
</pre>
<p>We chose 'AttributeTarget.Class' because we are using it on a class. If you want the attribute used on a field, or method or something else, you can specify AttributeTargets.Method or whatever you need.</p>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2010/03/custom-attributes-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Custom Attributes</title>
		<link>http://sullivan.net/blog/2009/08/custom-attributes/</link>
		<comments>http://sullivan.net/blog/2009/08/custom-attributes/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 14:25:02 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Custom Attribute]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=76</guid>
		<description><![CDATA[I was asked about Custom Attributes and thought it would make a good blog entry. Custom Attributes are added to properties of a class. They give extra meaning to these properties. Here is an example of what they look like and how they are used: class MyClass { [SearchablePropertyAttribute(DisplayType = &#34;Primary&#34;)] public string FirtName { [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked about Custom Attributes and thought it would make a good blog entry.</p>
<p>Custom Attributes are added to properties of a class. They give extra meaning to these properties. </p>
<p>Here is an example of what they look like and how they are used:</p>
<pre class="brush: c#">
class MyClass
{
  [SearchablePropertyAttribute(DisplayType = &quot;Primary&quot;)]
  public string FirtName
  {
      get { return firtName; }
      set { firtName= value; }
  }

  [AddressPropertyAttribute(Order = 1, ValidationRequired = true)]
  public string Address
  {
      get { return address; }
      set { address= value; }
  }
}
</pre>
<p>SearchablePropertyAttribute and AddressPropertyAttribute are both classes I created. They must extend System.Attribute.</p>
<p>You assign the values to these properties in the class as shown above.</p>
<p>They can be as simple as this:</p>
<pre class="brush: c#">
public class SearchablePropertyAttribute : Attribute
{
    public SearchablePropertyAttribute() { }

    public string DisplayType = string.Empty;
}

public class AddressPropertyAttribute : Attribute
{
    public AddressPropertyAttribute() { }

    public int Order = 0;
    public bool ValidationRequired = false;
}
</pre>
<p>To use these attributes, you have to get the PropertyInfo of the object</p>
<pre class="brush: c#">
...
MyClass myclass = new MyClass();

// Get a list of all the properties in the class
PropertyInfo[] piList = typeof(myclass).GetProperties();

// Loop thru each property and check if it is the
foreach (PropertyIfno pi in piList)
{
   // Get an array of the AddressPropertyAttribute attributes
   AddressPropertyAttribute[] attributes = (AddressPropertyAttribute[])pi.GetCustomAttributes(typeof(AddressPropertyAttribute), true);

   // Go thru the attributes and use them as you wish
}
...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/08/custom-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

