简体   繁体   English

如何获取使用顶级语句的 C# 9 程序的反射类型信息?

[英]How do I get the Reflection TypeInfo of a C# 9 program that use Top-level statements?

Assume I have a simple script writing in C# 9 like this:假设我在 C# 9 中有一个简单的脚本,如下所示:

using System;
using System.IO;

// What to put in the ???
var exeFolder = Path.GetDirectoryName(typeof(???).Assembly.Location);

Before, with the full program, we can use the Main class as an "indicator" class.之前,通过完整的程序,我们可以使用Main class 作为“指标”class。 this and this.GetType() is not available because technically it's inside a static method. thisthis.GetType()不可用,因为从技术上讲,它位于 static 方法中。 How do I get it now?我现在如何得到它?


A workaround I thought of while typing the question is Assembly.GetCallingAssembly() :我在输入问题时想到的解决方法是Assembly.GetCallingAssembly()

var exeFolder = Path.GetDirectoryName(Assembly.GetCallingAssembly().Location);

It works for my case, but I can only get the Assembly , not the TypeInfo that in which the code is running.它适用于我的情况,但我只能得到Assembly ,而不是运行代码的TypeInfo

You can also get the assembly using GetEntryAssembly .您还可以使用GetEntryAssembly获取程序集。

Once you have the assembly that your code is in, you can get its EntryPoint , which is the compiler-generated " Main " method.一旦你有了你的代码所在的程序集,你就可以获得它的EntryPoint ,这是编译器生成的“ Main ”方法。 You can then do DeclaringType to get the Type :然后,您可以执行DeclaringType来获取Type

Console.WriteLine(Assembly.GetEntryAssembly().EntryPoint.DeclaringType);

The above should get the compiler-generated " Program " class even if you are not at the top level.即使您不在顶层,上面也应该得到编译器生成的“ Program ” class。

I suggest starting from the method which is executing ( Main ):我建议从正在执行的方法开始( Main ):

TypeInfo result = MethodBase
  .GetCurrentMethod() // Executing method         (e.g. Main)
  .DeclaringType      // Type where it's declared (e.g. Program)
  .GetTypeInfo();    

If you want Type , not TypeInfo drop the last method:如果你想要Type ,而不是TypeInfo删除最后一个方法:

Type result = MethodBase
  .GetCurrentMethod() // Executing method         (e.g. Main)
  .DeclaringType;     // Type where it's declared (e.g. Program)

 

For C# 10 ( See 4th point in the breaking changes ) compiler generates Program class for top-level statement so you can use it:对于 C# 10 ( 请参阅重大更改中的第 4 点)编译器生成Program class 用于顶级语句,以便您可以使用它:

Console.WriteLine(typeof(Program).FullName);

And though currently docs state that:尽管目前的文档state 认为:

Note that the names "Program" and "Main" are used only for illustrations purposes, actual names used by compiler are implementation dependent and neither the type, nor the method can be referenced by name from source code.请注意,名称“Program”和“Main”仅用于说明目的,编译器使用的实际名称取决于实现,并且类型和方法都不能通过源代码中的名称引用。

ASP.NET Core integration testing docs rely on this naming convention for the class. ASP.NET 核心集成测试文档依赖于 class 的命名约定。

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

相关问题 如何在ASP.NET / C#中检查URL的顶级域? - How do I check for a URL's top-level domain in ASP.NET/C#? 有没有办法对 C# 中的顶级语句进行单元测试? - Is there a way to unit test top-level statements in C#? 如何使用顶级 Program.cs 在 C# 9 中处理 {STAThread] - How to handle {STAThread] in C# 9 Using Top-Level Program.cs C# 7.3 中不提供功能“顶级语句”。 请使用 9.0 或更高版本的语言。 戈多问题 - Feature 'top-level statements' is not available in C# 7.3. Please use language version 9.0 or greater. Godot Question 如何在C#中访问类中的顶级语句变量? - How to access the top-level statement variable in a class in C#? 使用 C# 9 顶级语句时,如何在 Main 的 scope 之外添加代码? - How do I add code outside the scope of Main when using C# 9 Top Level Statements? C# 只有一个编译单元可以有顶级语句 - C# Only one compilation unit can have top-level statements C# 中的顶级语句是什么? 它们的用途是什么? - What are top level statements in C#? What is their use? C# asp.net web api 自动创建项目的顶级语句? - C# asp.net web api automatically creates projects with top-level statements type is there any way to avoid that? 为什么不能在 C# 9 中定义顶级扩展方法? - Why can't I define top-level extension methods in C# 9?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM