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

Reflection

Scheduled Pinned Locked Moved C#
question
7 Posts 4 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.
  • J Offline
    J Offline
    Jammer 0
    wrote on last edited by
    #1

    Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,

    Jammer Going where everyone here has gone before! :) My Blog

    B B 2 Replies Last reply
    0
    • J Jammer 0

      Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,

      Jammer Going where everyone here has gone before! :) My Blog

      B Offline
      B Offline
      Bert delaVega
      wrote on last edited by
      #2

      You could pass it to the constructor for the new object and then use the System.Object.GetType() method to get it's type (of the object that constructed it) and do further processing from there. There's a bunch of different ways. And it depends on how many "calling" objects you're dealing with.

      J 1 Reply Last reply
      0
      • B Bert delaVega

        You could pass it to the constructor for the new object and then use the System.Object.GetType() method to get it's type (of the object that constructed it) and do further processing from there. There's a bunch of different ways. And it depends on how many "calling" objects you're dealing with.

        J Offline
        J Offline
        Jammer 0
        wrote on last edited by
        #3

        Thanks Bert, It does make more sense to pass it in and then build some form of switch statement I guess. Thanks,

        Jammer Going where everyone here has gone before! :) My Blog

        1 Reply Last reply
        0
        • J Jammer 0

          Hi All, I've not used reflection before so this is all really new to me. I have an object that I want to build in some automating dynamic behaviour based on what 'called it'. What's the best way to get the Type name of the calling type in this objects constructor? Cheers,

          Jammer Going where everyone here has gone before! :) My Blog

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #4

          You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.

          J V 2 Replies Last reply
          0
          • B Brady Kelly

            You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.

            J Offline
            J Offline
            Jammer 0
            wrote on last edited by
            #5

            Ahhhhh!! Thanks chap!

            Jammer Going where everyone here has gone before! :) My Blog

            1 Reply Last reply
            0
            • B Brady Kelly

              You can look at the collection of StackFrame objects returned by System.Diagnostics.StackTrace. The first frame should be your constructor, and the second the method that called it.

              V Offline
              V Offline
              Vega02
              wrote on last edited by
              #6

              Unfortunately this does not work in all cases and is not guaranteed to work in future versions of the framework. See Mike Stall's blog [^]for more info. The question one should be asking is "why does the constructor need to know the declaring type of its calling method?"

              B 1 Reply Last reply
              0
              • V Vega02

                Unfortunately this does not work in all cases and is not guaranteed to work in future versions of the framework. See Mike Stall's blog [^]for more info. The question one should be asking is "why does the constructor need to know the declaring type of its calling method?"

                B Offline
                B Offline
                Brady Kelly
                wrote on last edited by
                #7

                Thanks, I didn't know that. Ya, if the question is unavoidable, I suppose passing the result of this.GetType() to the constructor is nice and clear about what is going on.

                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