Util.Run - return an object from sub query?
Options
Is there any way to return an object when using Util.Run?
I keep getting an error:
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:
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
-
I've managed to return.
First query saved toC:\PathToSubquery\Subquery.linq
:
Second query:
string Main(string input)
{
return input;
}
Dumps INPUT.
void Main()
{
var queryExecutor = Util.Run("C:\\PathToSubquery\\Subquery.linq",QueryResultFormat.Text, "INPUT");
((string)queryExecutor.ReturnValue).Dump();
} -
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. -
@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).