public static object ChangeType(ScriptValue value, Type type) { if (type == TYPE_VALUE) { return(value); } switch (value.valueType) { case ScriptValue.doubleValueType: case ScriptValue.longValueType: return(type.IsEnum ? Enum.ToObject(type, value.ToInt32()) : Convert.ChangeType(value.Value, type)); case ScriptValue.scriptValueType: { if (value.scriptValue is ScriptFunction && TYPE_DELEGATE.IsAssignableFrom(type)) { return(ScorpioDelegateFactory.CreateDelegate(type, value.scriptValue)); } return(value.scriptValue.Value); } default: return(value.Value); } }
// public static object ChangeType(ScriptValue value, Type type) { if (type == typeof(ScriptValue)) { return(value); } switch (value.valueType) { case ScriptValue.doubleValueType: { unchecked { if (type == typeof(double) || type == typeof(object)) { return(value.doubleValue); } if (type == typeof(long)) { return((long)value.doubleValue); } if (type == typeof(ulong)) { return((ulong)value.doubleValue); } if (type == typeof(int)) { return((int)value.doubleValue); } if (type == typeof(float)) { return((float)value.doubleValue); } if (type == typeof(string)) { return(value.doubleValue.ToString()); } if (type.IsEnum) { return(Enum.ToObject(type, (int)value.doubleValue)); } } throw new System.Exception($"其他数字类型请先转换再传入 source:DoubleNumber target:{type.ToString()}"); } case ScriptValue.longValueType: { unchecked { if (type == typeof(long) || type == typeof(object)) { return(value.longValue); } if (type == typeof(double)) { return((double)value.longValue); } if (type == typeof(ulong)) { return((ulong)value.longValue); } if (type == typeof(int)) { return((int)value.longValue); } if (type == typeof(float)) { return((float)value.longValue); } if (type == typeof(string)) { return(value.longValue.ToString()); } if (type.IsEnum) { return(Enum.ToObject(type, (int)value.longValue)); } } throw new System.Exception($"其他数字类型请先转换再传入 source:LongNumber target:{type.ToString()}"); } case ScriptValue.nullValueType: return(null); case ScriptValue.trueValueType: return(true); case ScriptValue.falseValueType: return(false); case ScriptValue.stringValueType: return(value.stringValue); case ScriptValue.objectValueType: return(value.objectValue); case ScriptValue.scriptValueType: { if (value.scriptValue is ScriptFunction && typeof(Delegate).IsAssignableFrom(type)) { return(ScorpioDelegateFactory.CreateDelegate(type, value.scriptValue)); } return(value.scriptValue.Value); } default: return(value.Value); } }