<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Lou Haskett</title><subtitle type="html" /><id>http://devlicio.us/blogs/louhaskett/atom.aspx</id><link rel="alternate" type="text/html" href="http://devlicio.us/blogs/louhaskett/default.aspx" /><link rel="self" type="application/atom+xml" href="http://devlicio.us/blogs/louhaskett/atom.aspx" /><generator uri="http://communityserver.org" version="4.1.31106.3070">Community Server</generator><updated>2007-06-13T13:50:00Z</updated><entry><title>Design Considerations: StructureMap Configuration and Plugin Keys</title><link rel="alternate" type="text/html" href="/blogs/louhaskett/archive/2007/06/26/design-considerations-structuremap-configuration-and-plugin-keys.aspx" /><id>/blogs/louhaskett/archive/2007/06/26/design-considerations-structuremap-configuration-and-plugin-keys.aspx</id><published>2007-06-27T01:47:00Z</published><updated>2007-06-27T01:47:00Z</updated><content type="html">I started playing with StructureMap recently. I am struggling with how to design my code's interaction with StructureMap when it comes to the configuration and use of plugin keys. If you have any insights or experience with this, please let me know! 1. Configuration via StructureMap.config Pros : * Separation of Concerns * My pluggable code doesn't have to know that its going to be used with StructureMap. A big pro! * One Stop Shop * One file to look at to figure out how the application is configured...(&lt;a href="http://devlicio.us/blogs/louhaskett/archive/2007/06/26/design-considerations-structuremap-configuration-and-plugin-keys.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://devlicio.us/aggbug.aspx?PostID=30505" width="1" height="1"&gt;</content><author><name>lhaskett</name><uri>http://devlicio.us/members/lhaskett/default.aspx</uri></author><category term="c#" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/c_2300_/default.aspx" /><category term="Dependancy Injection" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/Dependancy+Injection/default.aspx" /><category term="StructureMap" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/StructureMap/default.aspx" /><category term="IoC" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/IoC/default.aspx" /><category term="Design Considerations" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/Design+Considerations/default.aspx" /></entry><entry><title>How-To: "Cast" between List&lt;T&gt;'s</title><link rel="alternate" type="text/html" href="/blogs/louhaskett/archive/2007/06/13/how-to-cast-between-list-t-s.aspx" /><id>/blogs/louhaskett/archive/2007/06/13/how-to-cast-between-list-t-s.aspx</id><published>2007-06-13T18:50:00Z</published><updated>2007-06-13T18:50:00Z</updated><content type="html">&lt;p&gt;&lt;b&gt;Problem&lt;/b&gt;:&lt;br&gt;If you've ever tried to cast been list generics, you know that you can't.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Example&lt;/b&gt;:&lt;br&gt;1. You have two classes Shape and Square, where Square inherits from Shape.&lt;/p&gt;

&lt;p&gt;2. You have two generic lists, List&amp;lt;Shape&amp;gt; and List&amp;lt;Square&amp;gt;.&lt;/p&gt;

&lt;p&gt;3. List&amp;lt;Square&amp;gt; does not inherit from List&amp;lt;Shape&amp;gt;.&amp;nbsp; So you cannot cast List&amp;lt;Square&amp;gt; to a List&amp;lt;Shape&amp;gt;.&amp;nbsp; It seems intuitive that it should be possible, but it isn't.&amp;nbsp; &lt;br&gt;&lt;br&gt;You can find more information &lt;a href="http://64.233.167.104/search?q=cache:0GKl8SNMrxUJ:download.microsoft.com/download/9/8/f/98fdf0c7-2bbd-40d3-9fd1-5a4159fa8044/csharp%25202.0%2520specification_sept_2005.doc%3CA%3E+site:microsoft.com&amp;amp;hl=en&amp;amp;ct=clnk&amp;amp;cd=1&amp;amp;gl=us"&gt;here&lt;/a&gt;, in the "Constructed Types" section, subsection "Conversions", of the C# Version 2.0 Specification Document.&amp;nbsp; The important part is as follows:&lt;/p&gt;

