Reflect a structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Williamson

    Reflect a structure

    Assuming I have the following structure within a class:

    class myClass
    {
    public struct Action
    {
    public const String REQUIRED = "T";
    public const String IGNORE = "F";
    public const String OPTIONAL = "O";
    }

    ... code ...
    }

    How can I use reflection to access the elements of the
    structure?

    I can get the structure as such:
    typeof(myClass) .GetMember("Act ion",
    BindingFlags.Pu blic|BindingFla gs.Static), but I am
    stymied on how to delve into it.

    Cheers, Ian Williamson

  • Joe Mayo

    #2
    Re: Reflect a structure

    "Ian Williamson" <bmcpeake@nospa m.shaw.ca> wrote in message
    news:06fe01c3a9 80$9a5c3cb0$a30 1280a@phx.gbl.. .[color=blue]
    > Assuming I have the following structure within a class:
    >
    > class myClass
    > {
    > public struct Action
    > {
    > public const String REQUIRED = "T";
    > public const String IGNORE = "F";
    > public const String OPTIONAL = "O";
    > }
    >
    > ... code ...
    > }
    >
    > How can I use reflection to access the elements of the
    > structure?
    >
    > I can get the structure as such:
    > typeof(myClass) .GetMember("Act ion",
    > BindingFlags.Pu blic|BindingFla gs.Static), but I am
    > stymied on how to delve into it.[/color]

    Hi Ian,

    Try this:

    Type outerType = typeof(myClass) ;

    Type innerType = outerType.GetNe stedType("Actio n",
    BindingFlags.Pu blic|BindingFla gs.Static);

    Console.WriteLi ne("innerType: {0}\n", innerType.ToStr ing());

    FieldInfo[] fields = innerType.GetFi elds();

    foreach (FieldInfo field in fields)
    {
    Console.WriteLi ne("Type: {0} Name: {1}",
    field.FieldType , field.Name);
    }

    Console.ReadLin e();

    Joe
    --
    Welcome to C# Station!  This is a community site for people interested in applying .NET using the C# programming language.  We’ve been around since July 4th 2000 and have continued to grow over the years.  Items of interest include Articles, Books, Links, Documentation,  and Tutorials. More… Source Code If you would like to see an […]



    Comment

    • Ian Williamson

      #3
      Re: Reflect a structure

      That's the ticket. Thanks Joe.

      Ian
      [color=blue]
      >-----Original Message-----
      >"Ian Williamson" <bmcpeake@nospa m.shaw.ca> wrote in[/color]
      message[color=blue]
      >news:06fe01c3a 980$9a5c3cb0$a3 01280a@phx.gbl. ..[color=green]
      >> Assuming I have the following structure within a class:
      >>
      >> class myClass
      >> {
      >> public struct Action
      >> {
      >> public const String REQUIRED = "T";
      >> public const String IGNORE = "F";
      >> public const String OPTIONAL = "O";
      >> }
      >>
      >> ... code ...
      >> }
      >>
      >> How can I use reflection to access the elements of the
      >> structure?
      >>
      >> I can get the structure as such:
      >> typeof(myClass) .GetMember("Act ion",
      >> BindingFlags.Pu blic|BindingFla gs.Static), but I am
      >> stymied on how to delve into it.[/color]
      >
      >Hi Ian,
      >
      >Try this:
      >
      > Type outerType = typeof(myClass) ;
      >
      > Type innerType = outerType.GetNe stedType("Actio n",
      > BindingFlags.Pu blic|BindingFla gs.Static);
      >
      > Console.WriteLi ne("innerType: {0}\n",[/color]
      innerType.ToStr ing());[color=blue]
      >
      > FieldInfo[] fields = innerType.GetFi elds();
      >
      > foreach (FieldInfo field in fields)
      > {
      > Console.WriteLi ne("Type: {0} Name: {1}",
      > field.FieldType , field.Name);
      > }
      >
      > Console.ReadLin e();
      >
      >Joe
      >--
      >http://www.csharp-station.com
      >
      >
      >.
      >[/color]

      Comment

      Working...