Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Providing Inherited Static Properties (conceptually)

Tags:

c#

inheritance

I have a C# base class that I was to associate information with on a per-type (rather than per-instance) basis. Essentially I want all child classes to present an Icon and a FriendlyName that represents the type. I'd like to not have to create a type instance to get this information and I want to require that all child classes provide this information. Ideally it would be in the interface that the base derives from.

I tried to use a class Attribute, but it requires a constant value, so while I can set the FriendlyName, I can't set the Icon, and I can't see a way to make the Attribute required anyway, so child classes could get away with not having it (until run time).

I added a static Property to the base that children can hide, but that's kind of ugly and the values from the base are really nonsensical. It can't know what the name would be for a child. I could have the base throw in the Property, but again, that wouldn't get caught until run time.

Anyone have any ideas on how I might implement something like this? Is there a pattern I'm missing? Any out-of-the-box thinking is encouraged as well.

Edit: Really what I need to the ability to require class Attributes along an inheritance tree and be able to use an embedded resource to set those attributes. All of the information that will be returned really is static and known at compile. I just can't figure out a way to nicely expose it.

Edit 2: Here's a more concrete example of what I need:

Let's assume I have a base class:

abstract class Vehicle { ... }

I then have two children:

class Car : Vehicle { ... }

class Truck : Vehicle { ... }

I'd like to be able to get an Icon representing a "Car" or a "Truck" without having to create an instance of one (a la Car.Icon) since all vehicles of a given type will all have the same icon.

I also want to ensure that all Vehicle-derived Types expose the same way to get this Icon in case someone comes along in the future and adds a new class

class Airplane : Vehicle  { ... }

Edit 3: Why do I need this? Well we have an app that can take plug-ins. The plug in is defined by a type, and when the type is loaded, we show the user an icon and name. When they click the icon, it creates an instance of that type (and gives them a dialog for naming the instance, etc). I don't need or want to have to create an instance of the type to get the icon.

like image 622
ctacke Avatar asked Nov 05 '22 22:11

ctacke


1 Answers

If you want to absolutely dictate that the child implement this, then the only way I see doing it is making the base class abstract, and expose instance properties to return the icon and friendly name.

These would be abstract, not virtual, so you force the implementation by the derived class. I know it's not clean, in that it is information that is better stored on the type level, but it's the only way I can see to enforce this requirement at compile-time.

like image 144
casperOne Avatar answered Nov 12 '22 16:11

casperOne