A Reflection Puzzle:(

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laser Lu

    #1

    A Reflection Puzzle:(

    HI, all,
    I just want to invoke an internal method named 'ResolveClientU rl', which
    is defined in class System.Web.UI.C ontrol, using an instance object of a
    type that derives from Control.

    Let's see the following code snippet (class MyControl is a sub class that
    derives directly from System.Web.UI.C ontrol):
    //...
    MyControl instance = new MyControl();
    Type type = instance.GetTyp e();
    object[] args = new object[] {"images/test.gif"};
    string path = (string)type.In vokeMember("Res olveClientUrl",
    BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance |
    BindingFlags.Ge tProperty,
    null, instance, args);
    //...

    Actually, I intended to use the type object of MyControl to invoke the inherited
    method 'ResolveClientU rl', but an Exception occured, described as "Method
    LaserWeb.Presen tation.MyContro l.ResolveClient Url not found." It seems that
    reflection didn't search in the inherited members up the class hierarchy,
    since 'ResolveClientU rl' is a declared method in the base class Control.

    This action, I guess, should only be taken when BindingFlags.De claredOnly
    is specified for invoking a method, otherwise inherited members derived from
    base class should be considered. However, in this sample code, it didn't
    work:( I did not pass the BindingFlags.De claredOnly flag. Then why? How could
    it happen? I was puzzled:( Help!

    Regards,
    Laser Lu


  • Jon Skeet [C# MVP]

    #2
    Re: A Reflection Puzzle:(

    Laser Lu <laser_lu@hotma il.com> wrote:[color=blue]
    > HI, all,
    > I just want to invoke an internal method named 'ResolveClientU rl', which
    > is defined in class System.Web.UI.C ontrol, using an instance object of a
    > type that derives from Control.
    >
    > Let's see the following code snippet (class MyControl is a sub class that
    > derives directly from System.Web.UI.C ontrol):
    > //...
    > MyControl instance = new MyControl();
    > Type type = instance.GetTyp e();
    > object[] args = new object[] {"images/test.gif"};
    > string path = (string)type.In vokeMember("Res olveClientUrl",
    > BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance |
    > BindingFlags.Ge tProperty,
    > null, instance, args);
    > //...
    >
    > Actually, I intended to use the type object of MyControl to invoke the inherited
    > method 'ResolveClientU rl', but an Exception occured, described as "Method
    > LaserWeb.Presen tation.MyContro l.ResolveClient Url not found." It seems that
    > reflection didn't search in the inherited members up the class hierarchy,
    > since 'ResolveClientU rl' is a declared method in the base class Control.
    >
    > This action, I guess, should only be taken when BindingFlags.De claredOnly
    > is specified for invoking a method, otherwise inherited members derived from
    > base class should be considered. However, in this sample code, it didn't
    > work:( I did not pass the BindingFlags.De claredOnly flag. Then why? How could
    > it happen? I was puzzled:( Help![/color]

    Even if you could find the member, you shouldn't be able to invoke it,
    if it's internal to a separate assembly.

    --
    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

    • Laser Lu

      #3
      Re: A Reflection Puzzle:(

      Hello Jon Skeet, actually, the following code runs well:
      //...
      MyControl instance = new MyControl();
      Type type = typeof(System.W eb.UI.Control); // here use the Type object of
      Control, not of MyControl.
      object[] args = new object[] {"images/test.gif"};
      string path = (string)type.In vokeMember("Res olveClientUrl",
      BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
      | BindingFlags.Ge tProperty,
      null, instance, args);
      //...

      The only change, that I've made on the code, is that I used the Type object
      of Control instead. And the Control class is the declaring type of the member
      'ResolveClientU rl'.

      Maybe I didn't understand what do you mean in your post. However, the above
      code runs well without any exceptions, and the method member gets invoked
      correctly, even it is internal to my assembly.

      Still, My puzzle is how to invoke inherited members using derived types?
      It seems that there's no difference in cases whether you have a BindingFlags.De clareOnly
      specified or not:( Why is it so strange?
      [color=blue]
      > Laser Lu <laser_lu@hotma il.com> wrote:
      >[color=green]
      >> HI, all,
      >> I just want to invoke an internal method named 'ResolveClientU rl',
      >> which
      >> is defined in class System.Web.UI.C ontrol, using an instance object
      >> of a
      >> type that derives from Control.
      >> Let's see the following code snippet (class MyControl is a sub class
      >> that
      >> derives directly from System.Web.UI.C ontrol):
      >> //...
      >> MyControl instance = new MyControl();
      >> Type type = instance.GetTyp e();
      >> object[] args = new object[] {"images/test.gif"};
      >> string path = (string)type.In vokeMember("Res olveClientUrl",
      >> BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
      >> |
      >> BindingFlags.Ge tProperty,
      >> null, instance, args);
      >> //...
      >> Actually, I intended to use the type object of MyControl to invoke
      >> the inherited method 'ResolveClientU rl', but an Exception occured,
      >> described as "Method LaserWeb.Presen tation.MyContro l.ResolveClient Url
      >> not found." It seems that reflection didn't search in the inherited
      >> members up the class hierarchy, since 'ResolveClientU rl' is a
      >> declared method in the base class Control.
      >>
      >> This action, I guess, should only be taken when
      >> BindingFlags.De claredOnly is specified for invoking a method,
      >> otherwise inherited members derived from base class should be
      >> considered. However, in this sample code, it didn't work:( I did not
      >> pass the BindingFlags.De claredOnly flag. Then why? How could it
      >> happen? I was puzzled:( Help!
      >>[/color]
      > Even if you could find the member, you shouldn't be able to invoke it,
      > if it's internal to a separate assembly.
      >[/color]



      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: A Reflection Puzzle:(

        Laser Lu <laser_lu@hotma il.com> wrote:[color=blue]
        > Hello Jon Skeet, actually, the following code runs well:
        > //...
        > MyControl instance = new MyControl();
        > Type type = typeof(System.W eb.UI.Control); // here use the Type object of
        > Control, not of MyControl.
        > object[] args = new object[] {"images/test.gif"};
        > string path = (string)type.In vokeMember("Res olveClientUrl",
        > BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
        > | BindingFlags.Ge tProperty,
        > null, instance, args);
        > //...
        >
        > The only change, that I've made on the code, is that I used the Type object
        > of Control instead. And the Control class is the declaring type of the member
        > 'ResolveClientU rl'.
        >
        > Maybe I didn't understand what do you mean in your post. However, the above
        > code runs well without any exceptions, and the method member gets invoked
        > correctly, even it is internal to my assembly.[/color]

        It may in some trust environments, but:

        a) It won't in others
        b) There's no guarantee that that method will be in future versions of
        the framework
        c) Internal methods should *not* be seen as part of the public
        interface which you should call. If they were meant to be called by the
        "outside world", they'd have been made protected or public.
        [color=blue]
        > Still, My puzzle is how to invoke inherited members using derived
        > types? It seems that there's no difference in cases whether you have
        > a BindingFlags.De clareOnly specified or not:( Why is it so strange?[/color]

        Well, just looking at your code again, you're using
        BindingFlags.Ge tProperty, which probably isn't what you're after for a
        method call.

        --
        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

        • Laser Lu

          #5
          Re: A Reflection Puzzle:(

          Jon Skeet wrote:[color=blue]
          > Well, just looking at your code again, you're using
          > BindingFlags.Ge tProperty, which probably isn't what you're after for a
          > method call.
          >[/color]
          So sorry for the mistake I made during copying code to this post!! The original
          code snippet should be:
          //...
          MyControl instance = new MyControl();
          Type type = instance.GetTyp e();
          object[] args = new object[] {"images/test.gif"};
          string path = (string)type.In vokeMember("Res olveClientUrl",
          BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
          | BindingFlags.In vokeMethod,
          null, instance, args); // Exception is thrown, withou no such member
          found in the derived class.
          //...

          The reason why I made this mistake is actually I've wrote a helper class
          as the following:
          // code starts
          using System;
          using System.Reflecti on;

          namespace LaserWeb.Presen tation.Common
          {
          internal sealed class Reflector
          {
          private Reflector() {}

          public static object GetProperty(obj ect instance, string name)
          {
          Type type = instance.GetTyp e();
          return type.InvokeMemb er(name,
          BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
          | BindingFlags.Ge tProperty,
          null, instance, null);
          }

          public static object GetProperty(Typ e type, object instance, string name)
          {
          return type.InvokeMemb er(name,
          BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
          | BindingFlags.Ge tProperty,
          null, instance, null);
          }

          public static object InvokeMethod(ob ject instance, string name, object[]
          args)
          {
          Type type = instance.GetTyp e();
          return type.InvokeMemb er(name,
          BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
          | BindingFlags.In vokeMethod,
          null, instance, args);
          }

          public static object InvokeMethod(Ty pe type, object instance, string name,
          object[] args)
          {
          return type.InvokeMemb er(name,
          BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
          | BindingFlags.In vokeMethod,
          null, instance, args);
          }
          }
          }
          // code ends
          When I posted the puzzle, the original code snippet is not handy (I have
          removed it from my project), so I made the wrong code snippet by copying
          the code fragment from Reflector.GetPr operty() and modifing base on that
          code. That's the reason why I made the mistake, sorry again for misleading
          all of you in your reading!

          If you use the above helper class as the following, you will still find the
          problem I described:
          // ..
          MyControl instance = new MyControl();
          object args = new objet[] {"images/test.gif"};
          Reflector.Invok eMethod(typeof( System.Web.UI.C ontrol), instance, "ResolveClientU rl",
          args); // This will works fine.
          Reflector.Invok eMethod(instanc e, "ResolveClientU rl", args); // An exception
          will be thrown here, indicating member not found.
          // ...

          I don't know why I had encountered such a strange problem, and got puzzled.
          You may make the similar sample code and have a try, to see whether what
          I said would happen. Thanks:)

          Laser Lu.


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: A Reflection Puzzle:(

            Laser Lu <laser_lu@hotma il.com> wrote:[color=blue]
            > Jon Skeet wrote:[color=green]
            > > Well, just looking at your code again, you're using
            > > BindingFlags.Ge tProperty, which probably isn't what you're after for a
            > > method call.
            > >[/color]
            > So sorry for the mistake I made during copying code to this post!! The original
            > code snippet should be:
            > //...
            > MyControl instance = new MyControl();
            > Type type = instance.GetTyp e();
            > object[] args = new object[] {"images/test.gif"};
            > string path = (string)type.In vokeMember("Res olveClientUrl",
            > BindingFlags.Pu blic | BindingFlags.No nPublic | BindingFlags.In stance
            > | BindingFlags.In vokeMethod,
            > null, instance, args); // Exception is thrown, withou no such member
            > found in the derived class.
            > //...[/color]

            <snip>

            Okay, well I can reproduce the problem, but not solve it. However, as
            I've said before, this isn't something you should be doing anyway. I
            urge you to change your design so that you don't need to call internal
            methods in other assemblies.

            --
            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

            • Laser Lu

              #7
              Re: A Reflection Puzzle:(

              Okay, thank you very much!
              And I will try to avoid invoking internal or private members in ther future,
              as doing this will certainly violate the design principles:)[color=blue]
              >
              > Okay, well I can reproduce the problem, but not solve it. However, as
              > I've said before, this isn't something you should be doing anyway. I
              > urge you to change your design so that you don't need to call internal
              > methods in other assemblies.
              >[/color]



              Comment

              Working...