Here's an example of what I want to accomplish:
abstract class DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You asked the abstract class to work. Too bad.");
}
}
class InspireMe : DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You are amazing.");
}
}
class InsultMe : DoSomething
{
static void DoWhateverItIsThatIDo()
{
Console.WriteLine("You aren't worth it.");
}
}
class Program
{
static void Main()
{
DoSomething worker = InsultMe;
worker.DoWhateverItIsThatIDo();
worker = InspireMe;
worker.DoWhateverItIsThatIDo();
}
}
I'm coming from a Python background where a method itself can be a variable, which can then be called. It looks like C# doesn't have this concept, but I'm trying to accomplish something similar.
The idea is I want a variable that can be of an abstract type, so that many different kinds of subtypes could exist within it. All of these subtypes would have a certain method. I want to be able to assign any of those subtypes to this variable of the abstract type and then call the static methods that exist in the subtypes.
In C# terminology, I want to be able to assign the class to a variable, not an instance of the class, and then call that class's static methods.
A factory sounds like it might be on the right path, but I'm not sure how the factory itself would be able to generate these references to the class (rather than creating instances).
I could rework this to use instances, but suppose I wanted to have static methods that generate classes of each type, all of which still inherit from the base type.
I feel like there is most likely a way to do this - could someone advise please?