Getting Type of Enum witnin class through C# reflection

后端 未结 2 775
走了就别回头了
走了就别回头了 2021-01-21 22:01

I have a Enum like

namespace EnumTest
    {
    public class Enumeration
    {
        public Enumeration();

        public enum Days
        {
           day          


        
相关标签:
2条回答
  • 2021-01-21 22:17

    To access the type you need is very simple:

    Type type = typeof(Enumeration.Days);
    

    Note that the enumeration declaration will not work as you have written it in your question. It should be something like this:

    public enum Days    
        {    
           Monday,
           Tuesday,
           ...
        }   
    
    0 讨论(0)
  • 2021-01-21 22:39

    I'm not sure whether I understand you. If you have type name in a string object and want to get the type object you need to write the whole type name.
    And because your enum is an inner type the full type name is "EnumTest.Enumeration.DaysEnumTest.Enumeration+Days".

    To get the type object you can call then

    Type type = assembly.GetType("EnumTest.Enumeration.DaysEnumTest.Enumeration+Days");
    
    0 讨论(0)
提交回复
热议问题