How to handle nameof(this) to report class name

江枫思渺然 提交于 2021-02-06 09:55:44

问题


I'd like to use the following C#6 code

var joe = new Self();
Console.WriteLine(joe);

... and get the following output:

joe

The following attempt

class Self {
  public string Name { get; set; } = nameof(this);
  public override string ToString() {
    return Name;
  }
}

fails as nameof cannot be applied to this. Is it there a workaround for this problem?

EDIT. The scenario I'm working with assures that no two references point to the same Self object.


回答1:


No, nameof is designed to refer to the compile-time name of the member you're referring to. If you want an object to have a Name property as part of its state, that is independent of how you get to the Name property - as Frédéric Hamidi says, there could be multiple variables (or none) referring to the same object. Basically you need to differentiate between an object and a variable which happens to refer to that object.

However, if you have a constructor to specify the name, you could then use a couple of tricks to make it easier to get the right name:

class Self
{
    public string Name { get; }

    public Self([CallerMemberName] string name = null)
    {
        this.Name = name;
    }
}

Then:

class Foo
{
    private Self me = new Self(); // Equivalent to new Self("me")

    public void SomeMethod()
    {
        // Can't use the default here, as it would be "SomeMethod".
        // But we can use nameof...
        var joe = new Self(nameof(joe));
    }
}



回答2:


You can simply use nameof on the variable itself:

Console.WriteLine(nameof(joe));

Here's a working example using the current Roslyn version




回答3:


Maybe you can use the following method:

    class Self
    {
       public override string ToString()
       {
            return this.GetType().Name;
       }
    }



回答4:


The idea for nameof is to make things type safe for specifying program elements during runtime but with compile time type safety checking.

One should atomize what one wants to display. For example in my error messages I include the pertinent information of the class name and the method as such and its checked, so if I change any of the names they are caught as a compile time error:

class Operation
{
  public void Execute()
  { 
    try { ... }
    catch (Exception ex)
    {
    Console.Writeline($"{nameof(Operation)}.{nameof(Execute)} has encountered exception:{Environment.NewLine}{Environment.NewLine}{ex.Message}" );
    }
   }
}

Output

Operation.Excecute has exception:
...

With that said you should override ToString() and report the class name as such

public override string ToString() { return nameof(Self); } 



回答5:


I usually create an internal constant for it when dealing with long class names:

private const string SomeConst = nameof(Self);

Then you can use that in your code:

Console.WriteLine(SomeConst);


来源:https://stackoverflow.com/questions/27837867/how-to-handle-nameofthis-to-report-class-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!