title: How to assign C# dynamic variable properties to another dynamic object?
body: <p>I'm trying to write a function which creates an object of Type t and assign its properties. </p>
  
  <pre><code>    internal static object CreateInstanceWithParam(Type t, dynamic d)
      {
          //dynamic obj =  t.GetConstructor(new Type[] { d }).Invoke(new object[] { d });
  
          dynamic obj =  t.GetConstructor(new Type[] { }).Invoke(new object[] { });
          foreach (var prop in d.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
          {
              //prop.Name, 
              //prop.GetValue(d, null);
  
              // assign the properties and corresponding values to newly created object ???
          }
          return obj;
      }
  </code></pre>
  
  <p>Then I should be able to use this for any kind of class types like </p>
  
  <pre><code>IUser user = (IUser)CreateInstanceWithParam(userType, new { UserID = 0, Name = "user abc", LoginCode = "abc", DefaultPassword = "xxxxxx" });
  
  IUnit newUnit = (IUnit)CreateInstanceWithParam(unitType, new { ID = 3, Code = "In", Name = "Inch", Points = "72" })
  </code></pre>
  
  <p>How can I assign the property <code>prop.Name</code> to <code>obj</code>?</p>