c#: Overriding Static Methods/Vars

Associate
Joined
22 Jul 2004
Posts
230
Location
Nottingham, UK
Say i have a class called CustomModule1 which inherits from an abstract base class called Module.

All 'CustomModules' will be in thier own dll and my main application will load them from a folder at run time.
At this point i wish to store information about the CustomModules by getting their string ModuleName (with is different for each module) without creating an instance of the module (statically).

I can't think of away todo this, as I'm loading the CustomModules as Module types, so the static string ModuleName will have to be defined in the ModuleClass.
I need the CustomModule to override this static variable so it gives its own unique name, but you cannot override static properties or methods.

Any thoughts?
 
That's working cheers.

The only thing is a programmer isn't forced to "overide" it, so the base classes maybe used instead.

Is their any other methods which ensures its overriden like an abstract definition would?
 
I think that is the only inheritance related modifier you can use on static properties/methods... I don't think there is a way of forcing the programmer to override statics.

Could you not use the GetType method or some of the reflection stuff to do this instead of having to store these strings?

edit: I'm kinda guessing with that second suggestion, I mostly use C# with ASP, I've just coded a few simple personal tools for "real" apps so I'm not too sure on how I'd go about this sort of thing either.
 
I've done this sort of thing before by creating a custom attribute and then using reflection to build up a list of classes which are marked up with it. This doesn't require any instantiation and is somewhat neater than using statics, in my opinion.

arty
 
I'm gonna stick with the 'new' method.

But being as you mentioned reflection:

Can you create a static method in a class which uses reflection to return the type of that class?

I can do it easily from outside the class, but putting it in a static method is more convenient.
 
Thats what i would use outside the class.

Basically i want this static function in the abstract base class, so I cannot hardcode what type to get, as it depends on what has inherited the base class.
 
Why does it have to be static? If you're calling from within an instance of a derived type, just use GetType(). If you're calling the static method externally, then you'd have to go through the type itself, in which case typeof() would suffice.
 
Inquisitor said:
If you're calling the static method externally, then you'd have to go through the type itself, in which case typeof() would suffice.

That's what I'm doing at the moment, and it works fine.

But it would be more convenient to have a static method which returns a struct of all the details of that class, including the Type of it in one go.
Its not a major problem, I was just wondering if its possible.
 
Back
Top Bottom