c# - Switch on Type with Dictionary on Func and async / await -
i'm trying write piece of code doesn't compile (stuff deleted/simplifiied):
private async task<patientchartdata> getdomainlistingsummarymeta<t>(string phn) t : patientchartbase, new() { var getlistingsummarytypeswitch = new dictionary<type, func<????>> { { typeof(documents), async () => await this.getdocuments(phn) }, { typeof(encounters), async () => await this.getencounters(phn) }, { typeof(labs), async () => await this.getlabs(phn) }, }; task t = new task(async () => { // code without switch, e.g. // await this.getdocuments(phn); // type switch, want this: await getlistingsummarytypeswitch(typeof(t))(); }); t.start(); // notice, not waiting task complete }
getdocuments returns task, getencounters returns task, etc. however, in current design, don't care response call them in background thread have side-effects (e.g. putting them in database/cache, etc.) please ignore design issues being background threads.
my question syntax on how make work.
you want use type indexer not argument?
await getlistingsummarytypeswitch[typeof(t)]();
regarding func<???>
: must return awaitable, otherwise cannot use await
in front of switch. use func<task>
or generic func<task<resulttype>>
.
i won't discuss other flaws (like wrapping in task) discussed in comments-section of question, because said simplified it.
Comments
Post a Comment