Conditional RunTime code
-
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.
-
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.
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.
-
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.
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 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.
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)
-
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...
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.
-
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)
It's managed and loaded once at a given time.
-
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.
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...
-
It's managed and loaded once at a given time.
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)
-
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...
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)
-
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.
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.