I find myself writing some C# code while still thinking in Python. One thing in particular caught me out … it seems, at first, that C# doesn’t have first class classes. This is annoying, because I’d started writing some device driver classes where each class is a type of device, and instances represent the individual devices themselves. And I wanted to construct a list of these classes, and call a “probe” classmethod on each of them to ask the class to go search out any devices which were available. In Python, this would look like :
device_classes = (FooDevice, BarDevice, BazDevice)
for device_class in device_classes:
device_class.probe()
See? The classes are being treated just like any other variable, because they are, they’re just instances of type ‘classobj’ . But the equivalent doesn’t work in C# — doing this:
Type[] DeviceClasses = {
FooDevice,
BarDevice,
BazDevice
};
… complains that “‘FooDevice’ is a ‘type’ but is used like a ‘variable’”. At first it seemed that C# didn’t have first class classes, and indeed a few web searches came up empty handed.
Thankfully after a bit more exploration it turns out that all that is needed is some syntactic nastiness … namely, typeof(), GetMethod() and Invoke() (Passing “null” to Invoke works for static methods):
Type[] DeviceClasses = {
typeof(FooDevice),
typeof(BarDevice),
typeof(BazDevice)
};
foreach (Type dct in DeviceClasses) {
dct.GetMethod("Probe").Invoke(null, new object[] {} );
}
Now, quite why a shiny new programming language has to get saddled with such godawful syntax is a bit beyond me, but so it goes.
As always, this is lovingly documented in MSDN, in such a way that the answer is clear so long as you already know what you’re looking for.
(As a bonus, yes, you can use reflection to find the list of Devices in the first place. It just wasn’t all that relevant to this example)