Programmaticallygetting type of an enum

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Hundley

    #1

    Programmaticallygetting type of an enum

    Scenario:
    - I'm dumping an int value of myEnum into a table.
    - I am getting that int value out of the table.
    - I need to set myEnum equal to that int value

    Easy enough:
    private enum animals {cat=0, dog=1}
    private animals myAnimal = (animals)1;

    OK- so that works, and myAnimal is set to dog....

    Problem:
    I'm using reflection to go through a list of properties and am setting them
    to their respective values from a table. My problem is that I need to
    <<programmatica lly>> get type myAnimal.

    private animals myAnimal = (System.Enum)1; //doesn't work
    private animals myAnimal = 1; //doesn't work
    private animals myAnimal = (programaticall y get type myAnimal)1; //this is
    what I need

    Anyone have any suggestions???

    Thanks,
    Tom




  • Jay B. Harlow [MVP - Outlook]

    #2
    Re: Programmaticall ygetting type of an enum

    Tom,
    Have you tried the Enum.ToObject function?

    private animals myAnimal = Enum.ToObject(t ypeof(animals), 1);

    Of course if you already have a variable of type System.Type you can use
    that on ToObject instead of typeof. (if you are using reflection you should
    have a variable of type System.Type!

    Hope this helps
    Jay

    "Tom Hundley" <thomas_hundley @_nospam_hotmai l.com> wrote in message
    news:%23Bu$RzRi DHA.1060@tk2msf tngp13.phx.gbl. ..[color=blue]
    > Scenario:
    > - I'm dumping an int value of myEnum into a table.
    > - I am getting that int value out of the table.
    > - I need to set myEnum equal to that int value
    >
    > Easy enough:
    > private enum animals {cat=0, dog=1}
    > private animals myAnimal = (animals)1;
    >
    > OK- so that works, and myAnimal is set to dog....
    >
    > Problem:
    > I'm using reflection to go through a list of properties and am setting[/color]
    them[color=blue]
    > to their respective values from a table. My problem is that I need to
    > <<programmatica lly>> get type myAnimal.
    >
    > private animals myAnimal = (System.Enum)1; //doesn't work
    > private animals myAnimal = 1; //doesn't work
    > private animals myAnimal = (programaticall y get type myAnimal)1; //this[/color]
    is[color=blue]
    > what I need
    >
    > Anyone have any suggestions???
    >
    > Thanks,
    > Tom
    >
    >
    >
    >[/color]


    Comment

    • Tom Hundley

      #3
      Re: Programmaticall ygetting type of an enum

      Yup, worked great. Thanks!

      val = Enum.ToObject(p i.PropertyType,
      (int)Source.Get Type().GetPrope rty(_propertyPr efix +
      pi.Name).GetVal ue(Source, null));


      "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
      news:%23TK2v4Ri DHA.1300@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > Tom,
      > Have you tried the Enum.ToObject function?
      >
      > private animals myAnimal = Enum.ToObject(t ypeof(animals), 1);
      >
      > Of course if you already have a variable of type System.Type you can use
      > that on ToObject instead of typeof. (if you are using reflection you[/color]
      should[color=blue]
      > have a variable of type System.Type!
      >
      > Hope this helps
      > Jay
      >
      > "Tom Hundley" <thomas_hundley @_nospam_hotmai l.com> wrote in message
      > news:%23Bu$RzRi DHA.1060@tk2msf tngp13.phx.gbl. ..[color=green]
      > > Scenario:
      > > - I'm dumping an int value of myEnum into a table.
      > > - I am getting that int value out of the table.
      > > - I need to set myEnum equal to that int value
      > >
      > > Easy enough:
      > > private enum animals {cat=0, dog=1}
      > > private animals myAnimal = (animals)1;
      > >
      > > OK- so that works, and myAnimal is set to dog....
      > >
      > > Problem:
      > > I'm using reflection to go through a list of properties and am setting[/color]
      > them[color=green]
      > > to their respective values from a table. My problem is that I need to
      > > <<programmatica lly>> get type myAnimal.
      > >
      > > private animals myAnimal = (System.Enum)1; //doesn't work
      > > private animals myAnimal = 1; //doesn't work
      > > private animals myAnimal = (programaticall y get type myAnimal)1; //this[/color]
      > is[color=green]
      > > what I need
      > >
      > > Anyone have any suggestions???
      > >
      > > Thanks,
      > > Tom
      > >
      > >
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...