- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 Stephan Cleary 最近关于 Async Console Apps on .NET CoreCLR 的博文中他向我们展示了在 CoreCLR(目前在 Visual Studio 2015,CTP6 上运行)中,入口点“Main”实际上可以标记为 async Task
,正确编译并实际运行:
public class Program
{
public async Task Main(string[] args)
{
Console.WriteLine("Hello World");
await Task.Delay(TimeSpan.FromSeconds(1));
Console.WriteLine("Still here!");
Console.ReadLine();
}
}
给出以下输出:
ASP.NET 团队的博客文章 A Deep Dive into the ASP.NET 5 Runtime 加强了这一点:
In addition to a static
Program.Main
entry point, the KRE supports instance-based entry points. You can even make the main entry point asynchronous and return a Task. By having the main entry point be an instance method, you can have services injected into your application by the runtime environment.
我们知道到目前为止,An entry point cannot be marked with the 'async' modifier .那么,在新的 CoreCLR 运行时中,这实际上是如何实现的呢?
最佳答案
深入 CoreCLR 运行时的源代码,我们可以看到一个名为 RuntimeBootstrapper
的静态类,它负责调用我们的入口点:
public static int Execute(string[] args)
{
// If we're a console host then print exceptions to stderr
var printExceptionsToStdError = Environment.GetEnvironmentVariable(EnvironmentNames.ConsoleHost) == "1";
try
{
return ExecuteAsync(args).GetAwaiter().GetResult();
}
catch (Exception ex)
{
if (printExceptionsToStdError)
{
PrintErrors(ex);
return 1;
}
throw;
}
}
我们可以看到在内部,它调用了 ExecuteAsync(args).GetAwaiter().GetResult();
,这在语义上等同于调用 Task.Result
,除了我们收到的不是包装的 AggregationException
,而是未包装的异常。
理解这一点很重要,因为没有“黑魔法”可以解释它是如何发生的。对于当前版本的 CoreCLR 运行时,该方法被允许标记为 async Task
,因为它被运行时阻止在调用链的更高层。
深入了解 ExecuteAsync
,我们将看到它最终调用:
return bootstrapper.RunAsync(app.RemainingArguments);
When looking inside ,我们看到实际的 MethodInfo
调用我们的入口点:
public static Task<int> Execute(Assembly assembly, string[] args, IServiceProvider serviceProvider)
{
object instance;
MethodInfo entryPoint;
if (!TryGetEntryPoint(assembly, serviceProvider, out instance, out entryPoint))
{
return Task.FromResult(-1);
}
object result = null;
var parameters = entryPoint.GetParameters();
if (parameters.Length == 0)
{
result = entryPoint.Invoke(instance, null);
}
else if (parameters.Length == 1)
{
result = entryPoint.Invoke(instance, new object[] { args });
}
if (result is int)
{
return Task.FromResult((int)result);
}
if (result is Task<int>)
{
return (Task<int>)result;
}
if (result is Task)
{
return ((Task)result).ContinueWith(t =>
{
return 0;
});
}
return Task.FromResult(0);
}
关于c# - 入口点可以在 CoreCLR 上用 'async' 修饰符标记吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28938582/
SQL FMDB 的修饰符在哪里描述?特别是,如何在要插入的字符串中放置引号。比如我想将名称“Mac's Place”插入数据库?谢谢 最佳答案 如果在 SQL 中使用 executeUpdate 和
我读过 article在 MSDN 上。它解释了为什么“in”应该只与自定义只读结构一起使用,否则会有性能损失。但是,我不太明白如何对原始类型使用“in”。由于 C# 中的所有内置值类型都是不可变的,
“__printflike__ 修饰符”到底是什么?这个词是什么意思? 最佳答案 据推测,它会告诉编译器您正在使用的函数接受格式为 [anything, ] format, ... 的参数,其中 fo
我在 VS2010 上使用 ReSharper 插件,我正在生成一个接口(interface)方法。ReSharper 在参数名称上放置一个 @。那是做什么用的? int Count(Func @wh
我在写小偷中用了大量的正则,所以在接下来的版本中要逐步减少正则的应用 在PHP正则表达式中需要转义的字符如下: $^*()+={}[]|/:<>.?'" 注意:perl风
http://msdn.microsoft.com/en-us/library/435f1dw2.aspx public class Base { public string Field; }
根据scala-wartremover静态分析工具我必须将“final”放在我创建的每个案例类前面:错误消息显示“案例类必须是最终的”。 根据scapegoat (Scala 的另一个静态分析工具)相
在 tcsh 中,我可以通过以下方式从路径末尾提取第二个路径元素 cd /some/long/directory/structure/path/ set x=`pwd` echo ${x:h:h:t}
我找到了很多 SwiftUI 教程,其中修饰符 .resizable() 用于图像。它似乎不再适用于带有 Swift 5 的 Xcode 13.3.1。 导入了 SwiftUI。如果您自己键入整个修饰
我已经设置好了音频播放器 除了步进器的当前功能外,我还想为 onIncrement 和 onDecrement 播放单独的声音。 这个项目使用核心数据来持久化。 $estimatorData.qty
来自 linux 内核脚本/mod/modpost.c: static int is_vmlinux(const char *modname) { const char *myname;
我刚刚遇到了一个BNF Grammar for JAVA 。其中,“修饰符”有一个称为“threadsafe”的终结符号。但是,我以前从未见过它,也无法在《Java 语言规范》Java SE 7 版
我创建了一个脚本,它将我组中的所有帖子嵌入到我的网站上。为此,我必须知道所有可能的修饰符“附件”类型。 现在我知道,那个类型可以是: 分享, video_share_youtube。 但我不知道在哪里
在 tcsh 中,我可以通过以下方式从路径末尾提取第二个路径元素 cd /some/long/directory/structure/path/ set x=`pwd` echo ${x:h:h:t}
我找到了很多 SwiftUI 教程,其中修饰符 .resizable() 用于图像。它似乎不再适用于带有 Swift 5 的 Xcode 13.3.1。 导入了 SwiftUI。如果您自己键入整个修饰
我正在编写一个代表一些简单几何形状的 Java 类。 最上面abstract类(它本身是包私有(private))我已经声明了需要从同一包中的子类访问的属性。 如果我将属性声明为 final在 Abs
我已经设置好了音频播放器 除了步进器的当前功能外,我还想为 onIncrement 和 onDecrement 播放单独的声音。 这个项目使用核心数据来持久化。 $estimatorData.qty
明确地说,我是 不是 询问如何使用 ViewModifier 协议(protocol)来创建一个带有 body 函数的结构,然后可以使用它来修改 View 。这个问题有点不同。 我正在尝试创建 Nav
我有一个内部类,用于存储我用于游戏的控件的信息,现在我想在其中存储一个静态 ArrayList,其中包含所有控件的名称。但我收到此错误:“修饰符 static 只允许在常量变量声明中” private
public class This_testing { int x,y; public This_testing(int x,int y){ //Why modifier void
我是一名优秀的程序员,十分优秀!