Ok, here is the deal:
I want to load a custom Assembly into my AppDomain , but I want to do this only if the specified Assembly meets some requirements. In my case, it should have, among other requirements, an Assembly Attribute level, which we could call MandatoryAssemblyAttribute .
There are two ways that I can go, as far as I can see:
Download Assembly to my current AppDomain and check for Attribute . Easy but inconvenient as I am stuck in a loaded assembly, even if it does not have a MandatoryAssemblyAttribute . Not good.
I can create a new AppDomain and load Assembly there and check if my good old MandatoryAddemblyAttribute present. If so, dump the created AppDomain and go and load Assembly into my CurrentAppDomain , if not, just upload the new AppDomain , tell the user and ask him to try again.
Easier said than done. Searching the Internet, I found several examples on how to do this, including this previous question posted in SO: Loading a DLL into a separate AppDomain
The problem that I see in this solution is that you really need to know the type (full name) in Assembly that you want to load for a start. This is not a solution that I like. The main thing is to try to connect an arbitrary Assembly that meets some requirements, and through the attributes, find out which types to use. It is not known of what types Assembly will be. Of course, I could make this requirement that any Assembly intended to be used in this way must implement some dummy class in order to offer an entry point for CreateInstanceFromAndUnwrap . I would rather not.
Also, if I continue and do something along this line:
using (var frm = new OpenFileDialog()) { frm.DefaultExt = "dll"; frm.Title = "Select dll..."; frm.Filter = "Model files (*.dll)|*.dll"; answer = frm.ShowDialog(this); if (answer == DialogResult.OK) { domain = AppDomain.CreateDomain("Model", new Evidence(AppDomain.CurrentDomain.Evidence)); try { domain.CreateInstanceFrom(frm.FileName, "DummyNamespace.DummyObject"); modelIsValid = true; } catch (TypeLoadException) { ... } finally { if (domain != null) AppDomain.Unload(domain); } } }
This will work fine, but if I continue and do the following:
foreach (var ass in domain.GetAssemblies())
I get a FileNotFoundException . Why?
Another way I could take this is: How to load the DLL into a separate AppDomain But I'm also out of luck. I get a FileNotFoundException whenever I select a random .NET Assembly , and furthermore, it defeats the target, because I need to know the assembly name (not the file name) to load it that does not meet my requirements.
Is there any other way to do this by banning MEF (I am not configuring .NET 3.5). Or am I stuck in creating some kind of dummy object to load Assembly via CreateInstanceFromAndUnwrap ? And if so, why can't I FileNotFoundException over the loaded assemblies without getting a FileNotFoundException ? What am I doing wrong?
Thanks so much for any advice.