Get current assembly name (as compile-time constant) #6041
-
Normally getting an assembly name would be a library, but I'm arguing it should be a language feature. I regularly find myself needing to get the name of the current assembly (often for logging and /or auditing purposes), which currently requires doing something along the lines of this: var assemblyName = GetType().Assembly.GetName().Name; or var assemblyName = typeof(Program).Assembly.GetName().Name; both of which feel wrong; I'm getting to the assembly by using reflection on some random type that happens to be defined in that assembly. It's made even more difficult if you want to use top-level statements where there might not be any type to reference. Surely the compiler knows the assembly name of the code it's currently compiling? Assuming it has one, of course. Isn't there some way we can access the name of the current assembly, preferably in a constant way? Perhaps there can be a pseudo constant, such as [SomeAttribute(Assembly.Current.Name)]
public void Foo()
{} Bonus: It also feels like a public class Logger
{
public Logger([CallerAssemblyName] string callingAssembly = null)
{
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
There is also
Is that necessarily true? I don't know if the situation has changed but in .NET Framework it was possible that the assembly would be linked to a separate manifest which could have a completely different name.
I'm curious as to what attributes would require the current assembly name? Any code written to scan such attributes would be using reflection and would already know the assembly based on where they reflected over that attribute. Shrug, seems at worst to be a very minor nuisance. |
Beta Was this translation helpful? Give feedback.
-
Duplicate of #4984. |
Beta Was this translation helpful? Give feedback.
Duplicate of #4984.