Ported latest c++ changes to c#