Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# .NET 1.1 - MethodBase.Invoke on virtual methods (dynamic method lookup)

C# .NET 1.1 - MethodBase.Invoke on virtual methods (dynamic method lookup)

Scheduled Pinned Locked Moved C#
csharptutorialhtmlquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roman Nurik
    wrote on last edited by
    #1

    Hello all, I was wondering if there was a way to overcome the "dynamic method lookup" done by MethodBase.Invoke when invoking virtual methods. For example:

    public class A
    {
    public virtual int Call()
    {
    return 1;
    }
    }

    public class B : A
    {
    public override int Call()
    {
    return 2;
    }
    }

    ... invoking it somewhere else ...

    B obj = new B();
    MethodInfo method = typeof(A).GetMethod("Call", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
    int a = method.Invoke(this, null);

    the final value of a would be 2 and not 1. Anyone know how to get around this? (P.S. A page on the columbia university website details the default behavior[^] under the 'Remarks' section of Invoke ) Thanks in advance!

    r -€

    S 1 Reply Last reply
    0
    • R Roman Nurik

      Hello all, I was wondering if there was a way to overcome the "dynamic method lookup" done by MethodBase.Invoke when invoking virtual methods. For example:

      public class A
      {
      public virtual int Call()
      {
      return 1;
      }
      }

      public class B : A
      {
      public override int Call()
      {
      return 2;
      }
      }

      ... invoking it somewhere else ...

      B obj = new B();
      MethodInfo method = typeof(A).GetMethod("Call", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
      int a = method.Invoke(this, null);

      the final value of a would be 2 and not 1. Anyone know how to get around this? (P.S. A page on the columbia university website details the default behavior[^] under the 'Remarks' section of Invoke ) Thanks in advance!

      r -€

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      That's how virtual methods work, even if you call them directly. obj.Call would return 2, not 1, so why should Method.Invoke work differently? It would amount to bypassing the virtual method mechanism, wouldn't it?

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      R 1 Reply Last reply
      0
      • S S Senthil Kumar

        That's how virtual methods work, even if you call them directly. obj.Call would return 2, not 1, so why should Method.Invoke work differently? It would amount to bypassing the virtual method mechanism, wouldn't it?

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        R Offline
        R Offline
        Roman Nurik
        wrote on last edited by
        #3

        Yes, that makes sense. But somehow, inside the method, doing something like

        base.Call()

        will actually invoke the superclass method. Is there any way to simulate that functionality outside of the overriden method call?

        r -€

        V 1 Reply Last reply
        0
        • R Roman Nurik

          Yes, that makes sense. But somehow, inside the method, doing something like

          base.Call()

          will actually invoke the superclass method. Is there any way to simulate that functionality outside of the overriden method call?

          r -€

          V Offline
          V Offline
          Vivek Sivasamy
          wrote on last edited by
          #4

          Yeah it is possible ... instead of overriding the function in the derived class use the key word new ..ie class B : A { public void new Call() { return 2; } } Now if u create a object of class A and call the Call() function it would call A's definition of Call and not B's Hope it helps

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups