0

Converting some code from Swift to C#. There's a need in a base class's static method implementation to know which concrete type that method was called against.

In Swift, when in an instance method, you use self (lowercase) to get that instance. This is equivalent to using this in C#. To get the instance type, you use Self (uppercase) which is the equivalent of C#'s this.GetType().

However, Swift also lets you use lower-cased self from within a static method to get the type the static was called against (as opposed to the type where it was defined.) I'm trying to find if C# has something similar. We're using C# 9 if it matters.

Here's a simple example showing what we're after. Note, this will not compile. It's example only since self isn't known to C#.

public class BaseClass {

    public static void DoSomethingStatic(){

        // Looking for the equivalent of Swift's 'self' in a static method
        // which returns the concrete type, including when called from a derived class
        Debug.WriteLine($"Static method called on the type {self.FullName}.");
    }

    public void DoSomethingInstance(){

        // Swift's 'Self' in an instance method is equivalent to C#'s `this.GetType()`
        Debug.WriteLine($"Instance method called on the type {this.GetType().FullName}.");
    }
}

public class MyClass : BaseClass {}

Now this is easily possible with instance methods thanks to using this.GetType():

var myClass = new MyClass();
myClass.DoSomethingInstance();

Output:

Instance method called on the type MyClass.

But we're hoping to achieve the same thing from a static exposed via the base class, like this...

MyClass.DoSomethingStatic();

Output:

Static method called on the type MyClass.

So is this possible in C#9?

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • 1
    https://sharplab.io/#v2:EYLgtghgzgLgpgJwDQBMQGoA+ABATARgFgAobAZgAI8KBhCgbxIuasuoCFo4aAbaKBiSYtyVfADYqAFgoARAPYBleWDgwAFgEsAdgHNFMCDE0BjABQBKRsWEsWAensUAMvPkBrHbooAzeQgoNOAo4AEcAV00ANwgeOG0YCnkfCkUAd00fGAByAVy4Hh9sih0KCDFJVQ15FFs7CkcKNK0TdSoAdgEgihN5bRMENWCYAE8ABzgkEv6ecJQvJvV4nti4lF8EFTKKFERouHW8OrtsfABOMwASACIDI1MKKvUalZ41pO1ApcDx4OuLADcdQAvkJiCc2DIFMonl4AJLaWAQfpwSzWY4OJzpTI5PKKApFaZlT46JEox5qZ7rTQCMKRGJxBKBeS0ADEuQoAAMNDSAHQAcTUABVfpZORjmKcLjcEWSTMEni8TKsDh8vsNfhR/kDwSxQcR9SRRNQALIjXj8CggCicKDcPhQAT0fUQ6QUE2WQS6+ru80OqC86EqSleO7GcyAkEkYFAA – TheGeneral Nov 26 '20 at 03:38
  • What you're talking about isn't possible. `MyClass.DoSomethingStatic` doesn't actually exist, even though you can call it that way. You're still calling `BaseClass.DoSomethingStatic`. – madreflection Nov 26 '20 at 03:38
  • 1
    Such a shame. It really allows flexibility in API design along with lots of cool covariant-like types of behaviors. One of the many reasons I love Swift so much. – Mark A. Donohoe Nov 26 '20 at 03:38
  • Does this answer your question? [Get derived class type from a base's class static method](https://stackoverflow.com/questions/3064227/get-derived-class-type-from-a-bases-class-static-method) – madreflection Nov 26 '20 at 03:43
  • The generic hack is quite interesting. Though I think it definitely is hackery, I couldn't imaging why i would ever need it. Maybe someone has a use case though. – TheGeneral Nov 26 '20 at 03:46
  • Yeah, that's the work-around we're using. Was hoping not to have to make passing it down explicit because technically there's nothing stopping you from passing down *any* class that matches the constraint. (e.g. Base class A with subclasses B and C. You can call the method on C but passing the type parameter B (or A!) and it won't complain.) BUT... in light of any other solution, that will work. – Mark A. Donohoe Nov 26 '20 at 03:46
  • This may sorta come in a future version of C#. See https://github.com/dotnet/csharplang/issues/1711 which will allow you to define abstract static methods. – Yair Halberstadt Nov 26 '20 at 05:05
  • Not quite what I'm after as I don't want a subclass to override it or anything. I just want to know which actual type it was called on from within the method in the base class. – Mark A. Donohoe Nov 26 '20 at 18:54

0 Answers0