title: Use Dynamic objects to return different Types from a Method
body: <p>The Microsoft Unit Test Wizard creates Accessor objects if you need to test a non-public property from another project. Inside my Unit Tests I create helper functions so that I don't repeat the same code just in every Unit Test method. Currently I have two tests that are almost identical except one takes a standard public object, and the other takes the Accessor version. Since the Accessor is based on the standard version I should be able to have one function. </p>
  
  <p>I had assumed that I could use Generics to accomplish with a simple cast. But after <a href="https://stackoverflow.com/questions/12998916/update-two-common-functions-to-use-generics">posting the question</a> I discovered it was a lot more work, including having to update the underlying objects. This was confirmed when I asked if any other approach existed. It was suggested that <a href="https://stackoverflow.com/questions/13054481/best-way-to-update-two-common-functions-to-use-shared-function">Dynamic objects might work</a>. I'm not familiar with Dynamic objects, could someone post an example that allow me to have one shared function and determine the object at run time?  </p>
  
  <p>Here are the existing two functions:</p>
  
  <pre><code>// Common function to create a new test record with standard Account object
  internal static Account CreateAccount(bool saveToDatabase)
  {
      DateTime created = DateTime.Now;
      string createdBy = _testUserName;
  
      Account account = new Account(created, createdBy);
  
      account.Notes = Utilities.RandomString(1000);
  
      if (saveToDatabase)
          account.Create();
  }
  
  // Common function to create a new test record with Account_Accessor
  internal static Account_Accessor CreateAccount(bool saveToDatabase)
  {
      DateTime created = DateTime.Now;
      string createdBy = _testUserName;
  
      Account_Accessor account = new Account_Accessor(created, createdBy);
  
      account.Notes = Utilities.RandomString(1000);
  
      if (saveToDatabase)
          account.Create();
  }
  </code></pre>
  
  <p>I have two dozen of these Unit Tests and the real objects have an average of 10 properties, I've simplified the examples here. </p>
  
  <p>Here is the Accessor code that the Unit Test API creates (again, I have reduced it down to simplify the example):</p>
  
  <pre><code>using Microsoft.VisualStudio.TestTools.UnitTesting;
  using System;
  using System.Collections.ObjectModel;
  using System.Data;
  
  namespace NameHere.Bll
  {
      [Shadowing("NameHere.Bll.Account")]
      public class Account_Accessor : ProjectBase_Accessor&lt;Account&gt;
      {
          protected static PrivateType m_privateType;
  
          public Account_Accessor(PrivateObject value);
          [Shadowing(".ctor@2")]
          public Account_Accessor(DateTime created, string createdBy);
  
          [Shadowing("_notes")]
          public string _notes { get; set; }
  
          public static Account_Accessor AttachShadow(object value);
  
          [Shadowing("Create@0")]
          public override void Create();
      }
  }
  
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using System;
  using System.ComponentModel;
  using System.Linq.Expressions;
  
  namespace NameHere.Bll
  {
      [Shadowing("NameHere.Bll.ProjectBase`1")]
      public class ProjectBase_Accessor&lt;T&gt; : BaseShadow, INotifyPropertyChanged
      {
          protected static PrivateType m_privateType;
  
          public ProjectBase_Accessor(PrivateObject value);
  
          [Shadowing("Created")]
          public DateTime Created { get; set; }
          public static PrivateType ShadowedType { get; }
  
          [Shadowing("add_PropertyChanged@1")]
          public void add_PropertyChanged(PropertyChangedEventHandler value);
          public static ProjectBase_Accessor&lt;T&gt; AttachShadow(object value);
  
          [Shadowing("Create@0")]
          public virtual void Create();
      }
  }
  </code></pre>