samedi 5 décembre 2020

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:

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. this and this.GetType() is not available because technically it's inside a static method. How do I get it now?


A workaround I thought of while typing the question is 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.





Aucun commentaire:

Enregistrer un commentaire