title: CustomTypeDescriptor with MVC Validation - How to get property value with property.GetValue(component)?
body: <p>I've created custom TypeDescriptionProvider for one of my MVC models. I use it for dynamic assignment of ValidationAttribute. </p>
  
  <p>I use value of one property to decide what attributes to add to other properties. In web service, where I use DataAnnotationsValidationRunner, validation works fine.</p>
  
  <p>Source of runner: <a href="https://github.com/BorisMomtchev/NiCris.Dashboard/blob/master/NiCris.Core/Validation/DataAnnotationsValidationRunner.cs" rel="nofollow">here</a></p>
  
  <pre><code>internal static class DataAnnotationsValidationRunner
  {
      public static IEnumerable&lt;ErrorInfo&gt; GetErrors(object instance)
      {
          return from prop in TypeDescriptor.GetProperties(instance).Cast&lt;PropertyDescriptor&gt;()
                 from attribute in prop.Attributes.OfType&lt;ValidationAttribute&gt;()
                 where !attribute.IsValid(prop.GetValue(instance))
                 select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), instance);
      }
  }
  </code></pre>
  
  <p>To get property value I use following code (in MyCustomTypeDescriptor)</p>
  
  <pre><code>public override PropertyDescriptorCollection GetProperties()
      {
          var originalProperties = base.GetProperties();
          var newProperties = new List&lt;PropertyDescriptor&gt;();
          var myProperty = originalProperties.Find("CountryCodeID", false)
  
          var myId = (int)countryProperty.GetValue(base.GetPropertyOwner(myProperty));
  
          foreach (PropertyDescriptor pd in originalProperties)
          {
              AttributeCollection runtimeAttributes = pd.Attributes;
  
              // add new attributes based on myId value
              ....
          }
  
          return new PropertyDescriptorCollection(newProperties.ToArray());
      }
  </code></pre>
  
  <p>When using this model with this descriptor in MVC View, I get following exception:</p>
  
  <blockquote>
    <p>Value cannot be null. Parameter name: primary Description: An
    unhandled exception occurred during the execution of the current web
    request. Please review the stack trace for more information about the
    error and where it originated in the code.</p>
    
    <p>Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: primary</p>
  </blockquote>
  
  <p>What is the correct way of getting value of property within TypeDescriptor? I use this descriptor through provider on model type, not instance (e.g. global.asax).</p>
  
  <p>EDIT: I've found workaround. In GetTypeDescriptor method of MyTypeDescriptorProvider I use instance parameter and pass it to consctructor of MyCustomTypeDescriptor. However, MVC validation doesn't wok. I though it uses these dynamic data automatically (similar to runner mentioned above). </p>
  
  <p>EDIT 2: Using workaroud I almost always see instance null. So it is not possible to get value there and put it to consctructor of TypeDescriptor...</p>
  
  <p>Thank you!</p>