﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Generics</title>
	<atom:link href="http://byatool.com/tag/generics/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>F#: Duck typing with generic constraints and why you will be speechless</title>
		<link>http://byatool.com/f/f-duck-typing-with-generic-constraints-and-why-you-will-be-speechless/</link>
		<comments>http://byatool.com/f/f-duck-typing-with-generic-constraints-and-why-you-will-be-speechless/#comments</comments>
		<pubDate>Fri, 11 Nov 2011 17:01:01 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2359</guid>
		<description><![CDATA[So I&#8217;ve been working on a validation design that basically allows validation methods to be added to a list and then run. A small part of it: member x.Validate(valueToCheck:'a) = methodList &#124;&#62; Seq.map (fun (methodToRun) -&#62; methodToRun(valueToCheck)) &#124;&#62; Seq.reduce (fun(outer) inner -&#62; outer.MergeResults(inner)) What this is saying is take a list of methods, run them [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been working on a validation design that basically allows validation methods to be added to a list and then run. A small part of it:</p>
<pre>    <span style="color: #0000ff;">member</span> x.Validate(valueToCheck:'a) =
      methodList
      |&gt; <span style="color: #339966;">Seq</span>.map (<span style="color: #0000ff;">fun</span> (methodToRun) -&gt; methodToRun(valueToCheck))
      |&gt; <span style="color: #339966;">Seq</span>.reduce (<span style="color: #0000ff;">fun</span>(outer) inner -&gt; outer.MergeResults(inner))</pre>
<p>What this is saying is take a list of methods, run them and make a list of their results, then merge the results into one. While I won&#8217;t get into what the results are (And I almost typed that as resluts which makes me think there is a refurbishing factory for sluts.) just know that each result can merge with another and combine values.</p>
<p>The biggest problem I was running into was a way to allow any method to be added and let the method decide what it needs to run correctly. In C# this can be done easily with dynamic:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> MethodResult ValidatePasswordExistence(dynamic modelToCheck)
  {
    <span style="color: #0000ff;">var</span> returnResult = <span style="color: #0000ff;">new</span> <span style="color: #339966;">MethodResult</span>();
    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">string</span>.IsNullOrEmpty(modelToCheck.Password))
    {
      returnResult = returnResult.AddErrorMessage(<span style="color: #339966;">UserError</span>.PasswordIsNullOrEmpty);
    }

    <span style="color: #0000ff;">return</span> returnResult;
  }</pre>
<p>The use of a dynamic parameter means that all validation methods can have the same signature which makes it really easy to send the same class into any method in the list. The problem with F# is there is no ready way to use dynamic and I&#8217;m not sure it should be. F# is heavily based on typing and dynamic kind of goes in the opposite direction. So I thought my ship was sunk, that is if I had a ship to sink and said ship hand&#8217;t sunk already.</p>
<p>Thanks to some help from <a href="http://stackoverflow.com/questions/8095512/f-member-constraints-to-help-create-seemingly-dynamic-types">the O</a>, i was able to get past this not only without dynamic, but a way to strongly type any object coming into the method:</p>
<pre>  <span style="color: #0000ff;">let</span> <span style="color: #0000ff;">inline</span> UserNameExists (userModel) =
    <span style="color: #0000ff;">let</span> userName = (^a : (<span style="color: #0000ff;">member</span> UserName : <span style="color: #0000ff;">String</span> <span style="color: #0000ff;">with</span> <span style="color: #0000ff;">get</span>) (userModel))

    <span style="color: #0000ff;">let</span> result =
      <span style="color: #0000ff;">match</span> userName <span style="color: #0000ff;">with</span>
        | <span style="color: #0000ff;">null</span> -&gt; (<span style="color: #0000ff;">new</span> MethodResult()).AddErrorMessage(UserErrors.UserNameIsNullOrEmpty)
        | "" -&gt; (<span style="color: #0000ff;">new</span> MethodResult()).AddErrorMessage(UserErrors.UserNameIsNullOrEmpty)
        | _ -&gt; <span style="color: #0000ff;">new</span> MethodResult()

    result</pre>
<p>The important part is the second line. This line basically says that anything coming into the method as userModel has to have a property of UserName. One of the dangerous issues with dynamic is there are no compile time checks to make sure that any object going in will have the property or method you need it to. This isn&#8217;t true of F#. You not only get to pass in any object you want (That has what is required by the check) but you get complile time errros if you try to pass in an object that doesn&#8217;t fit the requirements.</p>
<p>Yup, that&#8217;s right. You&#8217;re speechless.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/f/f-duck-typing-with-generic-constraints-and-why-you-will-be-speechless/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Duck Typing my way to a Universal String Convert</title>
		<link>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/</link>
		<comments>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/#comments</comments>
		<pubDate>Mon, 17 Nov 2008 14:48:15 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=94</guid>
		<description><![CDATA[So being the beacon of ignorance in the fog of brilliance, I had no idea what Duck Typing was. In short it&#8217;s the idea of assuming a class&#8217;s type based on the methods it holds. Say you have 5 different classes, none of which share an inheritance tree. Now let&#8217;s say you have a method [...]]]></description>
			<content:encoded><![CDATA[<p>So being the beacon of ignorance in the fog of brilliance, I had no idea what Duck Typing was.  In short it&#8217;s the idea of assuming a class&#8217;s type based on the methods it holds. </p>
<p>Say you have 5 different classes, none of which share an inheritance tree.  Now let&#8217;s say you have a method that takes in any object and uses the SeanIsAwsome method on the object.  You could make sure that every object sent in has that method by using an interface that all the objects sent in share.</p>
<pre>    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> SomeMethod(<span style="color: #33cccc;">ISeanRules</span> inParameter)</pre>
<p>What if you want to send in a bunch of objects that don&#8217;t share the same interface/base class? Well you base it on what methods the class holds. If the class has the SeanIsAwsome method, then you call it. If not, well you could throw an exception, do nothing, go jogging, eat bacon, ect. That part is up to you.</p>
<p>The problem was that I wanted to create a universal convert that would take in a string and convert it to the 8 billion (ballpark figure) value types in C#. Now what I wanted to do is use the famous TryParse method so my first hope was that TryParse was a method on an interface they all shared. Yeah no. So next thought was to use the Duck Typing principle and say, &#8220;Hey jackass, you have a TryParse method? Yeah? Good. Use it.&#8221; &#8216;Course I had to find a way to do that. Turns out it wasn&#8217;t too bad.</p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">class</span> ConvertFromString
{
  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> T? ConvertTo&lt;T&gt;(<span style="color: #0000ff;">this</span> <span style="color: #33cccc;">String</span> numberToConvert) <span style="color: #0000ff;">where</span> T : <span style="color: #0000ff;">struct</span>
  {
    T? returnValue = null;

    <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(<span style="color: #0000ff;">typeof</span>(T));
    if (neededInfo != null &amp;&amp; !numberToConvert.IsNullOrEmpty())
    {
      T output = <span style="color: #0000ff;">default</span>(T);
      <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
      returnValue = <span style="color: #0000ff;">new</span> T();

      <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue.Value, paramsArray);

      <span style="color: #0000ff;">if</span> (returnedValue <span style="color: #0000ff;">is</span> <span style="color: #33cccc;">Boolean</span> &amp;&amp; (<span style="color: #33cccc;">Boolean</span>)returnedValue)
      {
        returnValue = (T)paramsArray[1];
      }
      <span style="color: #0000ff;">else</span>
      {
        returnValue = <span style="color: #0000ff;">null</span>;
      }
    }

    <span style="color: #0000ff;">return</span> returnValue;
  }
}</pre>
<p>A whaaaa?</p>
<p>Ok this might look odd, but it&#8217;s really simple. If it doesn&#8217;t look odd then chances are you&#8217;re smarter than me and you shouldn&#8217;t be here anyhow. First part is simple:</p>
<pre>  <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> T? ConvertTo&lt;T&gt;(<span style="color: #0000ff;">this</span> <span style="color: #33cccc;">String</span> numberToConvert) <span style="color: #0000ff;">where</span> T : <span style="color: #0000ff;">struct</span></pre>
<p>Due to this being a extension method (Opps forgot to tell you that part) I have to declare this static method in a static class. Basically I am taking in a string and returning a nullable version of whatever type I specific in the call.</p>
<pre>    T? returnValue = <span style="color: #0000ff;">null</span>;

    <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(<span style="color: #0000ff;">typeof</span>(T));</pre>
<p>Remember that MethodInfo method I had explained in the last post? The next part you might remember too, but I&#8217;m not betting on it.</p>
<pre>    <span style="color: #0000ff;">if</span> (neededInfo != <span style="color: #0000ff;">null</span> &amp;&amp; !numberToConvert.IsNullOrEmpty())
    {
      T output = <span style="color: #0000ff;">default</span>(T);
      <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
      returnValue = <span style="color: #0000ff;">new</span> T();

      <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue.Value, paramsArray);</pre>
<p>Ok so I have the method info, now I have to create the list of parameters to send in with the invoke. Pretty straight forward. If things went well, returnedValue should be a boolean. However, just incase:</p>
<pre>    <span style="color: #0000ff;">if</span> (returnedValue is <span style="color: #33cccc;">Boolean</span> &amp;&amp; (<span style="color: #33cccc;">Boolean</span>)returnedValue)
    {
      returnValue = (T)paramsArray[1];
    }
    <span style="color: #0000ff;">else</span>
    {
      returnValue = <span style="color: #0000ff;">null</span>;
    }</pre>
<p>So if the returnValue is true and boolean, then the tryParse was successful. With that in mind, I still have to get the converted value. If it had not been successful than I am just going to return null since this is just meant for converting and not for whether or not it could be. It is just assumed that if it&#8217;s null, then it could not be converted at this time.</p>
<p>And now for the use:</p>
<pre>  <span style="color: #0000ff;">decimal</span>? converted = someString.ConvertTo&lt;<span style="color: #0000ff;">decimal</span>&gt;()</pre>
<p>An boom, you have a universal convert. YAY!</p>
<pre>  <span style="color: #0000ff;">using</span> System;
  <span style="color: #0000ff;">using</span> System.Reflection;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cannot Resolve Method, Can&#8217;t Infer Return Type, and Funcs</title>
		<link>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/</link>
		<comments>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 20:07:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[.Net Issues]]></category>
		<category><![CDATA[Anonymous Methods]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=79</guid>
		<description><![CDATA[So ran into this today and the answer was actually a lot easier to understand than I thought it would be. Say you want to order a list of objects by a number. Seems simple. Now if you have been paying attention you would know I like using Funcs. Func&#60;SomeClass, Int32&#62; orderByNumber = currentClass =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>So ran into this today and the answer was actually a lot easier to understand than I thought it would be.</p>
<p>Say you want to order a list of objects by a number. Seems simple. Now if you have been paying attention you would know I like using Funcs.</p>
<pre>  <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">SomeClass</span>, <span style="color: #33cccc;">Int32</span>&gt; orderByNumber =
    currentClass =&gt;  currentClass.SomeNumber;

  anotherCollection = someCollection.OrderBy(orderByNumber);</pre>
<p>Seems simple, but what if you wanted to use a method already defined in the class?</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #33cccc;">Int32</span> ReturnNumber(<span style="color: #33cccc;">SomeClass</span> currentClass)
  {
    <span style="color: #0000ff;">return</span> currentClass.SomeNumber;
  }</pre>
<p>It seems like this could be the way to go, right?</p>
<pre>  someCollection.OrderBy(ReturnNumber);</pre>
<p>Compile and BOOOOOOM you get an error. It says it can&#8217;t infer the return type of the method. Wait what? It&#8217;s pretty obvious right, it&#8217;s an integer. It had no problem inferring from the <span style="color: #33cccc;">Func</span> and you have to figure that the method itself is &#8220;typed&#8221; also. Well here&#8217;s the problem (And you&#8217;re dumb for not knowing this, but I&#8217;m not because I&#8217;m immune to dumb), ReturnNumber isn&#8217;t a method, it&#8217;s part of a method group. You can have a million (well maybe not that many) methods named ReturnNumber, all with different parameters. Why is this a problem? Well let&#8217;s use lambda expressions:</p>
<pre>  someCollection.OrderBy(currentClass =&gt;  currentClass.SomeNumber);</pre>
<p>At this point it knows two things: currentClass is a <span style="color: #33cccc;">SomeClass</span> and there is a Method that takes in a <span style="color: #33cccc;">SomeClass</span> and returns something. So with that in mind, it looks for such a method and finds the return type. This is no different with the <span style="color: #33cccc;">Func</span> since the <span style="color: #33cccc;">Func</span> is basically unique due to it being a named field. After all you can&#8217;t have two fields named orderByNumber, but you can have many methods named ReturnNumber. That is where the problem is. When you use the second example:</p>
<pre>  someCollection.OrderBy(ReturnNumber);</pre>
<p>It can infer the <span style="color: #33cccc;">SomeClass</span> from the list and it sees the method. For there it has to find the method&#8217;s return type. Wait, which method? If i Have 10 overloads, each with different return types, how does it know what type to use?  Well the answers is, it doesn&#8217;t.  So basically you&#8217;re screwed.  Sucks, huh?</p>
<p>Side note: This works</p>
<pre>  <span style="color: #33cccc;">Func</span>&lt;<span style="color: #33cccc;">SomeClass</span>, <span style="color: #33cccc;">Int32</span>&gt; orderByNumber = ReturnNumber;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/net-issues/funcs-type-inference-and-linq-and-bears-oh-my/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yeah SessionTryParse</title>
		<link>http://byatool.com/utilities/yeah-sessiontryparse/</link>
		<comments>http://byatool.com/utilities/yeah-sessiontryparse/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 17:06:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Generics]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[TryParse]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/24/yeah-sessiontryparse/</guid>
		<description><![CDATA[So I got tired of seeing If Session["SomeKey"] != null&#8230; blah blah blah and thought it would be a semi worthwhile task to create a TryParse method because I&#8217;m bad like that. Not a Bad Enough Dude to Save the President, but bad. public static Boolean SessionTryParse&#60;K&#38;gt;(HttpSessionState session, String key, out K itemToSet) where K [...]]]></description>
			<content:encoded><![CDATA[<p>So I got tired of seeing If Session["SomeKey"] != null&#8230; blah blah blah and thought it would be a semi worthwhile task to create a TryParse method because I&#8217;m bad like that. Not a Bad Enough Dude to Save the President, but bad.</p>
<pre> <span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">Boolean</span> SessionTryParse&lt;K&amp;gt;<span style="color: #00cccc;">(HttpSessionState </span>session, <span style="color: #00cccc;">String </span>key, <span style="color: #3333ff;">out </span>K itemToSet) <span style="color: #3333ff;">where </span>K : <span style="color: #3333ff;">class</span>
 {
     itemToSet = <span style="color: #3333ff;">null</span>;

     <span style="color: #3333ff;">if</span> (session[key] != <span style="color: #3333ff;">null</span>)
     {
        itemToSet = session[key] <span style="color: #3333ff;">as</span> K;
     }

     <span style="color: #3333ff;">return</span> itemToSet != <span style="color: #3333ff;">null</span>;
 }</pre>
<p>And for structures&#8230; which I have to cheat and only allow them to be nullable.</p>
<pre><span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">Boolean</span> SessionTryParse&lt;K&gt;(<span style="color: #00cccc;">HttpSessionState</span> session, <span style="color: #00cccc;">String </span>key, <span style="color: #3333ff;">out </span>K? itemToSet) <span style="color: #3333ff;">where </span>K : <span style="color: #3333ff;">struct</span>
{
     itemToSet = <span style="color: #3333ff;">null</span>;

     <span style="color: #3333ff;">if </span>(session[key] != <span style="color: #3333ff;">null </span>&amp;&amp; session[key] <span style="color: #3333ff;">is</span> K)
     {
        itemToSet = (K)session[key];
     }

     <span style="color: #3333ff;">return</span> itemToSet != <span style="color: #3333ff;">null</span>;
}</pre>
<p>The idea is simple, pull in the Session, the needed Session key, and something to throw the value in. After that, just check to see if the Session + Key is null and if Session + Key is the same type of said something. That&#8217;s how it&#8217;s done Detroit greater metropolitan area style&#8230; punk.</p>
<p>USE THIS:</p>
<pre>   <span style="color: #3333ff;">using</span> System;
   <span style="color: #3333ff;">using</span> System.Web.SessionState;</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/utilities/yeah-sessiontryparse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mulitple Constraint Generics and Test Base Classes</title>
		<link>http://byatool.com/test/mulitple-constraint-generics-and-test-base-classes/</link>
		<comments>http://byatool.com/test/mulitple-constraint-generics-and-test-base-classes/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 12:23:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Test]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/01/mulitple-constraint-generics-and-test-base-classes/</guid>
		<description><![CDATA[This was basically an idea I had to have test classes inherit a TestBase that has a Static Create method on it. The reason for this is that I have found it easier to have a Create method that takes care of creating a temporary class of the type the test represents. Say I have [...]]]></description>
			<content:encoded><![CDATA[<p>This was basically an idea I had to have test classes inherit a TestBase that has a Static Create method on it. The reason for this is that I have found it easier to have a Create method that takes care of creating a temporary class of the type the test represents. Say I have a UserTest class and I need an address. Instead of creating and filling a whole address object in the UserTest, it easier to have a Create Method on the AddressTest class that gives me a premade Address object. Why would I want to redo a lot of the same code by having the create method on every class when I can have it on a base class and just have the non base classes specify how to create the object?</p>
<p>Is this really needed? I can&#8217;t argue absolutely, but this forces a Create method to appear on any test class that inherits from TestBase and by use of Exceptions, forces the child classes to define how to create the object they represent. (As in it forces AddressTest to define how to create an Address object.) This also allows the Create method to be static and inherited. You could make it abstract, to skip the need for the two create methods(Create and CreateInstance), but that would hose the whole situation since static abstract is a no no. Static is a must for this situation.</p>
<p>The idea behind the code is to have a static Create method on the TestBase class that when called will create the test class type passed in through generics.</p>
<pre><span style="color: #3333ff;">public static</span> I Create()
 {
     I returnValue;
     J testToUse;

     testToUse = System.<span style="color: #00cccc;">Activator</span>.CreateInstance&lt;J&gt;();
     returnValue = testToUse.CreateInstance();

     <span style="color: #3333ff;">return</span> returnValue;
 }</pre>
<p>Where J is the test class type to create. Once that test class is instantiated, the virtual method CreateInstance is called. This is where the test class creates the instance of the object it represents.</p>
<pre><span style="color: #3333ff;">protected override</span> I CreateInstance()
{
     I returnValue;

     returnValue = (I)<span style="color: #3333ff;">new</span> <span style="color: #00cccc;">SimpleItem</span>();
     returnValue.IsSimple = <span style="color: #3333ff;">true</span>;
     <span style="color: #3333ff;">return</span> returnValue;
}</pre>
<p>Where I is the type of object it is going to create. In use it will look something like this:</p>
<pre>  <span style="color: #00cccc;">ItemBase</span> itemToCreate;

  itemToCreate = <span style="color: #00cccc;">SimpleItemTest</span>&lt;<span style="color: #00cccc;">SimpleItem</span>&gt;.Create();</pre>
<p>Now for the code:</p>
<pre><span style="color: #009900;">//Some junk base items to demostrate the creating of an item with a base class</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">ItemBase </span>
{
}

<span style="color: #009900;">//One item that will be created</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">SimpleItem </span>: <span style="color: #00cccc;">ItemBase</span>
{
 <span style="color: #3333ff;">public </span><span style="color: #00cccc;">Boolean </span>IsSimple { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
}

<span style="color: #009900;">//The other item that can be created.</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">ComplexItem </span>: <span style="color: #00cccc;">ItemBase</span>
{
 <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">Boolean </span>IsComplex { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
 <span style="color: #3333ff;">public</span> <span style="color: #00cccc;">Boolean </span>IsNotSimple { <span style="color: #3333ff;">get</span>; <span style="color: #3333ff;">set</span>; }
}

<span style="color: #009900;">//This test will be used to create a SimpleItem</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">SimpleItemTest</span>&lt;I&gt;<span style="color: #00cccc;"> </span>: <span style="color: #00cccc;">TestBase</span>&lt;I, <span style="color: #00cccc;">SimpleItemTest</span>&lt;I&gt;&gt; <span style="color: #3333ff;">where</span> I : <span style="color: #00cccc;">SimpleItem</span>
{
 <span style="color: #3333ff;">protected override</span> I CreateInstance()
 {
     I returnValue;

     returnValue = (I)<span style="color: #3333ff;">new</span> <span style="color: #00cccc;">SimpleItem</span>();
     returnValue.IsSimple = <span style="color: #3333ff;">true</span>;
     <span style="color: #3333ff;">return</span> returnValue;
 }
}

<span style="color: #009900;">//This test will be used to create a ComplexItem</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">ComplexItemTest</span>&lt;I&gt; : <span style="color: #00cccc;">TestBase</span>&lt;I, <span style="color: #00cccc;">ComplexItemTest</span>&lt;I&gt;&gt; <span style="color: #3333ff;">where</span> I : <span style="color: #00cccc;">ComplexItem</span>
{
 <span style="color: #3333ff;">protected override</span> I CreateInstance()
 {
     I returnValue;

     returnValue = (I)<span style="color: #3333ff;">new</span> ComplexItem();
     returnValue.IsComplex = <span style="color: #3333ff;">true</span>;
     returnValue.IsNotSimple = <span style="color: #3333ff;">true</span>;
     <span style="color: #00cccc;">return</span> returnValue;
 }
}

<span style="color: #009900;">//This is the base class where the Create Method resides</span>
<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">TestBase</span>&lt;I, j&gt; <span style="color: #3333ff;">where</span> I : <span style="color: #3333ff;">class</span> <span style="color: #3333ff;">where</span> J : <span style="color: #00cccc;">TestBase</span>&lt;I, j&gt;
{
 <span style="color: #009900;">//This will either be overridden or throw an error.  Kind of a forced</span>
<span style="color: #009900;"> //abstract.  This is the method that the child test classes will use to</span>
<span style="color: #009900;"> //create whatever class they represent.</span>
 <span style="color: #3333ff;">protected</span> <span style="color: #3333ff;">virtual</span> I CreateInstance()
 {
     <span style="color: #3333ff;">throw</span> <span style="color: #3333ff;">new</span> System.<span style="color: #00cccc;">NotImplementedException</span>();
 }

 <span style="color: #009900;">//This is used to create a test class and call the CreateInstance method</span>
<span style="color: #009900;"> //so that this method can return the correct object.  The object type is</span>
<span style="color: #009900;"> //dependent on the type of test class that is being created.</span>
 <span style="color: #3333ff;">public static</span> I Create()
 {
     I returnValue;
     J testToUse;

     testToUse = System.<span style="color: #00cccc;">Activator</span>.CreateInstance&lt;J&gt;();
     returnValue = testToUse.CreateInstance();

     <span style="color: #3333ff;">return</span> returnValue;
 }
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/test/mulitple-constraint-generics-and-test-base-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

