How do I put a contract on a generic interface? Say I have the following interface:<br><br><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>interface</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;<br>{<br>    T Parse (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> toParse);<br>}</p></font>How do I specify the attributes for the contract class?<br><br>My initial thought was:<br><br><font size=2> <p>[</font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContractClass</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>IParserContract</font></font><font size=2>&lt;T&gt;))]<br></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>interface</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;<br>{<br>    T Parse (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> toParse);<br>}</p></font>and<br><br><font size=2> <p>[</font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContractClassFor</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;))]<br></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>class</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParserContract</font></font><font size=2>&lt;T&gt;<br>{<br>    ...<br>}</p></font>But that won't work, because you can't use the generic T argument in the attributes.  And leaving the generic argument out won't work, because that doesn't specify the right interface/type.<br><br>So, am I missing the proper way of doing this, or is this a limitation?<br><br>Cheers,<br><br>    Geoff© 2009 Microsoft Corporation. All rights reserved.Mon, 31 Aug 2009 15:04:08 Z0fba6673-0619-41c5-98f4-86160276a521http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#0fba6673-0619-41c5-98f4-86160276a521http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#0fba6673-0619-41c5-98f4-86160276a521OpinionatedGeekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OpinionatedGeekHow do I put a contract on a generic interface? Say I have the following interface:<br><br><font size=2> <p></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>interface</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;<br>{<br>    T Parse (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> toParse);<br>}</p></font>How do I specify the attributes for the contract class?<br><br>My initial thought was:<br><br><font size=2> <p>[</font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContractClass</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>IParserContract</font></font><font size=2>&lt;T&gt;))]<br></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>interface</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;<br>{<br>    T Parse (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>string</font></font><font size=2> toParse);<br>}</p></font>and<br><br><font size=2> <p>[</font><font color="#2b91af" size=2><font color="#2b91af" size=2>ContractClassFor</font></font><font size=2> (</font><font color="#0000ff" size=2><font color="#0000ff" size=2>typeof</font></font><font size=2> (</font><font color="#2b91af" size=2><font color="#2b91af" size=2>IParser</font></font><font size=2>&lt;T&gt;))]<br></font><font color="#0000ff" size=2><font color="#0000ff" size=2>public</font></font> <font color="#0000ff" size=2><font color="#0000ff" size=2>class</font></font> <font color="#2b91af" size=2><font color="#2b91af" size=2>IParserContract</font></font><font size=2>&lt;T&gt;<br>{<br>    ...<br>}</p></font>But that won't work, because you can't use the generic T argument in the attributes.  And leaving the generic argument out won't work, because that doesn't specify the right interface/type.<br><br>So, am I missing the proper way of doing this, or is this a limitation?<br><br>Cheers,<br><br>    GeoffThu, 26 Feb 2009 11:33:12 Z2009-02-26T11:33:12Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#b5b3eb58-f73a-473e-8074-04eb304e581fhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#b5b3eb58-f73a-473e-8074-04eb304e581fManuel Fahndrichhttp://social.msdn.microsoft.com/Profile/en-US/?user=Manuel%20FahndrichHow do I put a contract on a generic interface? Excellent question and we probably should put this into the documentation. For generic interface contracts, the typeof has to refer to the non-instantiated generic type. This is a little known C# feature. It looks as follows:<br><br> <div style="border-bottom:#7f9db9 1px solid;border-left:#7f9db9 1px solid;line-height:100% !important;background-color:white;font-family:Courier New;font-size:11px;overflow:auto;border-top:#7f9db9 1px solid;border-right:#7f9db9 1px solid"> <table style="border-bottom:#eee 0px solid;border-right-width:0px;background-color:#fff;margin:2px 0px;width:99%;border-collapse:collapse;border-top-width:0px;border-left-width:0px" cellspacing=0 cellpadding=0> <colgroup> <col style="border-bottom:#f7f7f7 1px solid;padding-left:10px;font-family:Courier New;white-space:nowrap;font-size:11px"> <tbody> <tr> <td><font style="font-size:11px">[ContractClass (</font><font style="color:blue">typeof</font><font style="font-size:11px"> (IParserContract&lt;&gt;))]  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">interface</font><font style="font-size:11px"> IParser&lt;T&gt;  </font></td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7">    T Parse (</font><font style="color:blue">string</font><font style="font-size:11px"> toParse);  </font></td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td>[ContractClassFor (</font><font style="color:blue">typeof</font><font style="font-size:11px"> (IParser&lt;&gt;))]  </font></td></tr> <tr> <td style="background-color:#f7f7f7"></font><font style="color:blue">public</font><font style="font-size:11px"> </font><font style="color:blue">class</font><font style="font-size:11px"> IParserContract&lt;T&gt;  </font></td></tr> <tr> <td>{  </td></tr> <tr> <td style="background-color:#f7f7f7">    ...  </td></tr> <tr> <td>}  </td></tr> <tr> <td style="background-color:#f7f7f7"> </td></tr> <tr> <td> </td></tr></tbody></table></div><br>Note the empty instantiation brackets IParserContract&lt;&gt; and IParser&lt;&gt;. <br><br>-MaFThu, 26 Feb 2009 15:26:26 Z2009-02-26T15:26:26Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#1fe962b1-6c46-4efd-bd14-f88de956e1b4http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#1fe962b1-6c46-4efd-bd14-f88de956e1b4Keith Farmerhttp://social.msdn.microsoft.com/Profile/en-US/?user=Keith%20FarmerHow do I put a contract on a generic interface?<p> For multi-parameter generics, as you might imagine, you can use IFoo&lt;,,,,&gt; -- just drop the generic identifiers, leaving the commas, and you're good to go.</p> <p>This is also useful in instantiating generic types via reflection.  typeof(List&lt;&gt;) returns a valid type awaiting parameterization.</p><hr class="sig">Keith J. Farmer [Idea Entity]Fri, 27 Feb 2009 00:48:01 Z2009-02-27T00:48:01Zhttp://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#02835b57-eafd-4979-8d23-72a6af320ae4http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/0fba6673-0619-41c5-98f4-86160276a521#02835b57-eafd-4979-8d23-72a6af320ae4OpinionatedGeekhttp://social.msdn.microsoft.com/Profile/en-US/?user=OpinionatedGeekHow do I put a contract on a generic interface?<div class=quote><font class=quoteHeader>Manuel Fahndrich said:</font> <p>For generic interface contracts, the typeof has to refer to the non-instantiated generic type. This is a little known C# feature. <p></p></div> <p>That's excellent - many thanks.  And I thought I knew most of the little-known C# features...!</p> <p>Cheers,</p> <p>Geoff</p>Fri, 27 Feb 2009 10:03:31 Z2009-02-27T10:03:31Z