我有一个控制器一个动作.在这个动作方法中,我有一个async
我调用的方法,就是这样.这是我正在使用的代码:
[HttpGet]
public Task<MyObject> Get()
{
return _task.GetMyObject()
}
Run Code Online (Sandbox Code Playgroud)
这正确地序列化为我期望的JSON.现在我的经理坚持要将签名更改为以下内容:
[HttpGet]
public async Task<IActionResult> Get()
{
var data = await_task.GetMyObject();
return Ok(data);
}
Run Code Online (Sandbox Code Playgroud)
我相信没有理由将代码await
放在控制器中并且可以返回,Task
因为事后没有任何东西取决于结果.除了为此完成的额外代码生成(创建状态机等)之外await
,从WebApi
这些方法的角度来看是否有任何影响?为了澄清,我想知道返回a IActionResult
是否比仅返回更好Task<MyObject>
似乎结果是相同的.
我正在尝试DynamicMethod
并尝试使用它IL
来创建一些对象。我想创建以下非常基本的对象:
new Queue<double>(new List<double>{100});
Run Code Online (Sandbox Code Playgroud)
我已经使用 ILDASM 来查看OpCodes
生成此文件需要什么。这是 ILDASM 告诉我的:
IL_0000: newobj instance void class [System.Collections]System.Collections.Generic.List`1<float64>::.ctor()
IL_0005: dup
IL_0006: ldc.r8 100.
IL_000f: callvirt instance void class [System.Collections]System.Collections.Generic.List`1<float64>::Add(!0)
IL_0014: newobj instance void class [System.Collections]System.Collections.Generic.Queue`1<float64>::.ctor(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!0>)
IL_0019: pop
IL_001a: ret
Run Code Online (Sandbox Code Playgroud)
这就是我正在做的:
var dynMethod = new DynamicMethod("QueueMaker", typeof(Queue<double>), Type.EmptyTypes);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, typeof(List<double>).GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Dup);
ilGen.Emit(OpCodes.Ldc_R8, 100);
ilGen.EmitCall(OpCodes.Callvirt, typeof(List<double>).GetMethod("Add"), null);
ilGen.Emit(OpCodes.Newobj, typeof(Queue<double>).GetConstructor(new[] { typeof(IEnumerable<double>) }));
ilGen.Emit(OpCodes.Pop);
ilGen.Emit(OpCodes.Ret);
var returnFunc = (Func<Queue<double>>)dynMethod.CreateDelegate(typeof(Func<Queue<double>>));
var queue = returnFunc();
Run Code Online (Sandbox Code Playgroud)
我得到异常System.InvalidProgramException: …