显示自定义类名称

Bla*_*ell 2 c#

检索自定义类名的最佳方法是什么?我的目标是远离使用描述我的类的每个变体的枚举,如下所示:

enum
{
    MyDataType1,
    MyDataType2,
    MyDataType3
}
Run Code Online (Sandbox Code Playgroud)

每个类的实现如下:

MyDataType1 : IGenericDataType
MyDataType2 : IGenericDataType
//etc...
Run Code Online (Sandbox Code Playgroud)

但是,我有时需要显示每个类的类型的用户友好名称.在我从枚举中得到这个之前,但现在我想从类元数据中获取它,如果可能的话.因此,而不是MyDataType1.GetType().名称将显示类名称我想使用自定义名称(无需在类中定义属性).

Dou*_*oug 5

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)