// this does the opposite of ToStruct(), takes a InteropObject and converts it to a VM.Struct private static VMObject CastViaReflection(object srcObj, int level) { var srcType = srcObj.GetType(); if (srcType.IsArray) { var children = new Dictionary <VMObject, VMObject>(); var array = (Array)srcObj; for (int i = 0; i < array.Length; i++) { var val = array.GetValue(i); var key = new VMObject(); key.SetValue(i); var vmVal = CastViaReflection(val, level + 1); children[key] = vmVal; } var result = new VMObject(); result.SetValue(children); return(result); } else { var targetType = VMObject.GetVMType(srcType); VMObject result; bool isKnownType = typeof(BigInteger) == srcType || typeof(Timestamp) == srcType || typeof(ISerializable).IsAssignableFrom(srcType); if (srcType.IsStructOrClass() && !isKnownType) { var children = new Dictionary <VMObject, VMObject>(); var fields = srcType.GetFields(); if (fields.Length > 0) { foreach (var field in fields) { var key = new VMObject(); key.SetValue(field.Name); var val = field.GetValue(srcObj); var vmVal = CastViaReflection(val, level + 1); children[key] = vmVal; } result = new VMObject(); result.SetValue(children); return(result); } } result = VMObject.FromObject(srcObj); if (result != null) { return(result); } throw new Exception($"invalid cast: Interop.{srcType.Name} to vm object"); } }