2

I have a Enum like

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

        public enum Days
        {
           day = sunday,
           night = monday
        }
    }
}

how can I get a Type Information for days through reflection.

Type type = assembly.GetType(Days);

Type type = typeof(Days) will return the Type info of Days. If I've have String s = "Days", with this string s I need to get the Type info of Days.

I need the type = Days

Vinod Kannan
  • 37
  • 1
  • 1
  • 7
  • You need `Enumeration+Days` as the name, I think. – user541686 Feb 23 '12 at 07:30
  • Which info you need, show expected name – sll Feb 23 '12 at 07:30
  • 2
    This code won't even compile - a) enum values can't be typed as `string` b) they are delimited by `,` not `;`. You should start with some code that compiles and then, when you have specific problems, come here and ask. I also don't understand what is trying to be acheived here - I think you need to give some more context. – Adam Ralph Feb 23 '12 at 07:32
  • with regard to your last edit - what do you mean by 'Type info of days'. The type of an enumeration is typically `int` and there is seldom a need to use another type. As I said, this code won't even compile until you change the type you are trying to use for the enumeration, which is currently `string`. – Adam Ralph Feb 23 '12 at 07:38

2 Answers2

5

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");
brgerner
  • 4,287
  • 4
  • 23
  • 38
4

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,
       ...
    }   
Falanwe
  • 4,636
  • 22
  • 37
  • factually correct but I suspect there is something fundamentally wrong with the question, i.e. why is the type so interesting? – Adam Ralph Feb 23 '12 at 07:40
  • @Adam, One of my method has a parameter of enum. GetDays(int dayCount, EnumTest.Enumeration.Days EnumTest.Enumeration.Days.day), I need to invoke this method through reflection. so that I m looking for getting the Type – Vinod Kannan Feb 23 '12 at 07:43
  • 1
    @VinodKannan post the code you're writing to invoke the method, and indicate where you're stuck. – phoog Feb 23 '12 at 07:55