Unable to invoking method via reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cartoper@gmail.com

    #1

    Unable to invoking method via reflection

    I am currently doing some R&D. The objective is to learn how to invoke
    methods via reflection using the InvokeMember method. The InvokeMember
    method throws an exception:

    Method 'ReflectionTest .Originator.Add ToA' not found.

    Here is the code:

    using System;
    using System.Diagnost ics;
    using System.Collecti ons.Generic;
    using System.Reflecti on;
    using System.Text;

    namespace ReflectionTest
    {
    public class Originator
    {
    private int a;

    public Originator(int a)
    {
    this.a = a;
    }

    public int AddToA(int value)
    {
    a += value;
    return a;
    }
    }

    public class Program
    {
    static void Main(string[] args)
    {
    object obj = new Originator(5);

    BindingFlags flags =
    BindingFlags.Pu blic |
    BindingFlags.In vokeMethod |
    BindingFlags.In stance;

    object result = obj.GetType().I nvokeMember(
    "AddToA", flags, null, obj, new object[] { "5" });

    Debug.WriteLine (result.ToStrin g());
    }
    }
    }

  • Jon Skeet [C# MVP]

    #2
    Re: Unable to invoking method via reflection

    <cartoper@gmail .com> wrote:[color=blue]
    > I am currently doing some R&D. The objective is to learn how to invoke
    > methods via reflection using the InvokeMember method. The InvokeMember
    > method throws an exception:[/color]

    <snip>
    [color=blue]
    > public int AddToA(int value)[/color]

    <snip>
    [color=blue]
    > object result = obj.GetType().I nvokeMember(
    > "AddToA", flags, null, obj, new object[] { "5" });[/color]

    You're effectively trying to call AddToA("5").

    "5" is not an int, it's a string. If you change "5" to 5, the code
    works.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • chris martin

      #3
      Re: Unable to invoking method via reflection

      > I am currently doing some R&D. The objective is to learn how to[color=blue]
      > invoke methods via reflection using the InvokeMember method. The
      > InvokeMember method throws an exception:
      >
      > Method 'ReflectionTest .Originator.Add ToA' not found.
      >
      > Here is the code:
      >
      > using System;
      > using System.Diagnost ics;
      > using System.Collecti ons.Generic;
      > using System.Reflecti on;
      > using System.Text;
      > namespace ReflectionTest
      > {
      > public class Originator
      > {
      > private int a;
      > public Originator(int a)
      > {
      > this.a = a;
      > }
      > public int AddToA(int value)
      > {
      > a += value;
      > return a;
      > }
      > }
      > public class Program
      > {
      > static void Main(string[] args)
      > {
      > object obj = new Originator(5);
      > BindingFlags flags =
      > BindingFlags.Pu blic |
      > BindingFlags.In vokeMethod |
      > BindingFlags.In stance;
      > object result = obj.GetType().I nvokeMember(
      > "AddToA", flags, null, obj, new object[] { "5" });
      > Debug.WriteLine (result.ToStrin g());
      > }
      > }
      > }[/color]


      Changing the line:
      object result = obj.GetType().I nvokeMember(
      "AddToA", flags, null, obj, new object[] { "5" });

      to
      object result = obj.GetType().I nvokeMember(
      "AddToA", flags, null, obj, new object[] { 5 });

      should do it. I didn't test it though.


      Comment

      • Mattias Sjögren

        #4
        Re: Unable to invoking method via reflection

        >Method 'ReflectionTest .Originator.Add ToA' not found.
        ....[color=blue]
        > object result = obj.GetType().I nvokeMember(
        > "AddToA", flags, null, obj, new object[] { "5" });[/color]

        The argument you're providing is a string so it will look for a
        AddToA(string) method, not your AddToA(int). Try changing "5" to 5.


        Mattias

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        Working...