Can you use a different class signature for different situations?

So this is probably either vague or something deceptively simple, so let me start from the top:

I want to make a class that, depending on a certain criteria, will implement different interfaces in different situations. Here is an example of what I mean:

//When you want to create a hero character (a character the player directly controls) internal class Character : IHero { //Implement interface and do stuff... } 

Or, if what you are creating isn't a hero character, this:

//When you want to create a nonhero character internal class Character : ICharacter { //Implement interface and do stuff... } 

Do note that IHero derives from ICharacter because a hero is basically a character with more properties to fill in.

I want outside classes to be able to pass in a "flag" (probably through an enum, though if you guys have a better idea than that, do tell) of some sort to differentiate what kind of character they wish to create out of this class. The class should then "mutate" (not talking about the coding thing) itself accordingly.

I saw an example of what I think I would want in the msdn source code, specifically in the Int32 structure:

#if GENERICS_WORK public struct Int32 : IComparable, IFormattable, IConvertible , IComparable<Int32>, IEquatable<Int32> #else public struct Int32 : IComparable, IFormattable, IConvertible #endif 

However, I am completely in the dark about these #if and #else things (apparently called directives). Looked them up, and saw that they don't actually work on runtime, so they may not be what I want, but this best illustrates what I'd like to do.

by TheSnowballofCobalt via /r/csharp

Leave a Reply