title: C# Buddy Classes / Meta Data and Reflection
body: <p>I am trying to use reflection to check if properties on a given class have a ReadOnly attribute set.  The classes I am using are MVC View Models (using a partial "buddy" class for the metadata.</p>
  
  <pre><code> public partial class AccountViewModel  
  {
      public virtual Int32 ID { get; set; } 
      public virtual decimal Balance { get; set; }    
  
  } 
  [MetadataType(typeof(AccountViewModelMetaData))]
  public partial class AccountViewModel  
  {
      class AccountViewModelMetaData
      {
          [DisplayName("ID")]
          public virtual Int32 ID { get; set; }
  
          [DisplayName("Balance")]
          [DataType(DataType.Currency)] 
          [ReadOnly(true)]
          public virtual decimal Balance { get; set; }
  
      }
  }
  </code></pre>
  
  <p>I want to check if "Balance" has the ReadOnly property.  If i set the ReadOnly attribute on the Balance property of AccountViewModel, i can retrieve it this way:</p>
  
  <pre><code>Type t = typeof(AccountViewModel);
  PropertyInfo pi = t.GetProperty("Balance");  
  bool isReadOnly =  ReadOnlyAttribute.IsDefined(pi,typeof( ReadOnlyAttribute);
  </code></pre>
  
  <p>I can't retrieve the attribute info if it is on the meta data class.  How can i check if the attribute exists?  I have meta data classes defined for all my view models, and need a generic way to check for attributes on meta data classes.</p>
  
  <p>Any suggestions?</p>