public static VMObject CastTo(VMObject srcObj, VMType type) { if (srcObj.Type == type) { var result = new VMObject(); result.Copy(srcObj); return(result); } switch (type) { case VMType.None: return(new VMObject()); case VMType.String: { var result = new VMObject(); result.SetValue(srcObj.AsString()); // TODO does this work for all types? return(result); } case VMType.Timestamp: { var result = new VMObject(); result.SetValue(srcObj.AsTimestamp()); // TODO does this work for all types? return(result); } case VMType.Bool: { var result = new VMObject(); result.SetValue(srcObj.AsBool()); // TODO does this work for all types? return(result); } case VMType.Bytes: { var result = new VMObject(); result.SetValue(srcObj.AsByteArray()); // TODO does this work for all types? return(result); } case VMType.Number: { var result = new VMObject(); result.SetValue(srcObj.AsNumber()); // TODO does this work for all types? return(result); } case VMType.Struct: switch (srcObj.Type) { case VMType.Object: return(CastViaReflection(srcObj.Data, 0)); default: throw new Exception($"invalid cast: {srcObj.Type} to {type}"); } default: throw new Exception($"invalid cast: {srcObj.Type} to {type}"); } }