Home

Util.Run - return an object from sub query?

Is there any way to return an object when using Util.Run?

I keep getting an error: Object of type 'System.Object[]' cannot be converted to type 'System.String[]
I've tried returning a dynamic, an object, and a string, but all fail with the same error.
My subQuery that is being called from another query with Util.Run has signature:
string Main(string[] args)

Comments

  • edited November 2018
    I've managed to return.

    First query saved to C:\PathToSubquery\Subquery.linq:

    string Main(string input)
    {
    return input;
    }
    Second query:

    void Main()
    {
    var queryExecutor = Util.Run("C:\\PathToSubquery\\Subquery.linq",QueryResultFormat.Text, "INPUT");
    ((string)queryExecutor.ReturnValue).Dump();
    }
    Dumps INPUT.
  • Returning to this, I still can't successfully return an object. My sub-query signature looks like this:
    async Task<SomeObject> Main()
    and returns an object (class is defined in My Extensions).

    My second query calls the sub-query like this:
    var qryRunner = Util.Run("MySubQuery.linq", QueryResultFormat.Text, null);
    SomeObject result = qryRunner.ReturnValue;


    The return value is always null.
  • edited May 2019
    @timohayes AFAIK in order for linqpad to be able to return the object through Util.Run like that, it needs to be marked serializable. I was able to reproduce the behavior you're seeing when it didn't have [Serializable], and once I added that attribute it worked fine.

    @JoeAlbahari is this true? If it is, could there be a warning added when Main returns a non-void/non-serializable type? Presumably Main doing so is specifically so other scripts could call it and would hit this issue AFAICT
  • Yes, I'm looking into making some upgrades to this in LINQPad 6.

    Making the type serializable will work only if the type is visible to both scripts (defined in My Extensions or a common assembly).
Sign In or Register to comment.