title: Inferring 2 out of 3 generic types
body: <p>I've built a method that returns me an instance of attribute if it is found on the property:</p>
  
  <pre><code>public static U GetPropertyAttribute&lt;T, TProperty, U&gt;(this T instance, Expression&lt;Func&lt;T, TProperty&gt;&gt; propertySelector, U attribute) where U : Attribute
  {
     return Attribute.GetCustomAttribute(instance.GetType().GetProperty((propertySelector.Body as MemberExpression).Member.Name), typeof(U), true) as U;
  }
  </code></pre>
  
  <p>In order to get instance, I have to invoke:</p>
  
  <pre><code>var cc = new CustomClass();
  cc.GetPropertyAttribute(x =&gt; x.Name, new NullableAttribute())
  </code></pre>
  
  <p>And it works fine, I get exact instance of the attribute class.</p>
  
  <p>However, I don't like that I have to use new NullableAttribute() as parameter, I'd like to have invoke look like:</p>
  
  <pre><code>cc.GetPropertyAttribute&lt;NullableAttribute&gt;(x =&gt; x.Name)
  </code></pre>
  
  <p>This however does not work due to the fact that as soon as I remove second parameter and add 1 generic type in method name, it starts to require other two as well. Is there a way to force the method to infer 2 out of 3 generic parameters? I.e. I want to specify attribute class, but don't want to specify class name and property name.</p>
  
  <p>UPDATE:</p>
  
  <p>Here's the implemented code, thanks to Jon, as well as the code for string solution. I've decided to nest the class so that I don't polute the namespace if I introduce same approach for some other extension classes.</p>
  
  <pre><code>public static class AttributeExtensions
  {
      public static ObjectProperty&lt;T, TProperty&gt; From&lt;T, TProperty&gt;(this T instance, Expression&lt;Func&lt;T, TProperty&gt;&gt; propertySelector)
      {
          return new ObjectProperty&lt;T, TProperty&gt;(instance, propertySelector);
      }
  
      public class ObjectProperty&lt;T, TProperty&gt;
      {
          private readonly T instance;
          private readonly Expression&lt;Func&lt;T, TProperty&gt;&gt; propertySelector;
  
          public ObjectProperty(T instance, Expression&lt;Func&lt;T, TProperty&gt;&gt; propertySelector)
          {
              this.instance = instance;
              this.propertySelector = propertySelector;
          }
  
          public U GetAttribute&lt;U&gt;() where U : Attribute
          {
              return Attribute.GetCustomAttribute(instance.GetType().GetProperty((propertySelector.Body as MemberExpression).Member.Name), typeof(U), true) as U;
          }
      }
  
      public static T GetPropertyAttribute&lt;T&gt;(this object instance, string propertyName) where T : Attribute
      {
          return Attribute.GetCustomAttribute(instance.GetType().GetProperty(propertyName), typeof(T), true) as T;
      }
  }
  </code></pre>
  
  <p>So invoke now goes like this:</p>
  
  <pre><code>var cc = new CustomClass();
  var attr = cc.From(x =&gt; x.Name).GetAttribute&lt;NullableAttribute&gt;();
  </code></pre>