0

I have a class:

public class StateMachine<TTag> where TTag : Enum { ... }

Which logs using Log that accepts as first argument string tag. I use nameof with StateMachine<TTag> as an argument:

_logger.Log(nameof(StateMachine<TTag>), ...)

In my app I have instances of StateMachine<TTag>, like StateMachine<AppStateTag>, etc.

And I want my StateMachine to log like this:

StateMachine<AppStateTag> ...

But instead I get:

StateMachine ...

Do I need to get weird like this?

${nameof(StateMachine)}<{nameof(TTag)}>

Is there any simple way of doing this?

stroibot
  • 808
  • 1
  • 11
  • 25
  • @PaulKaram not exactly, but I've managed to get something out of those answers – stroibot Feb 16 '23 at 23:29
  • You may be interested in [Get generic type name in good format](https://stackoverflow.com/questions/1533115/get-generictype-name-in-good-format-using-reflection-on-c-sharp) – John Wu Feb 16 '23 at 23:43

1 Answers1

0

So, ok, using answers from here, I've managed to frankenstein this:

$"{nameof(StateMachine<TTag>)}<{typeof(TTag).Name}>"

If anyone can suggest a better way of doing this, it would be very much appreciated.

stroibot
  • 808
  • 1
  • 11
  • 25