private static void CreateInputVariables(DynamicFunctionObject functionExpression, XElement lobjChildNode) { foreach (var variable in lobjChildNode.Elements("variable")) { string variableName = variable.Attribute("name").Value; string aliasName = variable.Attribute("alias") == null?variable.Attribute("name").Value : variable.Attribute("alias").Value; functionExpression.InputParams.Add(new Variable(Type.GetType(variableName), aliasName, false)); } //create output variable. functionExpression.FunctionBody.Add(CodeLine.CreateVariable(typeof(mDictionary), "data")); functionExpression.FunctionBody.Add(CodeLine.Assign("data", Operation.CreateInstance(typeof(mDictionary)))); functionExpression.FunctionBody.Add(CodeLine.CreateVariable(typeof(mDictionary), "localDictionary")); // this is local variable of type dictionary (instance is created in single and list block) where the values in single and loop will be filled in and pushed to the single dictionary }
private DynamicFunctionObject ParseFilterFiles(IEnumerable <XElement> aobjElements) { DynamicFunctionObject dfo = new DynamicFunctionObject() { InputParams = new List <Variable>(), FunctionBody = new List <ICodeLine>(),// { CodeLine.CreateVariable(typeof(mDictionary), "data"), CodeLine.Assign("data", Operation.CreateInstance(typeof(mDictionary)))},//this dictionary will be used by single and list nodes. Each and every property/value pair is pushed to this object before adding it to the parent dictionary. FunctionReturn = string.Empty }; try { foreach (var lobjChildNode in aobjElements) { switch (lobjChildNode.Name.LocalName) { case "input": { var codelines = ProcessInputNode(lobjChildNode); dfo.InputParams.AddRange(codelines); } break; case "body": { var codelines = ProcessSingleNode(lobjChildNode, null); //codelines.Add(ProcessAssignElement("data", lobjChildNode.Attribute("name").Value, // lobjChildNode.Attribute("name").Value + "_0")); dfo.FunctionBody.AddRange(codelines); } break; case "list": { // var codelines = ProcessListNode(lobjChildNode); // dfo.FunctionBody.AddRange(codelines); } break; default: break; } } } catch (Exception ex) { } return(dfo); }
//public DynamicFunctionObject iobjFunctionObject { get; set; } public void ParseFilterFiles(object mainobj) { var idicParamStack = new Dictionary <string, object>(); var lobjXDocument = XDocument.Load(@"..\..\newgetcontactInfo.xml"); var lobjRootNode = lobjXDocument.Root; DynamicFunctionObject dfo = ParseFilterFiles(lobjRootNode.XPathSelectElements("/xFilter/xFilterGet").Elements()); var fn = Function.Create() .WithParameters(dfo.InputParams.ToArray()) .WithBody(CodeLine.Nop, dfo.FunctionBody.ToArray()) .Returns("data"); var outputExpr = fn.ToExpression(); var outputExprCompiled = outputExpr.Compile().DynamicInvoke(mainobj); var output = Newtonsoft.Json.JsonConvert.SerializeObject(outputExprCompiled, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); Console.WriteLine(output); Console.ReadLine(); // var fn = Function.Create() // .WithParameters(dfo.InputParams.ToArray()) // .WithBody(CodeLine.Nop, dfo.FunctionBody.ToArray()) // .Returns("data"); // var outputExpr = fn.ToExpression(); // var outputExprCompiled = outputExpr.Compile().DynamicInvoke(mainobj); // var output = Newtonsoft.Json.JsonConvert.SerializeObject(outputExprCompiled, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); // Console.WriteLine(output); // Console.ReadLine(); //} //catch (Exception ex) //{ //} }
private static List <ICodeLine> ProcessSingleElement(XElement aobjSingleChildNode, DynamicFunctionObject aobjDFO, List <ICodeLine> alstCodeLines = null) { // List<ICodeLine> llstCodeLines = alstCodeLines ?? new List<ICodeLine>(); //string lstrSingleObjectKey = aobjSingleChildNode.Attribute("name").Value; //dfo.FunctionBody.Add(CodeLine.Assign(lstrSingleObjectKey, Operation.CreateInstance(typeof(mDictionary)))); // if(aobjDFO.FunctionBody.Where(d)) if (alstCodeLines == null) { alstCodeLines = new List <ICodeLine>(); alstCodeLines.Add(CodeLine.Assign("localDictionary", Operation.CreateInstance(typeof(mDictionary)))); //temporary variable to hold the keyvalue paid within the loop. } //if (ablnPackInList) //{ // llstCodeLines.Add(CodeLine.CreateVariable(typeof(List<object>), "lst")); // llstCodeLines.Add(CodeLine.Assign("lst", Operation.CreateInstance(typeof(List<object>)))); //} foreach (var lobjChildNode in aobjSingleChildNode.Elements()) { switch (lobjChildNode.Name.LocalName) { case "assign": { string lstrPath = lobjChildNode.Attribute("path").Value; string lstrKey = lobjChildNode.Attribute("name").Value; alstCodeLines.Add(ProcessAssignElement(lstrKey, lstrPath)); } break; case "single": { // string astrKey = lobjChildNode.Attribute("name").Value; //llstCodeLines.Add(CodeLine.CreateVariable(typeof(mDictionary), astrKey)); //llstCodeLines.Add(CodeLine.Assign(astrKey,Operation.CreateInstance(typeof(mDictionary)))); // ProcessSingleElement(lobjChildNode, aobjDFO, alstCodeLines); // AddToDictionary(lobjChildNode.Attribute("name").Value, "lst", "localvar"); } break; } } // if (firstIteration) //if (ablnPackInList) //{ // llstCodeLines.Add(Operation.Invoke(Operation.Variable("lst"), "Add", // Operation.Variable("localvar"))); // llstCodeLines.Add(Operation.Invoke(Operation.Variable(aobjParentContainerKey), // "SetVal", new OperationConst(lstrSingleObjectKey), Operation.Variable("lst"))); //} //else { //ICodeLine lobjCodeLine = Operation.Invoke(Operation.Variable(aobjParentContainerKey), // "SetVal", new OperationConst(lstrSingleObjectKey), Operation.Variable("localvar")); //llstCodeLines.Add(lobjCodeLine); } return(alstCodeLines); }
protected static void ParseXMLAndCreateFunction(object mainobj, string xmlfile) { var idicParamStack = new Dictionary <string, object>(); var lobjXDocument = XDocument.Load(@"..\..\" + xmlfile); var lobjRootNode = lobjXDocument.Root; DynamicFunctionObject dfo = new DynamicFunctionObject() { InputParams = new List <Variable>(), FunctionBody = new List <ICodeLine>() }; try { foreach (var lobjChildNode in lobjRootNode.XPathSelectElements("/xFilter/xFilterGet").Elements()) { switch (lobjChildNode.Name.LocalName) { case "input": { CreateInputVariables(dfo, lobjChildNode); } break; //case "output": // { // string astrRetVal = "data"; // dfo.InputParams.Add(new Variable(typeof(mDictionary), astrRetVal)); // dfo.FunctionReturn = astrRetVal; // } // break; case "single": { var codelines = ProcessSingleElement(lobjChildNode, dfo); codelines.Add(AddToDictionary("data", lobjChildNode.Attribute("name").Value, "localDictionary")); dfo.FunctionBody.AddRange(codelines); } break; case "list": { var codelines = ProcessListElement(lobjChildNode); codelines.Add(AddToDictionary("data", lobjChildNode.Attribute("name").Value, "lst")); //string lstrSingleObjectKey = lobjChildNode.Attribute("name").Value; //ICodeLine lobjCodeLine = Operation.Invoke(Operation.Variable("data"), // "SetVal", new OperationConst(lstrSingleObjectKey), Operation.Variable("lst")); //codelines.Add(lobjCodeLine); dfo.FunctionBody.AddRange(codelines); } break; default: break; } } var fn = Function.Create() .WithParameters(dfo.InputParams.ToArray()) .WithBody(CodeLine.Nop, dfo.FunctionBody.ToArray()) .Returns("data"); var outputExpr = fn.ToExpression(); var outputExprCompiled = outputExpr.Compile().DynamicInvoke(mainobj); var output = Newtonsoft.Json.JsonConvert.SerializeObject(outputExprCompiled, Formatting.Indented, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); Console.WriteLine(output); Console.ReadLine(); } catch (Exception ex) { } }