﻿<?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; Reflection</title>
	<atom:link href="http://byatool.com/tag/reflection/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>Voted best site in existence by a top fictious rating site!</description>
	<lastBuildDate>Fri, 10 Sep 2010 18:53:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<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>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/general-coding/getting-back-out-parameters-using-getmethod/" title="Return Out Variables Using GetMethod">Return Out Variables Using GetMethod</a></li><li><a href="http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/" title="ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View">ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View</a></li><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li></ul>]]></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>Return Out Variables Using GetMethod</title>
		<link>http://byatool.com/general-coding/getting-back-out-parameters-using-getmethod/</link>
		<comments>http://byatool.com/general-coding/getting-back-out-parameters-using-getmethod/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 22:01:39 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Reflection]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=89</guid>
		<description><![CDATA[So I ran into a problem today. I wanted to call a method (TryParse) off a type using relfection, but the catch is that there is an out parameter. At first I though, &#8220;Hey, I just give it the type and it will find it. Not so lucky. See when you call GetMethod you have [...]]]></description>
			<content:encoded><![CDATA[<p>So I ran into a problem today. I wanted to call a method (TryParse) off a type using relfection, but the catch is that there is an out parameter. At first I though, &#8220;Hey, I just give it the type and it will find it. Not so lucky. See when you call GetMethod you have to supply a list of parameters. why? because if you don&#8217;t, the name alone could return any number of methods named the same thing. By using a parameter list, it in essence narrows it down to one. Say you look at decimal.TryParse which expects a string and an out parameter of type decimal. Would think it is:</p>
<pre>  <span style="color: #33cccc;">Type</span>[] paramTypes = new Type[2] { typeof(<span style="color: #0000ff;">string</span>), typeof(<span style="color: #0000ff;">decimal</span>) };
  returnValue = typeToCheck.GetMethod(<span style="color: #800000;">"TryParse"</span>, paramTypes);</pre>
<p>No dice. Looks ok until you actually look at the type of the second parameter. It&#8217;s actually &#8220;decimal&amp;&#8221;, meaning there is a reference attached to it. This makes sense since Out will switch the pointer of the old parameter sent in to whatever you create in the method. Problem is when you trying to find the method you are looking for &#8220;decimal&#8221; != &#8220;decimal&amp;&#8221;. Now this looks like a big problem, but it&#8217;s actually easy to solve.</p>
<pre>  <span style="color: #33cccc;">Type</span>[] paramTypes = <span style="color: #0000ff;">new</span> Type[2] { typeof(<span style="color: #0000ff;">string</span>), typeof(<span style="color: #0000ff;">decimal</span>).MakeByRefType() };</pre>
<p>See the difference? Turns out Microsoft already thought this one through. The MakeByRefType method allows you to simulate an out parameter. Here&#8217;s the method in full to get the MethodInfo:</p>
<pre>  <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> MethodInfo GetCorrectMethodInfo(Type typeToCheck)
  {
    <span style="color: #008000;">//This line may not mean much but with reflection, it's usually a good idea to store
<span style="color: #000000;">    </span>//things like method info or property info in a cache somewhere so that you don't have
<span style="color: #000000;">    </span>//have to use reflection every time to get what you need.  That's what this is doing.
<span style="color: #000000;">    </span>//Basically I am using the passed in type name as the key and the value is the methodInfo
<span style="color: #000000;">    </span>//for that type.
</span>    <span style="color: #33cccc;">MethodInfo</span> returnValue = someCache.Get(typeToCheck.FullName);

    <span style="color: #008000;">//Ok, now for the reflection part.</span>
    <span style="color: #0000ff;">if</span>(returnValue == <span style="color: #0000ff;">null</span>)
    {
      <span style="color: #33cccc;">Type</span>[] paramTypes = <span style="color: #0000ff;">new</span> <span style="color: #33cccc;">Type</span>[2] { typeof(<span style="color: #0000ff;">string</span>), typeToCheck.MakeByRefType() };
      returnValue = typeToCheck.GetMethod(<span style="color: #800000;">"TryParse"</span>, paramTypes);
      <span style="color: #0000ff;">if</span> (returnValue != <span style="color: #0000ff;">null</span>)
      {
        someCache.Add(typeToCheck.FullName, returnValue);
      }
    }

    <span style="color: #0000ff;">return</span> returnValue;
  }</pre>
<p>Now we&#8217;re not done yet, are we? No. That just gets the methodInfo needed. Now how do I call it and get a value back? Well that&#8217;s pretty easy too:</p>
<pre>  <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(typeof(decimal));
  <span style="color: #0000ff;">decimal</span> output = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">decimal</span>();
  <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
  <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue.Value, paramsArray);

  <span style="color: #0000ff;">if</span> (returnedValue is <span style="color: #0000ff;">bool</span> &amp;&amp; (<span style="color: #0000ff;">bool</span>)returnedValue)
  {
      returnValue = (<span style="color: #0000ff;">decimal</span>)paramsArray[1];
  }
  <span style="color: #0000ff;">else</span>
  {
    returnValue = <span style="color: #0000ff;">null</span>;
  }</pre>
<p>So first part is actually calling the method we now have:</p>
<pre>  <span style="color: #33cccc;">MethodInfo</span> neededInfo = GetCorrectMethodInfo(typeof(<span style="color: #0000ff;">decimal</span>));
  <span style="color: #0000ff;">decimal</span> output = <span style="color: #0000ff;">new</span> decimal();
  <span style="color: #0000ff;">object</span>[] paramsArray = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[2] { numberToConvert, output };
  <span style="color: #0000ff;">object</span> returnedValue = neededInfo.Invoke(returnValue, paramsArray);</pre>
<p>So basically you take the variables, put them into an array, and then pass that array trough the invoke method. Nice thing is when the two variables are sent into the method, you don&#8217;t have to flag output as an Out parameter.</p>
<p>Now for the FINALLY ITS OVER</p>
<pre>  <span style="color: #0000ff;">if</span> (returnedValue <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">bool</span> &amp;&amp; (<span style="color: #0000ff;">bool</span>)returnedValue)
  {
      returnValue = (<span style="color: #0000ff;">decimal</span>)paramsArray[1];
  }
  <span style="color: #0000ff;">else</span>
  {
    returnValue = null;
  }</pre>
<p>You would think that the &#8220;output&#8221; variable you sent through .Invoke would hold the needed value. Too bad. You actually have to look at the object array passed in to get the value of the Out parameter. Kind of annoying, but deal with it.</p>
<p>And there you have it, how to not only find a methodInfo with an out parameter, but how to get the value back too. YAY</p>
<pre>  <span style="color: #0000ff;">using</span> System;
  <span style="color: #0000ff;">using</span> System.Reflection;</pre>
<p>My next post will show why I had to figure this out and I promise it will be worth it.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/utilities/duck-typing-my-way-to-a-universal-string-convert/" title="Duck Typing my way to a Universal String Convert">Duck Typing my way to a Universal String Convert</a></li><li><a href="http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/" title="ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View">ASP.Net MVC: Upload Image to Database and Show Image &#8220;Dynamically&#8221; Using a View</a></li><li><a href="http://byatool.com/ui/jquery-slide-menu-another-cause-i-can-experiment/" title="jQuery Slide Menu&#8230;  Another Cause I Can Experiment">jQuery Slide Menu&#8230;  Another Cause I Can Experiment</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/getting-back-out-parameters-using-getmethod/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Filling a Private Field on a Base Class Using Reflection</title>
		<link>http://byatool.com/general-coding/filling-a-private-field-on-a-base-class/</link>
		<comments>http://byatool.com/general-coding/filling-a-private-field-on-a-base-class/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 17:03:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Reflection]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/07/01/filling-a-private-field-on-a-base-class/</guid>
		<description><![CDATA[Ok here&#8217;s the next step in this testing kick. So now you have your test classes that create classes. Swell. Problem is, there are private fields on the base class of whatever class you are creating. So you&#8217;re screwed, right? Not really. Now what I am about to do is nothing new. It&#8217;s just basically [...]]]></description>
			<content:encoded><![CDATA[<p>Ok here&#8217;s the next step in this testing kick. So now you have your test classes that create classes. Swell. Problem is, there are private fields on the base class of whatever class you are creating. So you&#8217;re screwed, right? Not really. Now what I am about to do is nothing new. It&#8217;s just basically using Reflection and FieldInfo to fill a field on a base class. Actually very easy. Here&#8217;s the code for the example.</p>
<pre>using System;
using System.Collections.Generic;
using System.Reflection;

<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">UserBase</span>
{
  <span style="color: #3333ff;">private </span><span style="color: #00cccc;">Boolean </span>_isBaseUser;

  <span style="color: #3333ff;">public </span>UserBase()
  {
      _isBaseUser = true;
  }

  <span style="color: #3333ff;">public </span><span style="color: #00cccc;">Boolean </span>IsBaseUser
  {
      get
      {
          <span style="color: #3333ff;">return </span>_isBaseUser;
      }
  }
}

<span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">MainUser </span>: <span style="color: #00cccc;">UserBase</span>
{
  <span style="color: #009900;">//Simple list used to "cache" the field info so reflection doesn't have to be used</span>
<span style="color: #009900;">        //again for a type that has already been used.</span>
  <span style="color: #3333ff;">private static</span> <span style="color: #00cccc;">Dictionary</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">FieldInfo</span>&gt;&gt; typeToInfoList;

  <span style="color: #666666;">/// &lt;summary&gt;</span>
<span style="color: #666666;">        /// <span style="color: #009900;">This is used to fill in the needed field on the passed in object.  This is done by reflection/</span></span>
<span style="color: #666666;">        /// <span style="color: #009900;">FieldInfo.  Basically you get the field info you want off the type, then you use the info to</span></span>
<span style="color: #666666;">        /// <span style="color: #009900;">fill the field on the object.</span>  </span>
<span style="color: #666666;">        /// &lt;/summary&gt;</span>
<span style="color: #666666;">        /// &lt;param name="objectToFill"&gt;<span style="color: #009900;">This is the object that needs the field changed.</span>&lt;/param&gt;</span>
<span style="color: #666666;">        /// &lt;param name="fieldName"&gt;<span style="color: #009900;">This is the name of the field.</span>&lt;/param&gt;</span>
<span style="color: #666666;">        /// &lt;param name="value"&gt;<span style="color: #009900;">This is the value to be set.</span>&lt;/param&gt;</span>
<span style="color: #666666;">        /// &lt;param name="typeToCheck"&gt;<span style="color: #009900;">This is the type of the class that the field resides.</span>&lt;/param&gt;</span>
  <span style="color: #3333ff;">public static</span> <span style="color: #3333ff;">void </span>FillField(<span style="color: #00cccc;">Object </span>objectToFill, <span style="color: #00cccc;">String </span>fieldName, <span style="color: #00cccc;">Object </span>value, <span style="color: #00cccc;">Type </span>typeToCheck)
  {
      <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">FieldInfo</span>&gt; fieldInfoList;
      <span style="color: #00cccc;">FieldInfo </span>neededFieldInfo;
      <span style="color: #00cccc;">Boolean </span>heldInfoList;

      <span style="color: #3333ff;">if </span>(typeToInfoList == <span style="color: #3333ff;">null</span>)
      {
          typeToInfoList = new <span style="color: #00cccc;">Dictionary</span>&lt;<span style="color: #00cccc;">Type</span>, <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">FieldInfo</span>&gt;&gt;();
      }
      <span style="color: #009900;">//Check to see of the list already has the field info and save that </span>
<span style="color: #009900;">          //boolean for later use.</span>
      heldInfoList = typeToInfoList.ContainsKey(typeToCheck);
      <span style="color: #009900;">//If it is in the "cache", grab it.  If not, create a new list</span>
<span style="color: #009900;">          //for the passed in type.</span>
      fieldInfoList = heldInfoList ? typeToInfoList[typeToCheck] : new <span style="color: #00cccc;">List</span>&lt;<span style="color: #00cccc;">FieldInfo</span>&gt;(typeToCheck.GetFields(<span style="color: #00cccc;">BindingFlags</span>.Instance | <span style="color: #00cccc;">BindingFlags</span>.NonPublic | <span style="color: #00cccc;">BindingFlags</span>.Public));

     <span style="color: #009900;">//Now just look for the needed field info in the list. </span>
     neededFieldInfo = fieldInfoList.Find(currentItem =&gt; currentItem.Name == fieldName);
      <span style="color: #009900;">//Use the field info to set the value on the object.</span>
      neededFieldInfo.SetValue(objectToFill, value);

      <span style="color: #009900;">//Store the field info list if it isn't being stored already.</span>
      if (!heldInfoList)
      {
          typeToInfoList.Add(typeToCheck, fieldInfoList);
      }
  }

  <span style="color: #009900;">//Simple constructor to create the user.</span>
  <span style="color: #3333ff;">public static</span> <span style="color: #00cccc;">MainUser </span>Create()
  {
      <span style="color: #00cccc;">MainUser </span>testUser;

      testUser = new <span style="color: #00cccc;">MainUser</span>();

      <span style="color: #3333ff;">return </span>testUser;
  }
}</pre>
<p>That&#8217;s pretty much it. What the hell is all that? Well basically you have the UserBase as the base class for the example. MainUser that in inherits UserBase. The FillField method that does all the work. And lastly, the dictionary used as a lame cache for this example. Why cache anything? Well everything you get the field info, reflection is used. This can be expensive. So why bother getting the same field info for the same type every time this method is called? Just store it somewhere so that if the same class type is passed through again, you can easily access the field info for the class without going for reflection again.</p>
<p>Here&#8217;s an example of the use:</p>
<pre>using Microsoft.VisualStudio.TestTools.UnitTesting;

 [<span style="color: #00cccc;">TestClass</span>]
 <span style="color: #3333ff;">public class</span> <span style="color: #00cccc;">FillFieldTest</span>
 {
     [<span style="color: #00cccc;">TestMethod</span>]
     <span style="color: #3333ff;">public void</span> FillField_CheckFieldForChange()
     {
         <span style="color: #00cccc;">MainUser </span>testUser;

         testUser = new <span style="color: #00cccc;">MainUser</span>();
         <span style="color: #00cccc;">Assert</span>.IsTrue(testUser.IsBaseUser);

         MainUser.FillField(testUser, "_isBaseUser", <span style="color: #3333ff;">false</span>, <span style="color: #3333ff;">typeof</span>(UserBase));
         <span style="color: #00cccc;">Assert</span>.IsFalse(testUser.IsBaseUser);

         <span style="color: #00cccc;">MainUser</span>.FillField(testUser, "_isBaseUser", <span style="color: #3333ff;">true</span>, <span style="color: #3333ff;">typeof</span>(UserBase));
         <span style="color: #00cccc;">Assert</span>.IsTrue(testUser.IsBaseUser);
     }
 }</pre>
<p>First test is checking to see if the IsBaseUser property is true. This will be true since UserBase sets _isBaseUser to true on instantiation.</p>
<p>Second test is checking to see if the FillField method worked correctly.</p>
<p>Third test is really just to step through a second time so you can see how the quasi-caching works.</p>
<div  class="related_post_title">Related Posts</div><ul class="related_post"><li><a href="http://byatool.com/test/mulitple-constraint-generics-and-test-base-classes/" title="Mulitple Constraint Generics and Test Base Classes">Mulitple Constraint Generics and Test Base Classes</a></li><li><a href="http://byatool.com/test/test-projects-and-unit-tests-part-1/" title="Test Projects and Unit Tests In Visual Studio 2008">Test Projects and Unit Tests In Visual Studio 2008</a></li><li><a href="http://byatool.com/lessons/how-to-use-a-factory-method-with-castle-windsorcontainer/" title="How to Use a Factory Method With Castle / WindsorContainer">How to Use a Factory Method With Castle / WindsorContainer</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/filling-a-private-field-on-a-base-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
