C
C#8mo ago
Drama Llama

❔ How to optionally include certain classes/libraries in a compile

So what I want to do is write an application that uses one of two external libraries, sometimes using the first and sometimes using the second. Both of these libraries are quite big, so ideally I'd only include the one that is needed, or build two versions of the app, one with the first library and one with the second. Currently my approach is to have all the relevant code using said library each in it's own class, then put those classes into their own .dll file and access it via reflection during runtime. That way, I can "ship" this with only the singular .dll file that is needed. Are there better ways? Reflection seems a bit like a pain when it comes to readability and reliability. So far I've thought about preprocessing directives (can I just exclude the entire class using one of the libraries and hopefully the library too?) and dependency injection (this came up during my search but I have never worked with it so I don't know if this will be helpful).
18 Replies
sibber
sibber8mo ago
you want to exclude the entire library or just a class in the library?
Drama Llama
Drama Llama8mo ago
I want to exlude the entire library - I will only ever use one of the two on a given machine
sibber
sibber8mo ago
and how do you decide which to use?
Drama Llama
Drama Llama8mo ago
The information as to which one of the two will be required will be available ahead of time. Think like I am either building for windows 7 or windows 8 - and needing two different libraries to make it work for each OS. In this sample scenario, I'd allow the user to just choose the version that's applicable to them.
sibber
sibber8mo ago
so the user will choose during installation you can just include both in the installer and have it only install the one the user chooses if your app never attempts to load the dll nothing will happen but they might depend on how its written
Drama Llama
Drama Llama8mo ago
That's a great thought, but I need the decision point to be earlier than that. :( it's not going to be a thing that uses an installer. plus size is a consideration
sibber
sibber8mo ago
this is something that can be easily done with dependency injection, but given that youre not familiar with it and have already written your app, itll probably be a lot of work ah so at build time? just build it twice then you don't need reflection for that .net loads the dlls when needed if you have it at compile time that is
Drama Llama
Drama Llama8mo ago
sorry, little bit out of my element here - what do you mean?
sibber
sibber8mo ago
which part?
Drama Llama
Drama Llama8mo ago
this
sibber
sibber8mo ago
it lazy loads them if you reference a dll and never use anything from it, it wont get loaded in other words, you can delete it
Drama Llama
Drama Llama8mo ago
I might have to look more into using external dlls - I was under the impression that you have to use reflection to use them?
sibber
sibber8mo ago
well all projects you reference are compiled to dlls you have to use reflection if you want to load a dll that you dont have access to at compile time e.g. plugins
Drama Llama
Drama Llama8mo ago
so if the dll isn't referenced, it won't be included in compilation?
sibber
sibber8mo ago
yeah but what i was saying is if it is referenced and included in the compilation, but your app never reaches a point where it calls something from it, it wont load it that means you can delete it, even if it was included and if you do delete, and your app does use it at some point, the app will work fine until that point then crash
Drama Llama
Drama Llama8mo ago
oh, that's promising! only question then would be where the external libraries would be at. I can probably find that out like file-wise thank you for your help and sorry about me phrasing things poorly, I'm certainly punching above my weight class here
sibber
sibber8mo ago
np thats how you learn new things :)
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts