typeof

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

    typeof

    Hi

    is there a difference between "this.GetType() " and typeof(MyClass) ?

    The only reason I ask is that we are using log4net in a project, and we give
    the Loggers names based on the class name.

    For example, some people use:
    ILog log = LogManager.GetL ogger(typeof(Fi nder).ToString( ));
    others use:
    ILog log = LogManager.GetL ogger(this.GetT ype().ToString( ));

    Is there any difference?

    Thanks,
    Peter


  • Joanna Carter \(TeamB\)

    #2
    Re: typeof

    "Peter Kirk" <pk@alpha-solutions.dk> a écrit dans le message de news:
    #PviwjkcFHA.155 6@TK2MSFTNGP10. phx.gbl...
    [color=blue]
    > is there a difference between "this.GetType() " and typeof(MyClass) ?[/color]

    GetType() only works on instances, typeof only works on types.

    Joanna

    --
    Joanna Carter
    Consultant Software Engineer


    Comment

    • Helge Jensen

      #3
      Re: typeof



      Peter Kirk wrote:[color=blue]
      > Hi
      >
      > is there a difference between "this.GetType() " and typeof(MyClass) ?[/color]

      this.GetType() is virtual, and will give you the type of this instance.
      typeof(MyClass) is static and will give you the type of MyClass.

      The difference is apparent here:

      class Bar {
      string TestType() {
      return string.format(" this.GetType()= {0}, typeof(Bar)={1} ",
      this.GetType(), typeof(Bar)); }
      }
      class Foo: Bar {}

      Console.Writeli ne(new Foo().TestType( ));
      [color=blue]
      > For example, some people use:
      > ILog log = LogManager.GetL ogger(typeof(Fi nder).ToString( ));
      > others use:
      > ILog log = LogManager.GetL ogger(this.GetT ype().ToString( ));
      >
      > Is there any difference?[/color]

      Yes, and even worse... it is significant, since typeof() and GetType()
      will put log-messages from Foo in different loggers.

      BTW: You cannot argue which method is "best" in general for your
      purpose, both would probably have their valid uses.

      --
      Helge Jensen
      mailto:helge.je nsen@slog.dk
      sip:helge.jense n@slog.dk
      -=> Sebastian cover-music: http://ungdomshus.nu <=-

      Comment

      • Peter Kirk

        #4
        Re: typeof

        "Helge Jensen" <helge.jensen@s log.dk> skrev i en meddelelse
        news:42B13F80.9 080908@slog.dk. ..[color=blue]
        > class Bar {
        > string TestType() {
        > return string.format(" this.GetType()= {0}, typeof(Bar)={1} ",
        > this.GetType(), typeof(Bar)); }
        > }
        > class Foo: Bar {}
        >
        > Console.Writeli ne(new Foo().TestType( ));
        >[color=green]
        >> For example, some people use:
        >> ILog log = LogManager.GetL ogger(typeof(Fi nder).ToString( ));
        >> others use:
        >> ILog log = LogManager.GetL ogger(this.GetT ype().ToString( ));
        >>
        >> Is there any difference?[/color]
        >
        > Yes, and even worse... it is significant, since typeof() and GetType()
        > will put log-messages from Foo in different loggers.
        >
        > BTW: You cannot argue which method is "best" in general for your purpose,
        > both would probably have their valid uses.[/color]

        OK, I can see arguments for both sides.

        With respect to log4net, then, what are good, consistent naming conventions
        for loggers?


        Comment

        • Cowboy (Gregory A. Beamer) - MVP

          #5
          RE: typeof

          GetType tells the type of a particular instance of a class. typeof() uses
          reflection to look at the actual underlying type. From the standpoint of the
          code, the main difference is whether or not you are looking at an instance of
          a class to determine the logger or you are working without an instance and
          creating one based on a specific type.

          Hope this helps.

          --
          Gregory A. Beamer
          MVP; MCP: +I, SE, SD, DBA

          *************** ************
          Think Outside the Box!
          *************** ************


          "Peter Kirk" wrote:
          [color=blue]
          > Hi
          >
          > is there a difference between "this.GetType() " and typeof(MyClass) ?
          >
          > The only reason I ask is that we are using log4net in a project, and we give
          > the Loggers names based on the class name.
          >
          > For example, some people use:
          > ILog log = LogManager.GetL ogger(typeof(Fi nder).ToString( ));
          > others use:
          > ILog log = LogManager.GetL ogger(this.GetT ype().ToString( ));
          >
          > Is there any difference?
          >
          > Thanks,
          > Peter
          >
          >
          >[/color]

          Comment

          Working...