如何获得班级的名称

时间:2022-10-31 07:31:50

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.

告诉我对象是BlueEntity,YellowEntity还是GreenEntity。重要的是MyName属性应该在基类中,因为我不想在每个插件中重新实现该属性。

Is this possible in C#?

这可能在C#中吗?

6 个解决方案

#1


10  

I think you can do it through GetType:

我想你可以通过GetType来做到这一点:

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

#2


5  

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

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

“this”将指向派生类,所以如果你这样做:

BaseEntity.MyName
"BaseEntity"

BlueEntitiy.MyName
"BlueEntity"

EDIT: Doh, Gorky beat me to it.

编辑:Doh,高尔基打败了我。

#3


2  

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

C#实现了一种查看名为Reflection的对象的方法。这可以返回有关您正在使用的对象的信息。

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

GetType()函数返回您调用它的类的名称。你可以像这样使用它:

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:

反思可以做很多事情。如果您想了解更多关于反思的知识,可以在以下网站上阅读:

#4


1  

Change your foreach statement to the following

将您的foreach语句更改为以下内容

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

#5


-1  

If you haven't overridden the ToString() method for the class, then you can just write the following

如果您没有覆盖类的ToString()方法,那么您可以编写以下内容

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

#6


-2  

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

#1


10  

I think you can do it through GetType:

我想你可以通过GetType来做到这一点:

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

#2


5  

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

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

“this”将指向派生类,所以如果你这样做:

BaseEntity.MyName
"BaseEntity"

BlueEntitiy.MyName
"BlueEntity"

EDIT: Doh, Gorky beat me to it.

编辑:Doh,高尔基打败了我。

#3


2  

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

C#实现了一种查看名为Reflection的对象的方法。这可以返回有关您正在使用的对象的信息。

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

GetType()函数返回您调用它的类的名称。你可以像这样使用它:

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:

反思可以做很多事情。如果您想了解更多关于反思的知识,可以在以下网站上阅读:

#4


1  

Change your foreach statement to the following

将您的foreach语句更改为以下内容

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

#5


-1  

If you haven't overridden the ToString() method for the class, then you can just write the following

如果您没有覆盖类的ToString()方法,那么您可以编写以下内容

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

#6


-2  

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