public object Provide(object context) { object o = GetCodeObject(); Type t = o.GetType(); MethodInfo mi = t.GetMethod(CodeMethodName); NameValueContext varContext = new NameValueContext(); foreach (VariableDescriptor varDescr in Variables) { object varValue = varDescr.VarProvider.Provide(context); if (varValue != null && !varDescr.VarType.IsInstanceOfType(varValue)) { ITypeConverter cnv = ConvertManager.FindConverter(varValue.GetType(), varDescr.VarType); if (cnv != null) { varValue = cnv.Convert(varValue, varDescr.VarType); } else { throw new InvalidCastException( String.Format("Context variable {0} cannot be cast to expected type {1}", varDescr.Name, varDescr.VarType)); } } varContext[varDescr.Name] = varValue; } object s = mi.Invoke(o, new object[] { varContext }); return(s); }
static void Main(string[] args) { LogManager.Configure(new TraceLogger()); IComponentsConfig config = ConfigurationSettings.GetConfig("components") as IComponentsConfig; INamedServiceProvider srvPrv = new NReco.Winter.ServiceProvider(config); var zz = ConvertManager.ChangeType <IOperation <object> >(srvPrv.GetService("ZZ")); IOperation <IDictionary <string, object> > a = srvPrv.GetService("A") as IOperation <IDictionary <string, object> >; NameValueContext c = new NameValueContext(); Console.WriteLine("NReco 'hello world' sample. Try to type 'hello' or 'what is your name', 'die' for exit"); while (true) { Console.Write("you> "); c["msg"] = Console.ReadLine(); Console.Write("nreco> "); a.Execute(c); if (c.ContainsKey("state") && Convert.ToString(c["state"]) == "exit") { return; } } }
public void GenericOperationConverterTest() { OperationConverter gOpCnv = new OperationConverter(); NameValueContext c = new NameValueContext(); InvokeMethod op = new InvokeMethod(this, "TestMethod", new object[] { c }); TestGenOp genOp = new TestGenOp(); Assert.AreEqual(true, gOpCnv.CanConvert(genOp.GetType(), typeof(IOperation <object>))); Convert.ToString(gOpCnv.Convert(genOp, typeof(IOperation <object>)).GetType()); Assert.AreEqual(true, gOpCnv.Convert(genOp, typeof(IOperation <object>)) is IOperation <object>); ((IOperation <object>)gOpCnv.Convert(genOp, typeof(IOperation <object>))).Execute(c); Assert.AreEqual("a", c["b"]); Assert.AreEqual(true, gOpCnv.CanConvert(op.GetType(), typeof(IOperation <NameValueContext>))); Assert.AreEqual(true, gOpCnv.Convert(op, typeof(IOperation <NameValueContext>)) is IOperation <NameValueContext>); ((IOperation <NameValueContext>)gOpCnv.Convert(op, typeof(IOperation <NameValueContext>))).Execute(c); Assert.AreEqual("b", c["a"]); // compatible conversion between IOperation<> Assert.AreEqual(true, gOpCnv.CanConvert(typeof(IOperation <Context>), typeof(IOperation <NameValueContext>))); Assert.AreEqual(true, gOpCnv.CanConvert(typeof(IOperation <NameValueContext>), typeof(IOperation <Context>))); IOperation <NameValueExContext> genOpEx = (IOperation <NameValueExContext>)gOpCnv.Convert(genOp, typeof(IOperation <NameValueExContext>)); NameValueExContext cEx = new NameValueExContext(); genOpEx.Execute(cEx); Assert.AreEqual("a", cEx["b"]); }
public void ChainTest() { List <string> log = new List <string>(); ChainOperationCall call1 = new ChainOperationCall(new LogOperation(log, "1")); ChainOperationCall call2 = new ChainOperationCall(new LogOperation(log, "2")); call2.RunCondition = new ConstProvider <IDictionary <string, object>, bool>(false); Chain c = new Chain() { Operations = new IOperation <IDictionary <string, object> >[] { call1, call2 } }; NameValueContext context = new NameValueContext(); c.Execute(context); Assert.AreEqual(1, log.Count); Assert.AreEqual("1", log[0]); }
public void TestMethod(NameValueContext c) { c["a"] = "b"; }