Esempio n. 1
0
        public void Execute(IContext context)
        {
            Module module = ModuleUtilities.LoadModule(this.modname, context);

            IValues values = context;
            int     nname  = 0;

            foreach (var name in this.names)
            {
                string normname = name.Trim();

                if (nname == this.names.Length - 1)
                {
                    values.SetValue(normname, module);
                }
                else if (!values.HasValue(normname))
                {
                    var mod = new Module(context.GlobalContext);
                    values.SetValue(normname, mod);
                    values = mod;
                }
                else
                {
                    values = (IValues)values.GetValue(normname);
                }

                nname++;
            }
        }
Esempio n. 2
0
        public object GetValue(object obj)
        {
            IValues values = obj as IValues;

            if (values == null)
            {
                IType type = Types.GetType(obj);

                if (type != null)
                {
                    return(type.GetMethod(this.name));
                }

                if (obj is Type)
                {
                    return(TypeUtilities.GetValue((Type)obj, this.name));
                }

                return(ObjectUtilities.GetValue(obj, this.name));
            }

            object value = values.GetValue(this.name);

            if (value != null)
            {
                return(value);
            }

            if (values.HasValue(this.name))
            {
                return(value);
            }

            string typename;

            if (values is BindingEnvironment)
            {
                typename = "module";
            }
            else if (values is DynamicObject)
            {
                typename = ((DynamicObject)values).Class.Name;
            }
            else
            {
                typename = values.GetType().Name;
            }

            throw new AttributeError(string.Format("'{1}' object has no attribute '{0}'", this.name, typename));
        }
Esempio n. 3
0
        /// <summary>
        /// Gets a color setting from the settings instance.
        /// </summary>
        /// <param name="values">
        /// The settings reference.
        /// </param>
        /// <param name="name">
        /// The name of the setting to retrieve.
        /// </param>
        /// <param name="defaultValue">
        /// The default color value to use if a error occurs or no setting with the specified <see cref="name"/> was found.
        /// </param>
        /// <returns>
        /// Returns the color for the specified setting.
        /// </returns>
        public static Color32 GetColorSetting(this IValues <string> values, string name, Color32 defaultValue)
        {
            // set default value
            var color = defaultValue;

            // check if setting exists
            if (values.HasValue(name))
            {
                // attempt to get setting string
                var value = values.GetSetting(name, string.Format("{0} {1} {2} {3}", color.a, color.r, color.g, color.b));

                // parse the color string
                ParseColorString(name, value, ref color);
            }

            // return the color
            return(color);
        }
        /// <summary>
        /// Tries to get the setting value.
        /// </summary>
        /// <param name="values">
        /// Reference to the <see cref="IValues{TKey}"/> implementation.
        /// </param>
        /// <param name="key">
        /// The key of the setting.
        /// </param>
        /// <param name="value">
        /// The value of the setting if successful.
        /// </param>
        /// <typeparam name="TKey">
        /// Defines the index key type that will be use to identify the value.
        /// </typeparam>
        /// <typeparam name="T">
        /// The type of setting to retrieve.
        /// </typeparam>
        /// <returns>
        /// Returns true if the setting was retrieved successfully. Will return false if the setting was missing or threw an exception.
        /// </returns>
        public static bool TryGetSetting <TKey, T>(this IValues <TKey> values, TKey key, out T value)
        {
            if (!values.HasValue(key))
            {
                value = default(T);
                return(false);
            }

            T retrievedValue;

            try
            {
                retrievedValue = values.GetValue <T>(key);
            }
            catch (Exception)
            {
                value = default(T);
                return(false);
            }

            value = retrievedValue;
            return(true);
        }
Esempio n. 5
0
        public object Evaluate(IContext context)
        {
            IList <object> arguments = null;
            IDictionary <string, object> namedArguments = null;

            if (this.hasnames)
            {
                namedArguments = new Dictionary <string, object>();
            }

            if (this.argumentExpressions != null && this.argumentExpressions.Count > 0)
            {
                arguments = new List <object>();

                foreach (var argexpr in this.argumentExpressions)
                {
                    object value = argexpr.Evaluate(context);

                    if (this.hasnames && argexpr is NamedArgumentExpression)
                    {
                        namedArguments[((NamedArgumentExpression)argexpr).Name] = value;
                    }
                    else
                    {
                        arguments.Add(argexpr.Evaluate(context));
                    }
                }
            }

            IFunction function = null;

            // TODO to skip AttributeExpression, or have a separated MethodCallExpression
            if (this.isobject)
            {
                var attrexpr = (AttributeExpression)this.targetExpression;
                var obj      = attrexpr.Expression.Evaluate(context);

                if (obj is DynamicObject)
                {
                    var dynobj = (DynamicObject)obj;
                    return(dynobj.Invoke(attrexpr.Name, context, arguments, namedArguments));
                }

                function = this.GetFunction(obj, attrexpr.Name);

                if (function == null)
                {
                    IType type = Types.GetType(obj);

                    if (type == null)
                    {
                        IValues values = obj as IValues;

                        if (values != null && values.HasValue(attrexpr.Name))
                        {
                            object value = values.GetValue(attrexpr.Name);
                            function = value as IFunction;

                            if (function == null)
                            {
                                if (value is Type)
                                {
                                    return(Activator.CreateInstance((Type)value, arguments == null ? null : arguments.ToArray()));
                                }
                            }
                        }

                        if (function == null)
                        {
                            if (obj is Type)
                            {
                                return(TypeUtilities.InvokeTypeMember((Type)obj, attrexpr.Name, arguments));
                            }

                            return(ObjectUtilities.GetValue(obj, attrexpr.Name, arguments));
                        }
                    }

                    function = type.GetMethod(attrexpr.Name);
                    arguments.Insert(0, obj);
                }
            }
            else
            {
                var value = this.targetExpression.Evaluate(context);
                function = value as IFunction;

                if (function == null)
                {
                    Type type = (Type)value;
                    return(Activator.CreateInstance(type, arguments == null ? null : arguments.ToArray()));
                }
            }

            return(function.Apply(context, arguments, namedArguments));
        }