<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://davesbox.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>Dave's Box : BCL, .NET 4.0</title><link>http://davesbox.com/archive/tags/BCL/.NET+4.0/default.aspx</link><description>Tags: BCL, .NET 4.0</description><dc:language>en</dc:language><generator>CommunityServer 2008 (Build: 30417.1769)</generator><item><title>Moving a type from one assembly to another using TypeForwardedToAttribute</title><link>http://davesbox.com/archive/2008/12/19/moving-a-type-from-one-assembly-to-another-using-TypeForwardedToAttribute.aspx</link><pubDate>Fri, 19 Dec 2008 15:00:00 GMT</pubDate><guid isPermaLink="false">2122c344-89bc-4cd7-b145-b0515ba3439f:1077</guid><dc:creator>David Kean</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/rsscomments.aspx?PostID=1077</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/commentapi.aspx?PostID=1077</wfw:comment><comments>http://davesbox.com/archive/2008/12/19/moving-a-type-from-one-assembly-to-another-using-TypeForwardedToAttribute.aspx#comments</comments><description>&lt;p&gt;One of the problems that we&amp;rsquo;re facing today in the Framework, mainly stemming from the &lt;a href="http://www.danielmoth.com/Blog/2007/06/net-framework-35.html"&gt;red bits/green bits&lt;/a&gt; design of .NET 3.5, is that certain &amp;lsquo;core&amp;rsquo; types are located in the wrong assemblies.&lt;/p&gt;
&lt;p&gt;For example, the extremely useful &lt;a href="http://msdn.microsoft.com/en-us/library/ms668604.aspx"&gt;ObservableCollection&amp;lt;T&amp;gt;&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms668620.aspx"&gt;ReadOnlyObservableCollection&amp;lt;T&amp;gt;&lt;/a&gt; types both live in WindowsBase.dll, which is a part of the Windows Presentation Foundation. For low-level assemblies (such as my team&amp;rsquo;s very own &lt;a href="http://www.codeplex.com/MEF"&gt;System.ComponentModel.Composition.dll&lt;/a&gt;), this means that to use types such as these, they need to take a reference to assemblies either at a higher level to themselves, or to technologies outside of their own. This is in turn leads to scary dependencies, such as the server assembly System.Web.dll&amp;rsquo;s current direct dependency on the client assembly System.Windows.Forms.dll in .NET 2.0.&lt;/p&gt;
&lt;p&gt;Going forward for .NET 4.0, the Framework Architecture team will be leading an effort (being pushed by my colleague &lt;a href="http://blogs.msdn.com/mirceat/"&gt;Mitch Trofin&lt;/a&gt;) to move these and other core types to lower-level assemblies such as mscorlib.dll and System.dll.&lt;/p&gt;
&lt;p&gt;Now, as &lt;a href="http://davesbox.com/archive/2008/12/09/quot-i-can-t-believe-microsoft-didn-t-make-enter-api-name-here-public-quot.aspx"&gt;I&amp;rsquo;ve mentioned previously&lt;/a&gt;, Microsoft takes compatibility very seriously, so you might be thinking &amp;lsquo;how can they move these types without runtime breaking changes&amp;rsquo;?&lt;/p&gt;
&lt;p&gt;Luckily, there&amp;rsquo;s already a solution for this; &lt;em&gt;type forwarding&lt;/em&gt;. Introduced in .NET 2.0, type forwarding allows assembly writers to move types into other assemblies without needing existing consumers to recompile their applications. This done via the &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.typeforwardedtoattribute.aspx"&gt;TypeFowardedToAttribute&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;To move a type, do the following:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Move the type from the original assembly to the assembly that will become the type&amp;rsquo;s new home.&lt;/li&gt;
&lt;li&gt;In the original assembly, add a reference to the new assembly if it does not already have one.&lt;/li&gt;
&lt;li&gt;Apply the following assembly-level attribute to the original assembly, replacing MyType with the type you moved in step 1:
&lt;pre class="code"&gt;[&lt;span style="color:blue;"&gt;assembly&lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;TypeForwardedToAttribute&lt;/span&gt;(&lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;MyType&lt;/span&gt;))]&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Compile both assemblies and that&amp;rsquo;s it.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;A couple of things to note:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The type must have the same name and namespace in the target assembly. &lt;/li&gt;
&lt;li&gt;This is considered a source breaking change; that is, users recompiling their application against the new versions of the assemblies, will be required to add a reference to the assembly that now contains the type. &lt;/li&gt;
&lt;li&gt;While fully supported by C# and C++/CLI, Visual Basic does not support moving types using the &lt;strong&gt;TypeForwardedToAttribute&lt;/strong&gt;. It does, however, support consuming assemblies written by other languages that have had their types moved. &lt;/li&gt;
&lt;li&gt;Starting in .NET 4.0, a new attribute called &lt;strong&gt;TypeForwardedFromAttribute&lt;/strong&gt; should be applied to types that have been forwarded indicating which assembly they came from. This allows runtime serialization to serialize these types correctly when interoping with previous versions of these assemblies. &lt;/li&gt;
&lt;/ul&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://davesbox.com/aggbug.aspx?PostID=1077" width="1" height="1"&gt;</description><category domain="http://davesbox.com/archive/tags/BCL/default.aspx">BCL</category><category domain="http://davesbox.com/archive/tags/.NET+4.0/default.aspx">.NET 4.0</category><category domain="http://davesbox.com/archive/tags/API+Design/default.aspx">API Design</category><category domain="http://davesbox.com/archive/tags/Compatibility/default.aspx">Compatibility</category></item><item><title>Breaking changes to the String class</title><link>http://davesbox.com/archive/2008/11/12/breaking-changes-to-the-string-class.aspx</link><pubDate>Wed, 12 Nov 2008 15:00:43 GMT</pubDate><guid isPermaLink="false">2122c344-89bc-4cd7-b145-b0515ba3439f:971</guid><dc:creator>David Kean</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/rsscomments.aspx?PostID=971</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://davesbox.com/commentapi.aspx?PostID=971</wfw:comment><comments>http://davesbox.com/archive/2008/11/12/breaking-changes-to-the-string-class.aspx#comments</comments><description>&lt;p&gt;You may have already read Justin Van Patten&amp;#39;s post about the &lt;a href="http://blogs.msdn.com/bclteam/archive/2008/11/04/what-s-new-in-the-bcl-in-net-4-0-justin-van-patten.aspx"&gt;upcoming breaking changes to the String class for .NET 4.0&lt;/a&gt;. This change will affect the behavior of the &lt;strong&gt;String.StartsWith&lt;/strong&gt;, &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;EndsWith&lt;/strong&gt;, &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;IndexOf&lt;/strong&gt; and &lt;strong&gt;String.&lt;/strong&gt;&lt;strong&gt;LastIndexOf&lt;/strong&gt; methods by changing them to perform an ordinal (byte-for-byte) comparison by default instead of a culture-sensitive comparison using &lt;strong&gt;CultureInfo.CurrentCulture. &lt;/strong&gt;In addition, the default overloads of &lt;strong&gt;String.ToUpper,&lt;/strong&gt; &lt;strong&gt;String.ToLower&lt;/strong&gt;,&lt;strong&gt; Char.ToUpper &lt;/strong&gt;and &lt;strong&gt;Char.ToLower&lt;/strong&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;will be changed to use &lt;strong&gt;CultureInfo.InvariantCulture&lt;/strong&gt; instead of &lt;strong&gt;CultureInfo.CurrentCulture&lt;/strong&gt;.&lt;/p&gt; &lt;h4&gt;&lt;strong&gt;How does this change affect me? &lt;/strong&gt;&lt;/h4&gt; &lt;p&gt;If you are using an overload of one of the above methods that does not take a &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt; as a parameter, then your application or library will be affected when you move it to .NET 4.0. &lt;strong&gt;String.Compare&lt;/strong&gt; and &lt;strong&gt;String.CompareTo&lt;/strong&gt; are not being changed.&lt;/p&gt; &lt;p&gt;An example of the kind of behavior change you could expect, is the following:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;static void &lt;/span&gt;Main()
{
    &lt;span style="color:blue;"&gt;int &lt;/span&gt;index = &lt;span style="color:#a31515;"&gt;&amp;quot;encyclopædia&amp;quot;&lt;/span&gt;.IndexOf(&lt;span style="color:#a31515;"&gt;&amp;quot;encyclopaedia&amp;quot;&lt;/span&gt;);

    &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.WriteLine(index);
}&lt;/pre&gt;&lt;/blockquote&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;On versions previous to .NET 4.0, this will output &lt;em&gt;0&lt;/em&gt; when the user&amp;#39;s current locale is set to some cultures, such &lt;em&gt;en-AU, English (Australia)&lt;/em&gt;, and &lt;em&gt;-1&lt;/em&gt; when the user&amp;#39;s current locale is other cultures, such &lt;em&gt;nn-NO, Norwegian, Nynorsk (Norway)&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;However in .NET 4.0, the above comparison will always output &lt;em&gt;-1&lt;/em&gt;, regardless of the user&amp;#39;s current locale.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Why was this change made?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;The comparison methods on the String class have always been a little schizophrenic; some methods, &lt;strong&gt;==&lt;/strong&gt;, &lt;strong&gt;String.Equals&lt;/strong&gt; and &lt;strong&gt;String.Contains &lt;/strong&gt;for example, perform an ordinal comparison by default, whereas the above methods, as well &lt;strong&gt;String.Compare&lt;/strong&gt; and &lt;strong&gt;String&lt;/strong&gt;.&lt;strong&gt;CompareTo,&lt;/strong&gt; perform a culture-sensitive comparison. Because they use the current culture, the default culture-sensitive comparisons can also vary between systems, users and even during the same application session. Which, as pointed out in Justin&amp;#39;s post, can lead to security vulnerabilities in applications that make security decisions using the default overloads. The planned breaking changes bring the majority of these methods inline with each other to have the same default behavior.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;How do I find and fix these comparisons?&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;To start, you should run &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9aeaa970-f281-4fb0-aba1-d59d7ed09772&amp;amp;DisplayLang=en"&gt;FxCop&lt;/a&gt; or &lt;a href="http://msdn.microsoft.com/en-us/library/3z0aeatx.aspx"&gt;Visual Studio Code Analysis&lt;/a&gt; over your code base. The rules &lt;a href="http://msdn.microsoft.com/en-us/library/bb386080.aspx"&gt;Specify StringComparison&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/ms182189.aspx"&gt;Specify CultureInfo&lt;/a&gt; will fire on call sites that do not explicitly specify a &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt;. &lt;a href="http://msdn.microsoft.com/en-us/library/bb385972.aspx"&gt;Specify ordinal StringComparison&lt;/a&gt; will fire when you use invariant culture instead of ordinal to compare, which is almost always wrong.&lt;/p&gt;
&lt;p&gt;To help determine the correct &lt;strong&gt;StringComparison&lt;/strong&gt; or &lt;strong&gt;CultureInfo&lt;/strong&gt; to use, you cannot go past the excellent article &lt;a href="http://msdn.microsoft.com/en-us/library/ms973919.aspx"&gt;New Recommendations for Using Strings in .NET 2.0&lt;/a&gt;. In particular, the section under &lt;strong&gt;Choosing a StringComparison Member for Your Method Call &lt;/strong&gt;provides a table detailing common operations and comparisons to use.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://davesbox.com/aggbug.aspx?PostID=971" width="1" height="1"&gt;</description><category domain="http://davesbox.com/archive/tags/FxCop/default.aspx">FxCop</category><category domain="http://davesbox.com/archive/tags/BCL/default.aspx">BCL</category><category domain="http://davesbox.com/archive/tags/.NET+4.0/default.aspx">.NET 4.0</category><category domain="http://davesbox.com/archive/tags/Compatibility/default.aspx">Compatibility</category></item></channel></rss>