title: Object does not match target type with SetValue and enum
body: <p>I tried adapting <a href="http://www.codeproject.com/Articles/15292/Setting-Enum-s-Through-Reflection" rel="nofollow">this tutorial from CodeProject</a> to try and change a <code>dynamic</code> which will in this case be an int to a simple <code>Enum</code>.</p>
  
  <p>If we define the <code>Enum</code> like so:</p>
  
  <pre><code>public Enum MyEnum { Zero = 0, One = 1, Two = 2 }
  </code></pre>
  
  <p>And the contents of a method to set the value of a <code>MyObject</code> class which contains an <code>MyEnum</code>:</p>
  
  <pre><code>var baseType = propertyInfo.PropertyType.BaseType; //`propertyInfo` is the `PropertyInfo` of `MyEnum`
  
  var isEnum = baseType != null &amp;&amp; baseType == typeof(Enum); //true in this case
  
  dynamic d;
  
  d = GetInt(); 
  
  //For example, `d` now equals `0`
  
  if (isEnum)
      d = Enum.ToObject(propertyInfo.PropertyType, (int)d); 
  
  //I can see from debugger that `d` now equals `Zero`
  
  propertyInfo.SetValue(myObject, d); 
  
  //Exception: Object does not match target type
  </code></pre>
  
  <p>Any ideas as to why this is happening?</p>