public void ExecuteImportCommand() { ImportCommand importcmd = new ImportCommand("setvar"); Machine machine = new Machine(); importcmd.Execute(machine.Environment); object setvar = machine.Environment.GetValue("setvar"); Assert.IsNotNull(setvar); Assert.IsInstanceOfType(setvar, typeof(IValues)); IValues setvarenv = (IValues)setvar; object var = setvarenv.GetValue("a"); Assert.IsNotNull(var); Assert.AreEqual(1, var); object doc = setvarenv.GetValue("__doc__"); Assert.IsNotNull(doc); Assert.AreEqual("setvar module", doc); }
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++; } }
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)); }
public void EvaluateImport() { Parser parser = new Parser(new StreamReader("import.py")); ICommand command = parser.CompileCommandList(); Machine machine = new Machine(); command.Execute(machine.Environment); object mod = machine.Environment.GetValue("setvar"); Assert.IsNotNull(mod); Assert.IsInstanceOfType(mod, typeof(IValues)); IValues modenv = (IValues)mod; Assert.AreEqual(1, modenv.GetValue("a")); }
/// <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); }
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)); }
public ActionResult <string> Get(int id) { return(_values.GetValue(id)); }