C# Reflection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Luiz Ragazzi

    C# Reflection

    Please, anyone can help on this:
    I need to call a method from my class
    but I need the information type at run time..as I know, I have to use
    GetType method of the object class.
    Any idea how to use this to call the method of my class ?

    Regards,
    Luiz
  • Jon Skeet [C# MVP]

    #2
    Re: C# Reflection

    Luiz Ragazzi <LuizRagazzi@di scussions.micro soft.com> wrote:[color=blue]
    > Please, anyone can help on this:
    > I need to call a method from my class
    > but I need the information type at run time..as I know, I have to use
    > GetType method of the object class.
    > Any idea how to use this to call the method of my class ?[/color]

    It's not at all clear what you're asking. What information do you
    already have? Could you provide a short but complete program which
    shows what you're trying to do, just with a line saying "I need to do
    <x> here" where you're stuck?

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

    • rossum

      #3
      Re: C# Reflection

      On Fri, 24 Jun 2005 09:22:02 -0700, Luiz Ragazzi
      <LuizRagazzi@di scussions.micro soft.com> wrote:
      [color=blue]
      >Please, anyone can help on this:
      >I need to call a method from my class
      >but I need the information type at run time..as I know, I have to use
      >GetType method of the object class.
      >Any idea how to use this to call the method of my class ?
      >
      >Regards,
      >Luiz[/color]

      If you want help specifically with C# you might get a better response
      on news://microsoft.public.dotnet.languages.csharp

      rossum


      The ultimate truth is that there is no ultimate truth

      Comment

      • Luiz Ragazzi

        #4
        Re: C# Reflection

        Hi Jon,
        please take a look:
        DataModel.DataL oader is my assembly, DmDataLoader.EU ROBONUS is my class
        I want to call the method DeleteAll from my class EUROBONUS.

        I created an object myobj and call CreateInstance:
        object myobj = new object();
        myobj =
        Activator.Creat eInstance("Data Model.DataLoade r","DmDataLoade r.EUROBONUS");

        line 1 and line 2 I've tryed the code:
        line1:
        myobj.GetType() .InvokeMember(" DeleteAll",Bind ingFlags.Public |
        BindingFlags.In vokeMethod,null ,myobj,new object[] {"euro" });
        the line 1: I got the message - "Method
        System.Runtime. Remoting.Object Handle.DeleteAl l not found."

        line 2:
        myobj.GetType() .GetMethod("Del eteAll",Binding Flags.Public |
        BindingFlags.Cr eateInstance | BindingFlags.In stance).Invoke( null,new
        object[] { "euro"});
        the line 2 I got "Object reference not set to an instance of an object."

        PS.: "euro" is a string parameter passed to the method DeleteAll

        thanks.

        "Jon Skeet [C# MVP]" wrote:
        [color=blue]
        > Luiz Ragazzi <LuizRagazzi@di scussions.micro soft.com> wrote:[color=green]
        > > Please, anyone can help on this:
        > > I need to call a method from my class
        > > but I need the information type at run time..as I know, I have to use
        > > GetType method of the object class.
        > > Any idea how to use this to call the method of my class ?[/color]
        >
        > It's not at all clear what you're asking. What information do you
        > already have? Could you provide a short but complete program which
        > shows what you're trying to do, just with a line saying "I need to do
        > <x> here" where you're stuck?
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: C# Reflection

          Luiz Ragazzi <LuizRagazzi@di scussions.micro soft.com> wrote:[color=blue]
          > please take a look:
          > DataModel.DataL oader is my assembly, DmDataLoader.EU ROBONUS is my class
          > I want to call the method DeleteAll from my class EUROBONUS.
          >
          > I created an object myobj and call CreateInstance:
          > object myobj = new object();[/color]

          Why bother creating an object which you're then just discarding?
          [color=blue]
          > myobj =
          > Activator.Creat eInstance("Data Model.DataLoade r","DmDataLoade r.EUROBONUS");[/color]

          And does that part work?
          [color=blue]
          > line 1 and line 2 I've tryed the code:
          > line1:
          > myobj.GetType() .InvokeMember(" DeleteAll",Bind ingFlags.Public |
          > BindingFlags.In vokeMethod,null ,myobj,new object[] {"euro" });
          > the line 1: I got the message - "Method
          > System.Runtime. Remoting.Object Handle.DeleteAl l not found."[/color]

          Here you don't include BindingFlags.In stance - put that in and you may
          well find it works.
          [color=blue]
          > line 2:
          > myobj.GetType() .GetMethod("Del eteAll",Binding Flags.Public |
          > BindingFlags.Cr eateInstance | BindingFlags.In stance).Invoke( null,new
          > object[] { "euro"});
          > the line 2 I got "Object reference not set to an instance of an object."[/color]

          Well, you don't want that, as that's asking for constructors rather
          than methods.
          [color=blue]
          > PS.: "euro" is a string parameter passed to the method DeleteAll[/color]

          If the above doesn't help, it would make things easier if you could
          provide a short but complete program which demonstrates the
          problem.

          See http://www.pobox.com/~skeet/csharp/complete.html for details of
          what I mean by that.

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