I have two classes like this:
public class test1: BaseClass
{
public test1() : base()
{
}
...
public class BaseClass
{
public BaseClass(
[CallerMemberName]string membername ="",
[CallerFilePath] string path = "")
{
var sf = new System.Diagnostics.StackTrace(1).GetFrame(0);
}
If I specify test1 ctor with call to base - I get membername and path initialized properly, but if not - compiler generates default constructor call, and membername and path are both empty.
Is this a bug or a feature ?
(Visual Studio 2019 16.11.8, net core 3.1 or net 5.0).
I've googled "c# compiler github" and ended up on https://github.com/dotnet/roslyn
After searching by
CallerMemberName- I've managed to find answer for this question:https://github.com/dotnet/roslyn/issues/53757
It mentions that this is done by design.
But quickly scanning through the tickets lead me of thinking - "can I use attributes for same purpose ?"
Since what I was doing was unit testing - I've recoded my own attribute for that purpose:
TestAttribute=>FactAttributeand resolved from NUnit method info that attribute, and got back file path and method name.This allows to determine from which file and from which method call was directed from.