title: Dynamically get class attribute value from type
body: <p>I'm trying to write a method that finds all types in an assembly with a specific custom attribute. I also need to be able to supply a string value to match on. The caveat is that I would like to be able to run this on any class and return any value.</p>
  
  <p>For example:
  I would like to execute a call like this</p>
  
  <pre><code>Type tTest = TypeFinder.GetTypesWithAttributeValue(Assembly.Load("MyAssembly"), typeof(DiagnosticTestAttribute), "TestName", "EmailTest");
  </code></pre>
  
  <p>My method so far looks like this:</p>
  
  <pre><code>public static Type GetTypesWithAttributeValue(Assembly aAssembly, Type tAttribute, string sPropertyName, object oValue)
  {
      object oReturn = null;
      foreach (Type type in aAssembly.GetTypes())
      {
          foreach (object oTemp in type.GetCustomAttributes(tAttribute, true))
          {
              //if the attribute we are looking for matches
              //the value we are looking for, return the current type.
          }
      }
      return typeof(string); //otherwise return a string type
  }
  </code></pre>
  
  <p>My Attribute looks like this:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
  public class DiagnosticTestAttribute : Attribute
  {
      private string _sTestName = string.Empty;
  
      public string TestName
      {
          get { return _sTestName; }
      }
      public DiagnosticTest(string sTestName)
      {
          _sTestName = sTestName;
      }
  }
  </code></pre>
  
  <p>For those familiar with expressions, I would really like to be able to make a call like:</p>
  
  <pre><code>TypeFinder.GetTypesWithAttributeValue&lt;DiagnosticTestAttribute&gt;(Assembly.Load("MyAssembly"), x=&gt; x.TestName, "EmailTest");
  </code></pre>
  
  <p>Where the expression uses my generic type to select the property that I'm looking for. </p>