2

In Android, if I need to get name of current class, i can do something like:

private final _TAG = DummyActivity.this.getClass().getSimpleName();

this would return "DummyActivity"

I want to do same in C# WPF app code-behind. How do I get the name of current class?

this.GetType().Name; //this works only on instances of a class 

Looks like the only option is to hard-code it in C# like this:

private const string _TAG = "DummyWindow";
pixel
  • 9,653
  • 16
  • 82
  • 149
  • 2
    See this: http://stackoverflow.com/questions/2081612/net-determine-the-type-of-this-class-in-its-static-method – Yacoub Massad Aug 08 '16 at 21:36
  • Note that the marked duplicate's question is more specific than this, asking for information not available at compile time. However, there are numerous answers there, several of which apply here as well. It's important to note that while the marked duplicate question posits that in a static base class method, they can tell whether they are being called via a derived class, this is actually just not true. The best you can do is find the type of the declaring class, which is what's being asked here, and there are answers in the duplicate addressing that. – Peter Duniho Aug 08 '16 at 22:00

6 Answers6

6

You can do it like this in C#:

private static string _TAG = MethodBase.GetCurrentMethod().DeclaringType.Name;

This will work because initializing this field actually happens in the static constructor. I.e., MethodBase.GetCurrentMethod() returns the static constructor of the class.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Much appreciated. This works, too. But I accepted H.B.'s answer as he answered first and his solution works, too. – pixel Aug 08 '16 at 22:00
5

You can use either the following:

1.) typeof(T).Name (Vanilla .NET way)

2.) nameof(T) (C#6 only)

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
4

An instance would exactly be "current", the concept does not make much sense otherwise. If you just want the name of a known type that would be typeof(Class).Name.

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • but result of typeof(Class).Name cannot be assigned to a constant – pixel Aug 08 '16 at 21:39
  • @dbnex14: Why in the world would you need to assign it to a constant? Well, as you don't use the `FullName` you could use `nameof(Class)` i suppose. – H.B. Aug 08 '16 at 21:40
  • 1
    @dbnex14: Also, you can replace `const` with `readonly` and you will be able to do much more with it. (It will no longer need to be compile time constant.) – H.B. Aug 08 '16 at 21:40
2

Try

typeof(DummyActivity).Name
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
1
nameof(DummyActivity) == new DummyActivity().GetType().Name
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
0

Exact same statement in C# 6:

private static readonly string _TAG = nameof(DummyActivity);
CoolBots
  • 4,770
  • 2
  • 16
  • 30
  • 1
    This question is a duplicate of http://stackoverflow.com/questions/2081612/net-determine-the-type-of-this-class-in-its-static-method. Assuming there's no answer there yet mentioning the `nameof` operator, you should post your answer there and vote-to-close this question as a duplicate of that other question, rather than putting your answer here. Frankly, your answer would get a lot more visibility in the other question anyway; as such a commonly-asked duplicate, it is highly up-voted and will be found via search much more easily. – Peter Duniho Aug 08 '16 at 21:47
  • @PeterDuniho thanks, will do! – CoolBots Aug 08 '16 at 21:48
  • @PeterDuniho: That question mentions that the type is unknown though, and `nameof` only works with known types. – H.B. Aug 08 '16 at 21:49
  • @PeterDuniho I agree with H.B., my answer doesn't apply on the link you posted. It's *sort of* duplicate... – CoolBots Aug 08 '16 at 21:51
  • @H.B.: it's true that the other question has broader answers than required by the question. I still think it's a duplicate though. It's basically the same question. In some cases, compile-time semantics are sufficient. The other question does include answers addressing that. – Peter Duniho Aug 08 '16 at 21:51
  • @PeterDuniho I dont think it is a duplicate, rather a "sort of duplicate" as CoolBots noticed. Anyways, I modified my question as it is not related to static classes but rather WPF code-behind classes (partial classes). Anyways, I accepted H.B.'s answer. Much appreciated. – pixel Aug 08 '16 at 21:59
  • @dbnex14: IMHO if H.B.s answer suits your needs, http://stackoverflow.com/a/38838900 is a better answer as it covers the C# 6.0 syntax option also (as does CoolBots answer here). IMHO, the marked duplicate deserves some better answers, elaborating on Jon Skeet's answer as well as updating for C# 6. But it's still IMHO a duplicate. Opinions vary, of course. – Peter Duniho Aug 08 '16 at 22:03