AngryHacker
AngryHacker

Reputation: 61596

How to get the name of the class

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#?

Upvotes: 3

Views: 448

Answers (6)

Jeremiah
Jeremiah

Reputation: 5514

C# implemented a way to look at objects called Reflection. This can return information about the object you are using.

The GetType() function returns the name of the class you are calling it on. You can use it like this:

return MyObject.GetType().Name;

Reflection can do a lot of things. If there is more that you want to know about reflection you can read about it on these websites:

Upvotes: 2

Charles Bretana
Charles Bretana

Reputation: 146429

If you haven't overridden the ToString() method for the class, then you can just 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);}

Upvotes: -1

JaredPar
JaredPar

Reputation: 754565

Try this pattern

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) {}
}

Upvotes: -2

FlySwat
FlySwat

Reputation: 175583

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

"this" will point to the derived class, so if you were to do:

BaseEntity.MyName
"BaseEntity"

BlueEntitiy.MyName
"BlueEntity"

EDIT: Doh, Gorky beat me to it.

Upvotes: 5

Grokys
Grokys

Reputation: 16526

I think you can do it through GetType:

public class BaseEntity {
    public string MyName() {
        return this.GetType().Name
    }
}

Upvotes: 10

Rob Prouse
Rob Prouse

Reputation: 22647

Change your foreach statement to the following

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

Upvotes: 1

Related Questions