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.
For C# 10 (See 4th point in the breaking changes) compiler generates Program
class for top-level statements so you can use it:
Console.WriteLine(typeof(Program).FullName);
And though original (C# 9) docs state that:
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.
ASP.NET Core integration testing docs rely on this naming convention for the class.