How to get class name

Ok, I have the following structure. Basically, 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 would like the operator

 be.MyName 

to tell me if the object is BlueEntity, YellowEntity or GreenEntity. The important thing is that the MyName property must be in the base class, because I do not want to override the property in each plugin.

Is this possible in C #?

+3
source share
6 answers

I think you can do it through GetType:

 public class BaseEntity { public string MyName() { return this.GetType().Name } } 
+10
source
 public class BaseEntity { public string MyName() { return this.GetType().Name; } } 

"this" will point to a derived class, so if you need to:

 BaseEntity.MyName "BaseEntity" BlueEntitiy.MyName "BlueEntity" 

EDIT: Doh, Gorky defeated me.

+5
source

C # implemented a way to view objects called Reflection. This may return information about the object that you are using.

The GetType () function returns the name of the class to which you call it. You can use it as follows:

 return MyObject.GetType().Name; 

Reflection can do a lot. If you want to know about it, you can read about it on these websites:

+2
source

Change the foreach statement to the following

 foreach (BaseEntity be in plugins) { Console.WriteLine(be.GetType().Name); } 
+1
source

If you have not redefined the ToString () method for the class, you can simply write the following

 string s = ToString().Split(',')[0]; // to get fully qualified class name... or, s = s.Substring(s.LastIndexOf(".")+1); // to get just the actual class name itself 

using yr code:

 // assembly 1 - Base Class which contains the contractpublic class BaseEntity { public virtual string MyName // I changed to a property { get { return MyFullyQualifiedName.Substring( MyFullyQualifiedName.LastIndexOf(".")+1); } } public virtual string MyFullyQualifiedName // I changed to a property { get { return ToString().Split(',')[0]; } } } // 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);} 
-one
source

Try this template.

 class BaseEntity { private readonly m_name as string; public Name { get { return m_name; } } protected BaseEntity(name as string) { m_name = name; } } class BlueEntity : BaseEntity { public BlueEntity() : base(typeof(BlueEntity).Name) {} } 
-2
source

All Articles