0

Consider the following code:

public class foo() {
  public foo() {
    ContentManager.DoStuff<foo>();
  }
}

Rather than passing in <foo> as the generic type, is there a way to programmatically determine the current class and pass in that? I tried ContentManager.DoStuff<typeof(this)>(), but that obviously doesn't work.

Is there a way?

AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • Does it have an overload that takes a `Type` Parameter? – Fildor Feb 02 '23 at 17:33
  • 1
    while the duplicate i've tagged should solve your problem, keep in mind that reflection is expensive and could slow down your application if used improperly. it's generally better to use reflection only where needed. – Franz Gleichmann Feb 02 '23 at 17:35
  • @FranzGleichmann yeah, the reflection workaround is pretty ugly. – AngryHacker Feb 02 '23 at 17:38
  • question is blocked as dup. so just commenting, if you introduce parameter in `DoStuff(T t)`, then you can just call `DoStuff(this)` and compiler will infer it automatically as `DoStuff(this)` – YK1 Feb 02 '23 at 17:43
  • @YK1 That is actually a great idea. Thank you. – AngryHacker Feb 02 '23 at 18:35

0 Answers0