Пример #1
0
        /// <inheritdoc />
        protected override object ExecuteToken(ScriptContext context)
        {
            string typename = TargetType.Execute(context)?.ToString();

            if (typename == null)
            {
                throw new ScriptRuntimeException("cannot cast to null", this);
            }
            Type type = TypeProvider.DetermineType(this, typename);

            object value = Value.Execute(context);

            if (IsDynamic?.Execute(context)?.ToBoolean() ?? false)
            {
                if (value == null)
                {
                    if (type.IsValueType)
                    {
                        return(Activator.CreateInstance(type));
                    }
                    return(null);
                }

                if (type.IsInstanceOfType(value))
                {
                    return(value);
                }
                return(null);
            }

            if (value == null)
            {
                if (type.IsValueType)
                {
                    throw new ScriptRuntimeException($"null cannot get cast to '{typename}'", this);
                }
                return(null);
            }

            if (!type.IsInstanceOfType(value))
            {
                throw new ScriptRuntimeException($"Unable to cast {value} to '{typename}'", this);
            }

            return(value);
        }
        /// <inheritdoc />
        protected override object ExecuteToken(ScriptContext context)
        {
            string typename = Type.Execute(context)?.ToString();

            if (typename == null)
            {
                throw new ScriptRuntimeException("Invalid parameter type specification", this);
            }

            bool isarray = typename.EndsWith("[]");

            if (isarray)
            {
                typename = typename.Substring(0, typename.Length - 2);
            }

            Type type = TypeProvider.DetermineType(this, typename);

            if (isarray)
            {
                type = typeof(IEnumerable <>).MakeGenericType(type);
            }

            object value;

            if (context.Variables.ContainsVariable(Variable.Name) || (context.Arguments.ContainsVariable(Variable.Name)))
            {
                value = Variable.Execute(context);
            }
            else
            {
                if (DefaultValue != null)
                {
                    value = DefaultValue.Execute(context);
                }
                else
                {
                    throw new ScriptRuntimeException($"Variable {Variable.Name} not provided by script call and no default value provided", this);
                }
            }

            if (value == null)
            {
                if (type.IsValueType)
                {
                    throw new ScriptRuntimeException("Unable to pass null to a value parameter", this);
                }
                return(null);
            }

            if (!type.IsInstanceOfType(value))
            {
                try {
                    value = value = Converter.Convert(value, type);
                }
                catch (Exception e) {
                    throw new ScriptRuntimeException($"Unable to convert parameter '{value}' to '{typename}'", this, e);
                }
            }

            ((VariableProvider)context.Arguments).ReplaceVariable(Variable.Name, value);
            return(value);
        }