Home

receiving strongly-typed results from other scripts?

With recent LINQPad versions (running queries in separate processes, debugger support), I'm guessing this might be easier than it would have been in the past, but could be wildly wrong. :)

I'm trying to avoid having 'My Extensions' and the plugins folder as a bit of a dumping ground that collects any bits of shared logic I want for multiple scripts. What'd I'd really like to have instead is something akin to require('foo') from node/npm or ES6 or the like.

Currently if I have another script that I want to run and get the results back to the calling script, I end up having to define the "data transfer" class in both scripts, have the one write them out, have the caller Util.Run it with csv output, then use something like the CsvHelper nuget package in order to 'deserialize' it back to the strong types. Another option would be having the called script write out JSON, call it with text format, then deserialize that, but it's still the same problem.

I'd love to be able to run something like:
var resultsFromOtherScript = Util.Run>("OtherScript.linq");
Is this something that either there's already existing support for (and I'm just missing it) or could be added in the future?

Thanks!

Comments

  • well, the preview showed the generics fine but the post didn't.

    It was meant to be Util.Run[List[MyResultClass]](...)
  • Nevermind - trying to repro the formatting issue, it looks like the preview also lost the generics, I just need to remember to not use angle brackets here :)
  • Looks like this is already pretty simple with the existing support since (as the page says) it returns JSON by default (the text format), so JsonConvert.DeserializeObject in the caller is all that's really needed.

    Sorry for the noise. :)
  • You can also have the query that you call return a value, like this:
    
    DateTime Main ()
    {
        return DateTime.Now;
    }
    and then get to it from another query as follows:

    (DateTime) Util.Compile ("myquery.linq").Run(QueryResultFormat.Text).ReturnValue

    The object returned can be any serializable type that's accessible to both queries, (including a type defined in My Extensions, or a type defined in the plugins folder, although this may not help you if the aim is to avoid having shared types defined here).
  • Thanks, Joe, you're absolutely right. Definitely simpler than having the unnecessary JSON intermediate step!
Sign In or Register to comment.