title: Get calling class name from abstract class in C#
body: <p>I recently started learning C#, I'm programming in various languages like PHP, JavaScript, Go, Rust, Elixir, a bit of Java and Python.</p>
  
  <p>I always was fascinated by C# and I decided now to give it a go.</p>
  
  <p>So far so good, my process of learning a new language is by trying to replicate a variety of things from other languages, especially from PHP where I'm most strong.</p>
  
  <p><strong>TLTR:</strong>
  How I could implement this in C#?</p>
  
  <pre><code>&lt;?php
  declare(strict_types=1);
  
  abstract class Foo
  {
      public static function className(): string
      {
          return static::class;
      }
  }
  
  final class Bar extends Foo
  {
  }
  
  echo Bar::className() . PHP_EOL;
  </code></pre>
  
  <p>The code above returns "Bar" instead of "Foo" because we are calling the name of the static, where static is the class that we are calling the className method from.</p>
  
  <p>In C# I managed to do that:</p>
  
  <pre><code>using System;
  using System.Reflection;
  
  namespace LearningConsole
  {
      abstract class Foo
      {
          public static string ClassName()
          {
              return MethodBase.GetCurrentMethod().DeclaringType.Name;
          }
      }
  
      class Bar : Foo
      {
  
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine(Bar.ClassName());
          }
      }
  }
  </code></pre>
  
  <p>But this returns the "Foo" class name instead of "Bar", is there a way to get the name of the class that invoking the ClassName method?</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>EDIT:</strong>
  The reason I'm using a static method is that I'm trying to accomplish something similar in C# like this in PHP:</p>
  
  <pre><code>&lt;?php
  declare(strict_types=1);
  
  interface Dummy {}
  
  abstract class Base implements Dummy
  {
      public static function create(...$args): Dummy
      {
          $callingClass = static::class;
          return new $callingClass(...$args);
      }
  }
  
  final class Bar extends Base
  {
      protected function __construct() {}
  }
  
  final class Foo extends Base
  {
      protected function __construct() {}
  }
  
  $foo = Foo::create();
  $bar = Bar::create();
  echo get_class($foo) . PHP_EOL.
       get_class($bar) . PHP_EOL;
  </code></pre>
  
  <p>The above will output:
  Foo
  Bar</p>
  
  <p>Maybe in C# that kind of "magic" is considered bad practice, but it could be handy for various occasions.</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT 2:</strong>
  That C# is not working (obviously), but it is my understanding of what could be a good compromise for achieving somehow the above example:</p>
  
  <pre><code>using System;
  using System.Reflection;
  
  namespace LearningConsole
  {
      interface Dummy
      {
      }
  
      abstract class Base : Dummy
      {
          public static Dummy Create(Dummy obj, params dynamic[] args)
          {
              var constructor = typeof(obj).GetConstructor();
              constructor.invoke(null, args);
          }
      }
  
      class Foo : Base
      {
      }
  
      class Bar : Base
      {
      }
  
      class Program
      {
          static void Main(string[] args)
          {
              var foo = Base.Create(Foo, null);
              var bar = Base.Create(Bar, null);
          }
      }
  }
  </code></pre>
  
  <p>Thanks!</p>