❔ 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
you want to exclude the entire library or just a class in the library?
I want to exlude the entire library - I will only ever use one of the two on a given machine
and how do you decide which to use?
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.
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
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
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
sorry, little bit out of my element here - what do you mean?
which part?
this
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
I might have to look more into using external dlls - I was under the impression that you have to use reflection to use them?
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
so if the dll isn't referenced, it won't be included in compilation?
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
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
np
thats how you learn new things :)
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.