Reflection Values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Webb

    Reflection Values

    Hey All,

    I've got a class that I'm trying to get a listing of the properties...ki nda
    like one of the Reflection examples from Microsoft. However, unlike their
    example, I need to get the value as well. And am having trouble
    understanding how the GetValue() function of the PropertyInfo should be
    setup as below.

    Also, with it coming back as an object, is there a way to get it recast so
    that the DoSomething function will work?


    I'm hopeing/thinking/praying.. that there is a way to make the code below
    have StringBuilder1. ToString() be
    "-FirstField='Str ing1'-SecondField=2"

    Any help ... or comments would be appreciated.

    - Roger


    class LocalClass
    {
    public string FirstField;
    public int SecondField;
    }

    <snip most of procedure>
    ....
    LocalClass LocalClass1 = new LocalClass();
    LocalClass1.Fir stField = "String1";
    LocalClass1.Sec ondField = 2;

    Type DRType = LocalClass1.Get Type();
    PropertyInfo[] PropInfo = DRType.GetPrope rties();
    foreach(Propert yInfo prop in PropInfo)
    {
    if(!(prop.Prope rtyType.IsArray ))
    {
    StringBuilder1. Append("-"+DoSomething(p rop.Name,
    prop.GetValue(p rop, ????)));
    }
    }
    ....


    public string DoSomething(str ing A, string B)
    {
    return string.Format(" {0}='{1}'",A,B) ;
    }
    public string DoSomething(str ing A, int B)
    {
    return string.Format(" {0}={1}",A,B.To String());
    }






  • Jon Skeet [C# MVP]

    #2
    Re: Reflection Values

    Roger Webb <rwebb@nos.twia .org> wrote:[color=blue]
    > I've got a class that I'm trying to get a listing of the properties...ki nda
    > like one of the Reflection examples from Microsoft. However, unlike their
    > example, I need to get the value as well. And am having trouble
    > understanding how the GetValue() function of the PropertyInfo should be
    > setup as below.[/color]

    The call to prop.GetValue should be

    prop.GetValue (LocalClass1, null);

    However, your class LocalClass doesn't actually have any properties, as
    you've written it - it has two fields instead.
    [color=blue]
    > Also, with it coming back as an object, is there a way to get it recast so
    > that the DoSomething function will work?[/color]

    Yes - cast it to string.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Roger Webb

      #3
      Re: Reflection Values

      > The call to prop.GetValue should be[color=blue]
      >
      > prop.GetValue (LocalClass1, null);[/color]

      That was it.
      [color=blue]
      >
      > However, your class LocalClass doesn't actually have any properties, as
      > you've written it - it has two fields instead.[/color]

      Yeah.. That was because it was a quick example... Didnt create it exactly
      right. My Bad. But, Thanks... I was missing the concept the the First
      Parameter was the class that the property belonged to.
      [color=blue][color=green]
      > > Also, with it coming back as an object, is there a way to get it recast[/color][/color]
      so[color=blue][color=green]
      > > that the DoSomething function will work?[/color]
      >
      > Yes - cast it to string.
      >[/color]

      I can believe that, however How can you cast it as .. whatever is was... for
      example...

      DoSomething(pro p.Name, (???) prop.GetValue(L ocalClass1, null)));

      What would you put here ... that would be "String" in the first case hence
      casting the Property as a String.... and an "int" in the second case,
      casting that property as an int... so that it will use the appropriate
      overloaded method.

      Filling that casting spot with prop. something give a compile error.

      - Roger


      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Reflection Values

        Roger,

        You can't do it. You would need to have a large switch statement based
        on type (or the name of the type, rather), which would output the values you
        want based on type, or, you could just call the ToString method on the
        object, and it will return something (the value, in some cases, otherwise,
        usually just the type name).

        Hope this helps.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Roger Webb" <rwebb@nos.twia .org> wrote in message
        news:uVIXTwr5EH A.828@TK2MSFTNG P14.phx.gbl...[color=blue][color=green]
        >> The call to prop.GetValue should be
        >>
        >> prop.GetValue (LocalClass1, null);[/color]
        >
        > That was it.
        >[color=green]
        >>
        >> However, your class LocalClass doesn't actually have any properties, as
        >> you've written it - it has two fields instead.[/color]
        >
        > Yeah.. That was because it was a quick example... Didnt create it exactly
        > right. My Bad. But, Thanks... I was missing the concept the the First
        > Parameter was the class that the property belonged to.
        >[color=green][color=darkred]
        >> > Also, with it coming back as an object, is there a way to get it recast[/color][/color]
        > so[color=green][color=darkred]
        >> > that the DoSomething function will work?[/color]
        >>
        >> Yes - cast it to string.
        >>[/color]
        >
        > I can believe that, however How can you cast it as .. whatever is was...
        > for
        > example...
        >
        > DoSomething(pro p.Name, (???) prop.GetValue(L ocalClass1, null)));
        >
        > What would you put here ... that would be "String" in the first case hence
        > casting the Property as a String.... and an "int" in the second case,
        > casting that property as an int... so that it will use the appropriate
        > overloaded method.
        >
        > Filling that casting spot with prop. something give a compile error.
        >
        > - Roger
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Reflection Values

          Roger Webb <rwebb@nos.twia .org> wrote:[color=blue][color=green]
          > > The call to prop.GetValue should be
          > >
          > > prop.GetValue (LocalClass1, null);[/color]
          >
          > That was it.[/color]

          Goodo.
          [color=blue][color=green][color=darkred]
          > > > Also, with it coming back as an object, is there a way to get it recast[/color][/color]
          > so[color=green][color=darkred]
          > > > that the DoSomething function will work?[/color]
          > >
          > > Yes - cast it to string.
          > >[/color]
          >
          > I can believe that, however How can you cast it as .. whatever is was... for
          > example...
          >
          > DoSomething(pro p.Name, (???) prop.GetValue(L ocalClass1, null)));[/color]

          Ah, you can't do that.

          You'll need to know ahead of time which overload to use... *or*
          possibly use reflection to find the right method and invoke it as well.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          Working...