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. Conditional RunTime code

Conditional RunTime code

Scheduled Pinned Locked Moved C#
help
10 Posts 5 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.
  • N Offline
    N Offline
    NJdotnetdev
    wrote on last edited by
    #1

    I am implementing this for the first time, so need expert advise. Following is what I am trying to achieve: -- I have a dll which is called by the exe application from two different places. My aim is to not call certain loc in dll when it is called from the first location. I don't think conditional directives will help as they would not compile the code. I want to skip loc at runtime based on the call from the application. Dll will have an overloaded function implemented to distinguish between the calls.

    P OriginalGriffO L 3 Replies Last reply
    0
    • N NJdotnetdev

      I am implementing this for the first time, so need expert advise. Following is what I am trying to achieve: -- I have a dll which is called by the exe application from two different places. My aim is to not call certain loc in dll when it is called from the first location. I don't think conditional directives will help as they would not compile the code. I want to skip loc at runtime based on the call from the application. Dll will have an overloaded function implemented to distinguish between the calls.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If I were you, I'd use Dependency Injection and have something in the DLL trigger the relevant registrations depending on which app you are using.

      1 Reply Last reply
      0
      • N NJdotnetdev

        I am implementing this for the first time, so need expert advise. Following is what I am trying to achieve: -- I have a dll which is called by the exe application from two different places. My aim is to not call certain loc in dll when it is called from the first location. I don't think conditional directives will help as they would not compile the code. I want to skip loc at runtime based on the call from the application. Dll will have an overloaded function implemented to distinguish between the calls.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        One way to do it is to put the "conditional" code in the called method: you can find the Assembly it was called from using System.Reflection.Assembly.GetCallingAssembly[^] so if the two calls can be in different Assemblies that's easy. Or you could look at the calling method:

        StackTrace st = new StackTrace();
        string callingMethodName = st.GetFrame(1).GetMethod().Name;

        But they are both rather messy solutions.

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        N 1 Reply Last reply
        0
        • N NJdotnetdev

          I am implementing this for the first time, so need expert advise. Following is what I am trying to achieve: -- I have a dll which is called by the exe application from two different places. My aim is to not call certain loc in dll when it is called from the first location. I don't think conditional directives will help as they would not compile the code. I want to skip loc at runtime based on the call from the application. Dll will have an overloaded function implemented to distinguish between the calls.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          NJdotnetdev wrote:

          I have a dll which is called by the exe application from two different places.

          A managed or unmanaged library? Do note, you cannot load the same assembly twice in the same AppDomain.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          N 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            One way to do it is to put the "conditional" code in the called method: you can find the Assembly it was called from using System.Reflection.Assembly.GetCallingAssembly[^] so if the two calls can be in different Assemblies that's easy. Or you could look at the calling method:

            StackTrace st = new StackTrace();
            string callingMethodName = st.GetFrame(1).GetMethod().Name;

            But they are both rather messy solutions.

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            N Offline
            N Offline
            NJdotnetdev
            wrote on last edited by
            #5

            Please correct me if it's a bad approach: I was thinking to set a boolean flag variable/property in overloaded function. This variable/property can be defined in a common static class. Finally, use this flag to execute LOC selectively.

            OriginalGriffO D 2 Replies Last reply
            0
            • L Lost User

              NJdotnetdev wrote:

              I have a dll which is called by the exe application from two different places.

              A managed or unmanaged library? Do note, you cannot load the same assembly twice in the same AppDomain.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              N Offline
              N Offline
              NJdotnetdev
              wrote on last edited by
              #6

              It's managed and loaded once at a given time.

              L 1 Reply Last reply
              0
              • N NJdotnetdev

                Please correct me if it's a bad approach: I was thinking to set a boolean flag variable/property in overloaded function. This variable/property can be defined in a common static class. Finally, use this flag to execute LOC selectively.

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                It's not nice - if you have to use a static, it's probably a bad design! :laugh: And what happens if you have multiple threads, now or in the future? The approach fails, perhaps very badly. Instead, I'd provide an optional paramater:

                public void MyMethod(...list of parameters..., bool fullMethod = false)
                {
                }

                And then only call it with the full version when I needed it. Why are you trying to do this? It sounds as if you have control of the calling and called ends, so why do you need to complicate things this much?

                Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                L 1 Reply Last reply
                0
                • N NJdotnetdev

                  It's managed and loaded once at a given time.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Sorry, was thinking of the physical location of the assembly; you're referring to from where it is called in code. Since there is an overload, why not simply add another parameter to distinguish?

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    It's not nice - if you have to use a static, it's probably a bad design! :laugh: And what happens if you have multiple threads, now or in the future? The approach fails, perhaps very badly. Instead, I'd provide an optional paramater:

                    public void MyMethod(...list of parameters..., bool fullMethod = false)
                    {
                    }

                    And then only call it with the full version when I needed it. Why are you trying to do this? It sounds as if you have control of the calling and called ends, so why do you need to complicate things this much?

                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Hypot.. hypohth.. hyph.. imagine it to be a brownfield :) There is a succesfull application that suddenly needs branding; easily done, but what about the helper routines? Your executing assembly is not you. The calling one is not you. One needs to cater customer #1, the other a politician. How do you distinguish the green UI from the brown one? Higher up in the tree of assembly references, how would you know whether to output green or brown text? If there's some intermediate shared assemblies, things quickly escalate. So, if the stacktrace contains a reference to the entrypoint of the login, we know whether or not we are dealing with #2. If they are working on a licensing-scheme then all bets are off, offcourse.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    1 Reply Last reply
                    0
                    • N NJdotnetdev

                      Please correct me if it's a bad approach: I was thinking to set a boolean flag variable/property in overloaded function. This variable/property can be defined in a common static class. Finally, use this flag to execute LOC selectively.

                      D Offline
                      D Offline
                      David A Gray
                      wrote on last edited by
                      #10

                      I see nothing wrong with a static property, so long is its setter is protected by a mutex of some sort, which could be as simple as a lock ( object ) statement, where object is a private static object in the same class. (To avoid a deadlock, the object that gets locked must be separate from the property being set.) This is standard practice for a lazy initialized Singleton.

                      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