Display Custom Class Name
What is the best way to retrieve a custom class name? My goal is to get away from using an enum that describes each variation of my classes as follows:
enum
{
MyDataType1,
MyDataType2,
MyDataType3
}
with each class being implemented as follows:
MyDataType1 : IGenericDataType
MyDataType2 : IGenericDataType
//etc...
However, I sometimes need to display a user-friendly name of each class's type. Before I could get this from the enum, but now I would like to get it from class metadata if possible. So instead of MyDataType1.GetType().Name which would display the class name I'd like to use a 开发者_运维百科custom name (without having to define a property in the class).
Have you considered adding a custom attribute to the class that can be then be retrieved through reflection. Here is Microsoft's quick tutorial to give you an idea how to apply this technique. Using this method will allow you to add as much additional metadata to your class as needed either through using built in framework attributes or defining your own.
Here is another post that I made that also refers to using extra metadata to do dynamic class loading; which I think is what you are trying to accomplish?
Since there were a few question how to do this below is a simple console application that you can run and see first hand how it works.
Enjoy!
using System;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Type[] _types = null;
//load up a dll that you would like to introspect. In this example
//we are loading the currently executing assemble since all the sample code
//is constained within this simple program. In practice you may want to change
//this to a string that point to a particual assemble on your file system using
//Assembly.LoadFrom("some assembly")
Assembly a = Assembly.GetExecutingAssembly();
//get an arrray of the types contained in the dll
_types = a.GetTypes();
//loop through the type contained in the assembly looking for
//particuar types.
foreach (Type t in _types)
{
//see if this type does not contain the desired interfaec
//move to the next one.
if (t.GetInterface("CustomInterface") == null)
{
continue;
}
//get a reference to the attribute
object[] attributes = t.GetCustomAttributes(typeof(CustomAttribute), false);
CustomAttribute attribute = (CustomAttribute)attributes[0];
//do something with the attribue
Console.WriteLine(attribute.Name);
}
}
}
/// <summary>
/// This is a sample custom attribute class. Add addtional properties as needed!
/// </summary>
public class CustomAttribute : Attribute
{
private string _name = string.Empty;
public CustomAttribute(string name)
: base()
{
_name = name;
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
/// <summary>
/// Here is a custom interface that we can search for while using reflection.
/// </summary>
public interface CustomInterface
{
void CustomMethod();
}
/// <summary>
/// Here is a sample class that implements our custom interface and custom attribute.
/// </summary>
[CustomAttribute("Some string I would like to assiciate with this class")]
public class TestClass : CustomInterface
{
public TestClass()
{
}
public void CustomMethod()
{
//do some work
}
}
/// <summary>
/// Just another class without the interface or attribute so you can see how to just
/// target the class you would like by the interface.
/// </summary>
public class TestClass2
{
public TestClass2()
{
}
public void CustomMethod()
{
//do some work
}
}
}
What about overriding the ToString() Property:
var x = MyCustomClass();
Console.WriteLine(x.ToString();
public class MyCustomClass()
{
public override string ToString()
{
return "MyCustom Class user friendly name";
}
}
I would use a Custom Attribute to do this.
You create a class
[AttributeUsage(AttributeTargets.Class]
Class SimpleClassName : System.Attribute{
public string ClassName { get; set; }
SimpleClassName(string _name){
ClassName = _name;
}
}
Then you would just need to get the Custom Attribute value after this, Then you just on your class do
[SimpleClassName("Easy")]
Class ComplicatedName{
}
I'm a VB Net programmer during the day so ignore the bad C#
If you don't want to define a property in the class, then you'll have to create some kind of lookup table that maps the class name to the friendly name. Unless you can derive the friendly name from the class name. That is "ManagementEmployeeClass" has a friendly name of "Management Employee", or the class name "IT_Contract_Worker" becomes "IT Contract Worker".
精彩评论