title: generic function with a "has property X" constraint?
body: <p>I have a third-party, closed source application that exports a COM interface, which I am using in my C#.NET application through Interop. This COM interface exports many objects that all show up as System.Object until I cast them to the appropriate interface type. I want to assign an property of all of these objects. Thus:</p>
  
  <pre><code>foreach (object x in BigComInterface.Chickens)
  {
      (x as Chicken).attribute = value;
  }
  foreach (object x in BigComInterface.Ducks)
  {
      (x as Duck).attribute = value;
  }
  </code></pre>
  
  <p>But assigning the property is likely (for application-specific reasons that are unavoidable) to throw Exceptions from which I want to recover, so I really want a try/catch around each one. Thus:</p>
  
  <pre><code>foreach (object x in BigComInterface.Chickens)
  {
      try
      {
          (x as Chicken).attribute = value;
      }
      catch (Exception ex)
      {
          // handle...
      }
  }
  foreach (object x in BigComInterface.Ducks)
  {
      try
      {
          (x as Duck).attribute = value;
      }
      catch (Exception ex)
      {
          // handle...
      }
  }
  </code></pre>
  
  <p>Obviously, it would be so much cleaner to do this:</p>
  
  <pre><code>foreach (object x in BigComInterface.Chickens)
  {
      SetAttribute&lt;Chicken&gt;(x as Chicken, value);
  }
  foreach (object x in BigComInterface.Ducks)
  {
      SetAttribute&lt;Duck&gt;(x as Duck, value);
  }
  
  void SetAttribute&lt;T&gt;(T x, System.Object value)
  {
      try
      {
          x.attribute = value;
      }
      catch
      {
          // handle...
      }
  }
  </code></pre>
  
  <p>See the problem? My <em>x</em> value can be of any type, so the compiler can't resolve <em>.attribute</em>. Chicken and Duck are not in any kind of inheritance tree and they do not share an interface that has <em>.attribute</em>. If they did, I could put a constraint for that interface on <em>T</em>. But since the class is closed-source, that's not possible for me.</p>
  
  <p>What I want, in my fantasy, is something like a constraint requiring the argument to <em>have the .attribute property</em> regardless of whether it implements a given interface. To wit,</p>
  
  <pre><code>void SetAttribute&lt;T&gt;(T x, System.Object value) where T:hasproperty(attribute)
  </code></pre>
  
  <p>I'm not sure what to do from here other than to cut/paste this little try/catch block for each of Chicken, Duck, Cow, Sheep, and so on.</p>
  
  <p>My question is: What is a good workaround for this problem of wanting to invoke a specific property on an object when the interface that implements that property cannot be known at compile time?</p>