title: Casting on run time using implicit con version
body: <p>I have the following code which copies property values from one object to another objects by matching their property names:</p>
  
  <pre><code>public static void CopyProperties(object source, object target,bool caseSenstive=true)
      {
          PropertyInfo[] targetProperties = target.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
          PropertyInfo[] sourceProperties = source.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
          foreach (PropertyInfo tp in targetProperties)
          {
              var sourceProperty = sourceProperties.FirstOrDefault(p =&gt; p.Name == tp.Name);
              if (sourceProperty == null &amp;&amp; !caseSenstive)
              {
                  sourceProperty = sourceProperties.FirstOrDefault(p =&gt; p.Name.ToUpper() == tp.Name.ToUpper());
              }
              // If source doesn't have this property, go for next one.
              if(sourceProperty ==null)
              {
                  continue;
              }
  
              // If target property is not writable then we can not set it; 
              // If source property is not readable then cannot check it's value 
              if (!tp.CanWrite || !sourceProperty.CanRead)
              {
                  continue;
              }
  
              MethodInfo mget = sourceProperty.GetGetMethod(false);
              MethodInfo mset = tp.GetSetMethod(false);
  
              // Get and set methods have to be public 
              if (mget == null)
              {
                  continue;
              }
  
              if (mset == null)
              {
                  continue;
              }
  
  
              var sourcevalue = sourceProperty.GetValue(source, null);
              tp.SetValue(target, sourcevalue, null);
  
          }
      }
  </code></pre>
  
  <p>This is working well when the type of properties on target and source are the same. But when there is a need for casting, the code doesn't work.</p>
  
  <p>For example, I have the following object:</p>
  
  <pre><code>class MyDateTime
  {
      public static implicit operator DateTime?(MyDateTime myDateTime)
      {
          return myDateTime.DateTime;
      }
  
      public static implicit operator DateTime(MyDateTime myDateTime)
      {
          if (myDateTime.DateTime.HasValue)
          {
              return myDateTime.DateTime.Value;
          }
          else
          {
              return System.DateTime.MinValue;
          }
      }
  
      public static implicit operator MyDateTime(DateTime? dateTime)
      {
          return FromDateTime(dateTime);
      }
  
      public static implicit operator MyDateTime(DateTime dateTime)
      {
          return FromDateTime(dateTime);
      }
   }
  </code></pre>
  
  <p>If I do the following, the implicit cast is called and everything works well: </p>
  
  <pre><code>MyDateTime x= DateTime.Now; 
  </code></pre>
  
  <p>But when I have a two objects that one of them has a DateTime and the other has MyDateTime, and I am using the above code to copy properties from one object to other, it doesn't and generate an error saying that DateTime can not converted to MyTimeDate.</p>
  
  <p>How can I fix this problem?</p>