&lt;p&gt;"&lt;font face="Times New Roman" size="3"&gt;No special conversions exist between 
constructed reference types other than those described in §6. In particular, 
unlike array types, constructed reference types do not exhibit “covariant” 
conversions.&amp;nbsp; This means that a type &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;B&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; has no 
conversion (either implicit or explicit) to &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;A&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; even if 
&lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;B&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; is derived from &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;A&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt;.&amp;nbsp; Likewise, no conversion exists from 
&lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;B&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; to &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;object&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt;.&lt;/font&gt;"&amp;nbsp;&lt;/p&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;p&gt;"&lt;font face="Times New Roman" size="3"&gt;The rationale for this is simple: if a 
conversion to &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;A&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; is permitted, 
then apparently one can store values of type &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;A&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; into the list.&amp;nbsp; But this 
would break the invariant that every object in a list of type &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;List&amp;lt;B&amp;gt;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt; is always a value of type &lt;/font&gt;&lt;font face="Lucida Console" size="2"&gt;B&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt;, or else unexpected failures 
may occur when assigning into collection classes.&lt;/font&gt;"&amp;nbsp;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Solution&lt;/b&gt;:&lt;br&gt;Since you cannot cast between them, you end up having to create a new list, code a for loop, and add the items from one list to the other list.&amp;nbsp; Not a big deal, but i got tired of writing the for loop. :)&amp;nbsp; So here is the utility method i created to do it for me:&lt;br&gt;&lt;/p&gt;


&lt;p&gt;
&lt;textarea class="c#:nogutter" name="code" rows="25" cols="150"&gt;/// &amp;lt;summary&amp;gt;
/// This method adds the items in the first list to the second list.
/// This method is  helpful because even when X inherits from Y, List&amp;lt;X&amp;gt; does not inherit from List&amp;lt;Y&amp;gt;, and so you cannot cast between them, only copy.
/// &amp;lt;/summary&amp;gt;
/// &amp;lt;typeparam name="FROM_TYPE"&amp;gt;&amp;lt;/typeparam&amp;gt;
/// &amp;lt;typeparam name="TO_TYPE"&amp;gt;&amp;lt;/typeparam&amp;gt;
/// &amp;lt;param name="listToCopyFrom"&amp;gt;&amp;lt;/param&amp;gt;
/// &amp;lt;param name="listToCopyTo"&amp;gt;&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;
public static List&amp;lt;TO_TYPE&amp;gt; AddRange &amp;lt;FROM_TYPE, TO_TYPE&amp;gt;( List&amp;lt;FROM_TYPE&amp;gt; listToCopyFrom, List&amp;lt;TO_TYPE&amp;gt; listToCopyTo ) where FROM_TYPE : TO_TYPE
{
    // assert
    Assert.IsNotNull(listToCopyFrom, "listToCopyFrom");
    Assert.IsNotNull(listToCopyTo, "listToCopyTo");
    
    // loop through the list to copy, and
    foreach ( FROM_TYPE item in listToCopyFrom )
    {
	// add items to the copy tolist
	listToCopyTo.Add( item );
    }
    
    // return the copy to list
    return listToCopyTo;
}
&lt;/textarea&gt; 
&lt;/p&gt;

&lt;p&gt;Here is a test i wrote for my method:&amp;nbsp;&lt;/p&gt;

&lt;p&gt;
&lt;textarea class="c#:nogutter" name="code" rows="24" cols="130"&gt;/// &amp;lt;summary&amp;gt;
/// This method verifies successfully adding items from one List{} to another.
/// &amp;lt;/summary&amp;gt;
[TestMethod]
public void AddRange_Valid()
{
    // create a test list
    List&amp;lt;TestObjectB&amp;gt; testList = new List&amp;lt;TestObjectB&amp;gt;();
    
    // populate it
    testList.Add( new TestObjectB() );
    testList.Add( new TestObjectB() );
    testList.Add( new TestObjectB() );
    
    // declare a result list
    List&amp;lt;TestObjectA&amp;gt; resultList;
    
    // test it
    resultList = ListUtility.AddRange( testList, new List&amp;lt;TestObjectA&amp;gt;() );
    
    // assert that the items in the test list and the result list are the same
    Assert.AreEqual( testList[0], resultList[0] );
    Assert.AreEqual( testList[1], resultList[1] );
    Assert.AreEqual( testList[2], resultList[2] );
}
&lt;/textarea&gt; 
&lt;/p&gt;

&lt;p&gt;Ok, Ok, I know its basically just a for loop.&amp;nbsp; But I think the use of a generic method is kind of cool, and it makes my code read a lot better when i have to "cast" a whole bunch of List&amp;lt;&amp;gt;'s.&amp;nbsp; If anyone knows of a better way, please let me know!&lt;br&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=28568" width="1" height="1"&gt;</content><author><name>lhaskett</name><uri>http://devlicio.us/members/lhaskett/default.aspx</uri></author><category term="c#" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/c_2300_/default.aspx" /><category term="generics" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/generics/default.aspx" /><category term="how-to" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/how-to/default.aspx" /><category term="casting" scheme="http://devlicio.us/blogs/louhaskett/archive/tags/casting/default.aspx" /></entry></feed>