c# - Exception (sometimes) is thrown when awaiting a method with dynamic argument -
i have following code:
string commandtext = await _client .getcommandtextasync("products", x.productname == "chai");
the second parameter (x.productname == "chai") contains dynamic clause (x.productname), resulting expression dynamic. when code executed on .net 4.0, throws following exception:
system.invalidcastexception unable cast object of type 'system.runtime.compilerservices.taskawaiter`1[system.string]' type 'system.runtime.compilerservices.inotifycompletion'.
the exception not thrown if explicitly case method result task:
string commandtext = await (task<string>)_client .getcommandtextasync("products", x.productname == "chai");
is there more elegant way resolve problem (without casting every single line of code awaits dynamic result), or known problem using tpl on .net 4.0.
i haven't experienced on .net 4.5.
here theory:
according taskawaiter definition:
[hostprotectionattribute(securityaction.linkdemand, synchronization = true, externalthreading = true)] public struct taskawaiter : icriticalnotifycompletion, inotifycompletion
it seems taskawaiter inotifycompletion. said have dynamic clause in code. ms states dynamic objects behaves object. casting required in code handled run-time or compiler.
you stated platform xamarin ios. possibly utilizing hostprotectionattribute example block usage of classes or etc.
the taskawaiter implementation marked securityaction.linkdemand , again if check msdn says:
... linkdemand (do not use in .net framework 4) ...
so conclusion is: platform code running, lacking security implementations required host protection , methods not invoked (while security not working properly) casting 1 of "secure" operation type of runtime casting fails.
if explicitly cast did, there no problem because compiler not add "buggy" code.
Comments
Post a Comment