Ok, I have the following structure. Basically a plugin architecture
// assembly 1 - Base Class which contains the contract public class BaseEntity { public string MyName() { // figure out the name of the deriving class // perhaps via reflection } } // assembly 2 - contains plugins based on the Base Class public class BlueEntity : BaseEntity {} public class YellowEntity : BaseEntity {} public class GreenEntity : BaseEntity {} // main console app List<BaseEntity> plugins = Factory.GetMePluginList(); foreach (BaseEntity be in plugins) { Console.WriteLine(be.MyName); }
I’d like the statement
be.MyName
to tell me whether the object is BlueEntity, YellowEntity or GreenEntity. The important thing is that the MyName property should be in the base class, because I don’t want to reimplement the property in every plugin.
Is this possible in C#?
I think you can do it through GetType: