<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Sam Ng's Blog : CLS compliance</title><link>http://blogs.msdn.com/samng/archive/tags/CLS+compliance/default.aspx</link><description>Tags: CLS compliance</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Generic Method Substitutions and Unification - Part two</title><link>http://blogs.msdn.com/samng/archive/2008/03/04/generic-method-substitutions-and-unification-part-two.aspx</link><pubDate>Tue, 04 Mar 2008 07:24:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8018693</guid><dc:creator>samng</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/samng/comments/8018693.aspx</comments><wfw:commentRss>http://blogs.msdn.com/samng/commentrss.aspx?PostID=8018693</wfw:commentRss><description>&lt;p&gt;Last time we talked about generic method substitutions which resulted in types being declared with more than one method with identical constructed signatures. After thinking long and hard about this problem, we've come to a conclusion as to how to resolve this issue in the next release. &lt;/p&gt;  &lt;p&gt;First, lets recap what we know about the problem.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;We currently produce code which causes the CLR to throw TypeLoadExceptions at runtime. &lt;/li&gt;    &lt;li&gt;Code that falls into this scenario can be easily fixed by a simple rename &lt;/li&gt;    &lt;li&gt;It is a corner case - typical coding practices will not get you into this scenario &lt;/li&gt;    &lt;li&gt;The code written is not CLS-compliant &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The following is a general set of categories that can result in this scenario occurring:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: green"&gt;// Same class
&lt;/span&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Base&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Foo(T t);
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;t);
}
 
&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Base&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; { }
 
&lt;span style="color: green"&gt;// Inherited from base class
&lt;/span&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;A
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;i) { }
}
 
&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;B&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;A
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;void &lt;/span&gt;Foo(T t) { }
}
 
&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;B&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; { }
 
&lt;span style="color: green"&gt;// Outer class
&lt;/span&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;A&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;private void &lt;/span&gt;Foo(T t) { }
    &lt;span style="color: blue"&gt;private void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;i) { }
 
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;A&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; { }
}
 
&lt;span style="color: green"&gt;// Outer class and its base class
&lt;/span&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;A&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;Foo(T t) { }
}
 
&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;B&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;A&lt;/span&gt;&amp;lt;T&amp;gt;
{
    &lt;span style="color: blue"&gt;private void &lt;/span&gt;Foo(&lt;span style="color: blue"&gt;int &lt;/span&gt;i) { }
    &lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;C &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;B&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; { }
}&lt;/pre&gt;

&lt;p&gt;Given the above points, we've decided to take the following action:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;If the user specifies that the type is CLS-compliant (via the CLSCompliantAttribute attribute), the compiler will produce a warning indicating that the constructed type is not CLS-compliant &lt;/li&gt;

  &lt;li&gt;If the user does not specify CLS compliance, or specifies that the type is &lt;em&gt;not&lt;/em&gt; CLS-compliant, then we will keep our current behavior, allow the type to be emitted, and produce an assembly that will throw a runtime exception. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This behavior will make it into the next release of the compiler. Until then, don't write code like this if you want it to work! Stay tuned for my investigations on the C# Compiler and modopts!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8018693" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/samng/archive/tags/Generics/default.aspx">Generics</category><category domain="http://blogs.msdn.com/samng/archive/tags/CLR/default.aspx">CLR</category><category domain="http://blogs.msdn.com/samng/archive/tags/CLS+compliance/default.aspx">CLS compliance</category></item></channel></rss>