简体   繁体   English

.Net Application Loader的命令行解析器

[英].Net Application Loader's Command Line Parser

I was recently working on a generic parser and gotten to the point of folding ( [...] / {...} ) management I was interested to know how the .Net args parser works (The parser that is essentially populating the args array in the application entry point). 我最近正在研究一个通用的解析器并且已经达到折叠( [...] / {...} )管理的程度我很想知道.Net args解析器是如何工作的(解析器基本上填充了args应用程序入口点中的数组)。

I started looking around the CLR ( mscorlib.dll ) but didn't find what is was looking for, so my question is what mechanism is used to parse these arguments, if it's a CLR Type which one it is, or if it's an un-managed P/Invoke call where it's being declared. 我开始环顾CLRmscorlib.dll ),但没有找到正在寻找的东西,所以我的问题是用什么机制来解析这些参数,如果它是一个CLR类型,或者它是一个un - 被管理的P / Invoke调用它被声明的位置。

This is the Call Stack inside the main function of a Console application: 这是Console应用程序main功能中的Call Stack:

Program.Main(string[] args = {string[0]}) Line 18 + 0x5 bytes   C#
mscoreei.dll!6f79f5a3()     
mscoree.dll!70bc7f16()  
mscoree.dll!70bc4de3()  
kernel32.dll!76f03677()     
ntdll.dll!77c09d72()    
ntdll.dll!77c09d45()    

At least main(string[]) is directly called from the native code. 至少main(string[])是从本机代码直接调用的。 I'm still not sure that the parsing also took place inside the native code, maybe it is a managed function and it was called before main . 我仍然不确定解析是否也发生在本机代码中,也许它是一个托管函数,它在main之前被调用。

Microsoft recently released the source code of the .NET Compiler Platform , so we can just take a look! 微软最近发布了.NET编译器平台的源代码,所以我们可以看一看!

It contains a class Microsoft.CodeAnalysis.CommandLineParser with a SplitCommandLineIntoArguments method, which is described as follows: 它包含一个带有SplitCommandLineIntoArguments方法的Microsoft.CodeAnalysis.CommandLineParser类,其描述如下:

/// <summary>
/// Split a command line by the same rules as Main would get the commands.
/// </summary>
/// <remarks>
/// Rules for command line parsing, according to MSDN:
/// ...
/// </remarks>

So here's a complete C# implementation of the args parser, conveniently available under the Apache License: 所以这里是args解析器的完整C#实现,可以在Apache License下方便地使用:

The .NET framework probably calls into the Windows API. .NET框架可能会调用Windows API。

There are the functions: GetCommandLine ( http://msdn.microsoft.com/en-us/library/ms683156.aspx ) and CommandLineToArgvW ( http://msdn.microsoft.com/en-us/library/ms647232.aspx ) which can be used to get and parse the command line. 有一些功能:GetCommandLine( http://msdn.microsoft.com/en-us/library/ms683156.aspx )和CommandLineToArgvW( http://msdn.microsoft.com/en-us/library/ms647232.aspx )可用于获取和解析命令行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM