public override object Value(Type type)
 {
     if (OnlineMapsReflectionHelper.IsValueType(type))
     {
         return(Activator.CreateInstance(type));
     }
     return(null);
 }
Esempio n. 2
0
    public override object Value(Type t)
    {
        if (_type == ValueType.NULL || _value == null)
        {
            if (OnlineMapsReflectionHelper.IsValueType(t))
            {
                return(Activator.CreateInstance(t));
            }
            return(null);
        }

        if (t == typeof(string))
        {
            return(Convert.ChangeType(_value, t));
        }

        if (_type == ValueType.BOOLEAN)
        {
            if (t == typeof(bool))
            {
                return(Convert.ChangeType(_value, t));
            }
        }
        else if (_type == ValueType.DOUBLE)
        {
            if (t == typeof(double))
            {
                return(Convert.ChangeType(_value, t, OnlineMapsUtils.numberFormat));
            }
            if (t == typeof(float))
            {
                return(Convert.ChangeType((double)_value, t, OnlineMapsUtils.numberFormat));
            }
        }
        else if (_type == ValueType.LONG)
        {
            if (t == typeof(long))
            {
                return(Convert.ChangeType(_value, t));
            }
#if UNITY_EDITOR
            if (t.IsSubclassOf(typeof(UnityEngine.Object)))
            {
                return(UnityEditor.EditorUtility.InstanceIDToObject((int)(long)_value));
            }
#endif

            try
            {
                return(Convert.ChangeType((long)_value, t));
            }
            catch (Exception e)
            {
                Debug.Log(e.Message + "\n" + e.StackTrace);
                return(null);
            }
        }
        else if (_type == ValueType.STRING)
        {
            MethodInfo method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string), typeof(IFormatProvider) });
            if (method != null)
            {
                return(method.Invoke(null, new object[] { value, OnlineMapsUtils.numberFormat }));
            }

            method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string) });
            return(method.Invoke(null, new[] { value }));
        }
        StringBuilder builder = new StringBuilder();
        ToJSON(builder);
        throw new InvalidCastException(t.FullName + "\n" + builder);
    }
Esempio n. 3
0
    public override object Value(Type t)
    {
        if (_type == ValueType.NULL || _value == null)
        {
            if (OnlineMapsReflectionHelper.IsValueType(t))
            {
                return(Activator.CreateInstance(t));
            }
            return(null);
        }

        if (t == typeof(string))
        {
            return(Convert.ChangeType(_value, t));
        }

        if (_type == ValueType.BOOLEAN)
        {
            if (t == typeof(bool))
            {
                return(Convert.ChangeType(_value, t));
            }
        }
        else if (_type == ValueType.DOUBLE)
        {
            if (t == typeof(double))
            {
                return(Convert.ChangeType(_value, t));
            }
            if (t == typeof(float))
            {
                return(Convert.ChangeType((double)_value, t));
            }
        }
        else if (_type == ValueType.LONG)
        {
            if (t == typeof(long))
            {
                return(Convert.ChangeType(_value, t));
            }
            if (t == typeof(int))
            {
                return(Convert.ChangeType((long)_value, t));
            }
            if (t == typeof(float))
            {
                return(Convert.ChangeType((long)_value, t));
            }
            if (t == typeof(double))
            {
                return(Convert.ChangeType((long)_value, t));
            }
        }
        else if (_type == ValueType.STRING)
        {
            MethodInfo method = OnlineMapsReflectionHelper.GetMethod(t, "Parse", new[] { typeof(string) });
            if (method != null)
            {
                return(method.Invoke(null, new[] { _value }));
            }
            return(null);
        }
        StringBuilder builder = new StringBuilder();

        ToJSON(builder);
        throw new InvalidCastException(t.FullName + "\n" + builder);
    }