コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arg"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static T To <T>(this IArgument arg, Engine context)
        {
            var o = arg.Evaluate(context) ?? throw new ArgumentTypeException("Evaluated to null", arg);

            if (o is T t)
            {
                return(t);
            }
            throw new ArgumentTypeException($"Cannot convert to {typeof(T)} ({o.GetType()}): {o}", arg);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static int ToInt(this IArgument arg, Engine context)
        {
            var o = arg.Evaluate(context) ?? throw new ArgumentTypeException("Evaluated to null", arg);

            switch (o)
            {
            case int i:
                return(i);

            case double d:
                return((int)d);

            default:
                var s = o.ToString();
                if (!int.TryParse(s, out var v))
                {
                    throw new ArgumentTypeException($"Cannot convert to int ({o.GetType().FullName}): {s}", arg);
                }
                return(v);
            }
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arg"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool ToBool(this IArgument arg, Engine context)
        {
            var o = arg.Evaluate(context) ?? throw new ArgumentTypeException("Evaluated to null", arg);

            switch (o)
            {
            case bool b:
                return(b);

            case double d:
                return(d != 0.0);

            case int i:
                return(i != 0);

            default:
                var s = o.ToString();
                if (!bool.TryParse(s, out var v))
                {
                    throw new ArgumentTypeException($"Cannot convert to bool ({o.GetType().FullName}): {s}", arg);
                }
                return(v);
            }
        }