Reflection simple problem

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

    #1

    Reflection simple problem

    Dear friends,
    consider the following scenario, in Visual Studio 2005 and C#

    - I create a ASP.NET web service ("server") with a class MyClass and a
    WebMethod SomeMethod()
    - I create a client application, using "Add web reference..." and
    specifying some web reference name "MyName"

    Normally I would do:

    MyName.MyClass foo = new MyName.MyClass( );
    SomeType bar = foo.SomeMethod( );

    Now, instead, suppose that I have a STRING s = "MyName.MyClass " and
    that I want to be able to do the same as above. How can I do?

    Please, please provide some code. I know I need reflection and tried
    many examples by Googling the Internet, but I can't succeed...

  • Chris Shepherd

    #2
    Re: Reflection simple problem

    Thanks a lot for your help. Sorry to say it doesn't work...:-(

    It works fine here.
    This works:
    MyNameSpace.MyC lass foo = new MyNameSpace.MyC lass();
    string s = foo.GetString() ;
    >
    This doesn't work:
    Type myClassType = Type.GetType("M yNameSpace.MyCl ass");
    MethodInfo someMethodInfo =
    myClassType.Get Method("GetStri ng");
    Object instance = Activator.Creat eInstance(myCla ssType);
    string = (string)someMet hodInfo.Invoke( instance, null);
    >
    I'm not sure about the cast in the last line (maybe I should
    Convert.ToStrin g...?) but anyway, I commented out one line per time
    starting from the bottom, and I don't get runtime error only if I
    leave just Type myClassType = ... (1st line).
    First of all, your string = (string) line is flawed, since you don't
    have a variable you're assigning it to.

    The MethodInfo line just above works for me using Object and ToString.

    Here's an example:

    Type myType = Type.GetType("S ystem.Object");
    MethodInfo someInfo = myType.GetMetho d("ToString") ;
    Object instance = Activator.Creat eInstance(myTyp e);
    string s = someInfo.Invoke (instance, null).ToString( );


    Chris

    Comment

    • mpetrotta@gmail.com

      #3
      Re: Reflection simple problem

      On Sep 11, 8:04 am, Nightfall <chesbo...@gmai l.comwrote:
      Thanks a lot for your help. Sorry to say it doesn't work...:-(
      This works:
      MyNameSpace.MyC lass foo = new MyNameSpace.MyC lass();
      string s = foo.GetString() ;
      >
      This doesn't work:
      Type myClassType = Type.GetType("M yNameSpace.MyCl ass");
      MethodInfo someMethodInfo =
      myClassType.Get Method("GetStri ng");
      Object instance = Activator.Creat eInstance(myCla ssType);
      string = (string)someMet hodInfo.Invoke( instance, null);
      >
      I'm not sure about the cast in the last line (maybe I should
      Convert.ToStrin g...?) but anyway, I commented out one line per time
      starting from the bottom, and I don't get runtime error only if I
      leave just Type myClassType = ... (1st line).
      >
      The runtime error I get is Error 500 in the browser (internal server
      error).
      >
      Any hint?
      You're making things much, much too hard on yourself, if that's how
      you're debugging. I suggest setting ASP.NET aside for the moment, and
      concentrate on reflection. When you've got that working, then you can
      integrate it into your web app.

      Take my original code, which runs just fine, and create a console
      application with it. Run it, then adapt it for your classes. Get it
      to work as a console app. Then, adapt it for your web app.

      You also need to get ASP.NET to give you better error reporting. I
      don't do much web development, but the ASP.NET apps I've put together
      will report the actual exception, with a stack trace, when I get an
      error. If you're not connecting from the same machine that IIS is
      running on, then you'll need to modify your web.config's customErrors
      line:<customErr ors mode="Off" />

      If you run into a problem at some point, post a complete program that
      reproduces the problem (I should be able to copy and paste into a new
      project, and run it). This takes some effort, but often in the
      process of simplifying your code for posting, you'll have a Eureka
      moment and realize what the problem is.

      Good luck,

      Michael


      Comment

      • almir

        #4
        Re: Reflection simple problem

        Hi,

        I had a similar problem with asp.net, so to awoid this problem try
        your code first in a simple windows or comand-prompt application, it
        will take you only few lines of code, if it works there, which should
        be easy if you follow the steps abowe try it in asp.net. there it will
        probably not work the problem is in asp.net compiler which compiles
        each file or directory in asp.net in a differen assembly, thus whan
        you start searching for class with a given name you get null as return
        as the class you are looking for is in a different assembly, to avoid
        this problem get a reference to a clas with
        typeof(MyClassF romWebReference ).GetAssembly() ; than with the reference
        to this assembly try finding class with given name, this should work
        than, if not drop me a mail with your code i ll try to send you a
        working reply.

        eventually try activator.getin stance -or somthing like that maybe this
        will give you better results


        good luck
        almir

        On Sep 11, 6:06 pm, "mpetro...@gmai l.com" <mpetro...@gmai l.comwrote:
        On Sep 11, 8:04 am, Nightfall <chesbo...@gmai l.comwrote:
        >
        >
        >
        >
        >
        Thanks a lot for your help. Sorry to say it doesn't work...:-(
        This works:
        MyNameSpace.MyC lass foo = new MyNameSpace.MyC lass();
        string s = foo.GetString() ;
        >
        This doesn't work:
        Type myClassType = Type.GetType("M yNameSpace.MyCl ass");
        MethodInfo someMethodInfo =
        myClassType.Get Method("GetStri ng");
        Object instance = Activator.Creat eInstance(myCla ssType);
        string = (string)someMet hodInfo.Invoke( instance, null);
        >
        I'm not sure about the cast in the last line (maybe I should
        Convert.ToStrin g...?) but anyway, I commented out one line per time
        starting from the bottom, and I don't get runtime error only if I
        leave just Type myClassType = ... (1st line).
        >
        The runtime error I get is Error 500 in the browser (internal server
        error).
        >
        Any hint?
        >
        You're making things much, much too hard on yourself, if that's how
        you're debugging. I suggest setting ASP.NET aside for the moment, and
        concentrate on reflection. When you've got that working, then you can
        integrate it into your web app.
        >
        Take my original code, which runs just fine, and create a console
        application with it. Run it, then adapt it for your classes. Get it
        to work as a console app. Then, adapt it for your web app.
        >
        You also need to get ASP.NET to give you better error reporting. I
        don't do much web development, but the ASP.NET apps I've put together
        will report the actual exception, with a stack trace, when I get an
        error. If you're not connecting from the same machine that IIS is
        running on, then you'll need to modify your web.config's customErrors
        line:<customErr ors mode="Off" />
        >
        If you run into a problem at some point, post a complete program that
        reproduces the problem (I should be able to copy and paste into a new
        project, and run it). This takes some effort, but often in the
        process of simplifying your code for posting, you'll have a Eureka
        moment and realize what the problem is.
        >
        Good luck,
        >
        Michael- Hide quoted text -
        >
        - Show quoted text -

        Comment

        • Nightfall

          #5
          Re: Reflection simple problem

          On 12 Set, 00:24, almir <kaza...@gmail. comwrote:
          Hi,
          >
          I had a similar problem with asp.net, so to awoid this problem try
          your code first in a simple windows or comand-prompt application, it
          will take you only few lines of code, if it works there, which should
          be easy if you follow the steps abowe try it in asp.net. there it will
          probably not work the problem is in asp.net compiler which compiles
          each file or directory in asp.net in a differen assembly, thus whan
          you start searching for class with a given name you get null as return
          as the class you are looking for is in a different assembly, to avoid
          this problem get a reference to a clas with
          typeof(MyClassF romWebReference ).GetAssembly() ; than with the reference
          to this assembly try finding class with given name, this should work
          than, if not drop me a mail with your code i ll try to send you a
          working reply.
          >
          eventually try activator.getin stance -or somthing like that maybe this
          will give you better results
          >
          good luck
          almir
          >
          On Sep 11, 6:06 pm, "mpetro...@gmai l.com" <mpetro...@gmai l.comwrote:
          >
          >
          >
          On Sep 11, 8:04 am, Nightfall <chesbo...@gmai l.comwrote:
          >
          Thanks a lot for your help. Sorry to say it doesn't work...:-(
          This works:
          MyNameSpace.MyC lass foo = new MyNameSpace.MyC lass();
          string s = foo.GetString() ;
          >
          This doesn't work:
          Type myClassType = Type.GetType("M yNameSpace.MyCl ass");
          MethodInfo someMethodInfo =
          myClassType.Get Method("GetStri ng");
          Object instance = Activator.Creat eInstance(myCla ssType);
          string = (string)someMet hodInfo.Invoke( instance, null);
          >
          I'm not sure about the cast in the last line (maybe I should
          Convert.ToStrin g...?) but anyway, I commented out one line per time
          starting from the bottom, and I don't get runtime error only if I
          leave just Type myClassType = ... (1st line).
          >
          The runtime error I get is Error 500 in the browser (internal server
          error).
          >
          Any hint?
          >
          You're making things much, much too hard on yourself, if that's how
          you're debugging. I suggest setting ASP.NET aside for the moment, and
          concentrate on reflection. When you've got that working, then you can
          integrate it into your web app.
          >
          Take my original code, which runs just fine, and create a console
          application with it. Run it, then adapt it for your classes. Get it
          to work as a console app. Then, adapt it for your web app.
          >
          You also need to get ASP.NET to give you better error reporting. I
          don't do much web development, but the ASP.NET apps I've put together
          will report the actual exception, with a stack trace, when I get an
          error. If you're not connecting from the same machine that IIS is
          running on, then you'll need to modify your web.config's customErrors
          line:<customErr ors mode="Off" />
          >
          If you run into a problem at some point, post a complete program that
          reproduces the problem (I should be able to copy and paste into a new
          project, and run it). This takes some effort, but often in the
          process of simplifying your code for posting, you'll have a Eureka
          moment and realize what the problem is.
          >
          Good luck,
          >
          Michael- Hide quoted text -
          >
          - Show quoted text -- Nascondi testo tra virgolette -
          >
          - Mostra testo tra virgolette -
          So... almir you got there...
          I wrote an email to you, but I explain what's happening here, too.

          In my CLIENT code I wrote

          Type myClassType = Type.GetType("S omeName.WSServe r");
          MethodInfo someMethodInfo =
          myClassType.Get Method("HelloWo rld");
          Object instance = Activator.Creat eInstance(myCla ssType);
          string s = someMethodInfo. Invoke(instance ,
          null).ToString( );

          SomeName.WSServ er is the NameSpace.Class Name for a class provided via
          "Add web reference..." to my client.
          In this case, I get a null reference back, and the application hangs.

          If I write instead
          Type myClassType = typeof(SomeName .WSServer);
          everything works fine.

          Everything works fine (obviously, I suppose) even if SomeName.WSServ er
          is NOT a Web Reference, but a "local" class, that is, a class inside
          the same project as the client.

          So I guess the problem is just in invoking Type.GetType with a Web
          reference as argument, and this is really too hard to understand for
          me :-(((

          Bye bye

          Comment

          • Nightfall

            #6
            Re: Reflection simple problem

            On 12 Set, 11:07, Nightfall <chesbo...@gmai l.comwrote:
            On 12 Set, 00:24, almir <kaza...@gmail. comwrote:
            >
            >
            >
            >
            >
            Hi,
            >
            I had a similar problem with asp.net, so to awoid this problem try
            your code first in a simple windows or comand-prompt application, it
            will take you only few lines of code, if it works there, which should
            be easy if you follow the steps abowe try it in asp.net. there it will
            probably not work the problem is in asp.net compiler which compiles
            each file or directory in asp.net in a differen assembly, thus whan
            you start searching for class with a given name you get null as return
            as the class you are looking for is in a different assembly, to avoid
            this problem get a reference to a clas with
            typeof(MyClassF romWebReference ).GetAssembly() ; than with the reference
            to this assembly try finding class with given name, this should work
            than, if not drop me a mail with your code i ll try to send you a
            working reply.
            >
            eventually try activator.getin stance -or somthing like that maybe this
            will give you better results
            >
            good luck
            almir
            >
            On Sep 11, 6:06 pm, "mpetro...@gmai l.com" <mpetro...@gmai l.comwrote:
            >
            On Sep 11, 8:04 am, Nightfall <chesbo...@gmai l.comwrote:
            >
            Thanks a lot for your help. Sorry to say it doesn't work...:-(
            This works:
            MyNameSpace.MyC lass foo = new MyNameSpace.MyC lass();
            string s = foo.GetString() ;
            >
            This doesn't work:
            Type myClassType = Type.GetType("M yNameSpace.MyCl ass");
            MethodInfo someMethodInfo =
            myClassType.Get Method("GetStri ng");
            Object instance = Activator.Creat eInstance(myCla ssType);
            string = (string)someMet hodInfo.Invoke( instance, null);
            >
            I'm not sure about the cast in the last line (maybe I should
            Convert.ToStrin g...?) but anyway, I commented out one line per time
            starting from the bottom, and I don't get runtime error only if I
            leave just Type myClassType = ... (1st line).
            >
            The runtime error I get is Error 500 in the browser (internal server
            error).
            >
            Any hint?
            >
            You're making things much, much too hard on yourself, if that's how
            you're debugging. I suggest setting ASP.NET aside for the moment, and
            concentrate on reflection. When you've got that working, then you can
            integrate it into your web app.
            >
            Take my original code, which runs just fine, and create a console
            application with it. Run it, then adapt it for your classes. Get it
            to work as a console app. Then, adapt it for your web app.
            >
            You also need to get ASP.NET to give you better error reporting. I
            don't do much web development, but the ASP.NET apps I've put together
            will report the actual exception, with a stack trace, when I get an
            error. If you're not connecting from the same machine that IIS is
            running on, then you'll need to modify your web.config's customErrors
            line:<customErr ors mode="Off" />
            >
            If you run into a problem at some point, post a complete program that
            reproduces the problem (I should be able to copy and paste into a new
            project, and run it). This takes some effort, but often in the
            process of simplifying your code for posting, you'll have a Eureka
            moment and realize what the problem is.
            >
            Good luck,
            >
            Michael- Hide quoted text -
            >
            - Show quoted text -- Nascondi testo tra virgolette -
            >
            - Mostra testo tra virgolette -
            >
            So... almir you got there...
            I wrote an email to you, but I explain what's happening here, too.
            >
            In my CLIENT code I wrote
            >
            Type myClassType = Type.GetType("S omeName.WSServe r");
            MethodInfo someMethodInfo =
            myClassType.Get Method("HelloWo rld");
            Object instance = Activator.Creat eInstance(myCla ssType);
            string s = someMethodInfo. Invoke(instance ,
            null).ToString( );
            >
            SomeName.WSServ er is the NameSpace.Class Name for a class provided via
            "Add web reference..." to my client.
            In this case, I get a null reference back, and the application hangs.
            >
            If I write instead
            Type myClassType = typeof(SomeName .WSServer);
            everything works fine.
            >
            Everything works fine (obviously, I suppose) even if SomeName.WSServ er
            is NOT a Web Reference, but a "local" class, that is, a class inside
            the same project as the client.
            >
            So I guess the problem is just in invoking Type.GetType with a Web
            reference as argument, and this is really too hard to understand for
            me :-(((
            >
            Bye bye- Nascondi testo tra virgolette -
            >
            - Mostra testo tra virgolette -
            Well... it seems I found the solution:

            instead of Type myClassType = Type.GetType...
            I'm using

            Type myClassType =
            System.Web.Comp ilation.BuildMa nager.GetType(" SomeName.WSServ er",
            true);

            and everything is working fine :-)))

            Thanx a lot to everybody anyway.

            Bye!

            Comment

            Working...