views:

217

answers:

6

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

A: 

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) {}
}
JaredPar
That's not exactly what the questioner was looking for (look at the question the comments he added to the MyName method)
hhafez
Both the end result and the method are the same.
JaredPar
End result is the same, true, but it'll confuse the hell out of a person who is going to maintain the code after me. I'll stick with the .GetType in the property getter.
AngryHacker
+1  A: 

Change your foreach statement to the following

foreach (BaseEntity be in plugins) {
   Console.WriteLine(be.GetType().Name);
}
Rob Prouse
+8  A: 

I think you can do it through GetType:

public class BaseEntity {
    public string MyName() {
        return this.GetType().Name
    }
}
Groky
+1  A: 

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:

Jeremiah
+5  A: 
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.

FlySwat
A: 

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);}
Charles Bretana