Home

Util.Compile and Util.Run questions

edited September 2017
Before I ask my questions I would like to state my end goal. I would like to do the following

1)Call a Subscript and output its Dump() in the main window.
2) Return a value from a Subscript.
3) Input a highly type object into a subscript.

I have manage to achieve 3 and sort of achieve 1 and 2

I can achieve 1 by using the Util.Run() method and also pass in string argument which is pretty nice. I also get the subscript .Dump() into my Results view window. Pretty cool and useful.

I can achieve 3 and 2 by using the following code and having the TestObject defined in MyExtensions script.

Parent Script
void Main() { Directory.SetCurrentDirectory (Path.GetDirectoryName (Util.CurrentQueryPath)); var details = new TestObject { /* whatever I want */ } var compiledQuery = Util.Compile(@".\SubTestQuery.linq", false); var retVal = compiledQuery.Run(QueryResultFormat.Html, details); retVal.ReturnValue.Dump();
}

SubScript with working return value
int Main(EnvironmentDetails input) { "Test Dump".Dump(); return 5; }
Using this method though I do not get my "Test Dump".Dump() in my results window in the parent script.



Once I change the subscript return value to async Task my ReturnValue becomes null. I still do not get the "Test Dump".Dump() in my parent script results window either which is expected from the result of the above test case.
async Task<int> Main(EnvironmentDetails input) { "Test Dump".Dump(); return 5; }


Is it possible to achieve all 3 ( well I guess 4) goals ? I have ran these scripts in the release version of linqpad 5 and the Download LINQPad 5.23.05 (Any CPU) beta.

Comments

  • edited September 2017
    To get what was dumped by the your subscript, you call AsString():
    var compiledQuery = Util.Compile ("...");
    var retVal = compiledQuery.Run (QueryResultFormat.Html, ...);
    retVal.AsString().Dump();        // This is what was dumped
    retVal.ReturnValue.Dump();
    
  • awesome that works great and looks the same once you wrap the dumped output into
    Util.RawHtml(retVal.AsString()).Dump();
    I must have thought I was looking at the contents of the linqpad document instead of the dumped output in raw html.

    The subquery with a Task returns null though and I have been playing around with the following

    Parent Query
    void Main()
    {
    	Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath));
    	var details = new TestObject {   test = "Hey I'm a test" };
    
    	var compiledQuery = Util.Compile(@".\SubTestQuery.linq", false);
    	var retVal = compiledQuery.Run(QueryResultFormat.Html, details);
    	
    	Util.RawHtml(retVal.AsString()).Dump();
    	retVal.ReturnValue.Dump();
    
    	retVal.ReturnValueAsync.Status.Dump("Status");
    	retVal.ReturnValueAsync.Wait();
    	retVal.ReturnValueAsync.Result.Dump("Result");
    
    }

    SubQuery
    async Task<int> Main(TestObject input)
    {
    	await Task.Run(delegate()
    	{
    			"Action".Dump("Action Action Action");
    	});
    	
    	input.test.Dump("In Sub Script");
    	return 22;
    }
    

    but honestly I think should reformulated my approach on my designs instead of getting this to work. I could always switch to a non Task subquery and get my results from that.




Sign In or Register to comment.