Ok, here is the deal:
I want to load a user defined Assembly
into my AppDomain
, but I only want to do so if the specified Assembly
matches some requirements. In my case, it must have, among other requirements, an Assembly
level Attribute
we could call MandatoryAssemblyAttribute
.
There are two paths I can go as far as I see:
Load the
Assembly
into my current AppDomain and check if theAttribute
is present. Easy but inconvenient as I'm stuck with the loaded Assembly even if it doesnt have theMandatoryAssemblyAttribute
. Not good.I can create a new
AppDomain
and load theAssembly
from there and check if my good oldMandatoryAddemblyAttribute
is present. If it is, dump the created AppDomain and go ahead and load theAssembly
into myCurrentAppDomain
, if not, just dump the newAppDomain
, tell the user, and have him try again.
Easier said than done. Searching the web, I've found a couple of examples on how to go about this, including this previous question posted in SO:Loading DLLs into a separate AppDomain
The problem I see with this solution is that you actually have to know a type (full name) in the Assembly
you want to load to begin with. That is not a solution I like. The main point is trying to plug in an arbitrary Assembly
that matches some requirements and through attributes discover what types to use. There is no knowledge beforehand of what types the Assembly
will have. Of course I could make it a requirement that any Assembly
meant to be used this way should implement some dummy class in order to offer an "entry point" for CreateInstanceFromAndUnwrap
. I'd rather not.
Also, if I go ahead 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 then I go ahead and do the following:
foreach (var ass in domain.GetAssemblies()) //Do not fret, I would run this before unloading the AppDomain
Console.WriteLine(ass.FullName);
I get a FileNotFoundException
. Why?
Another path I could take is this one: How load DLL in separate AppDomain But I'm not getting any luck either. I'm getting a FileNotFoundException
whenever I choose some random .NET Assembly
and besides it defeats the purpose as I need to know the Assembly's name (not file name) in order to load it up which doesn't match my requirements.
Is there another way to do this barring MEF
(I am not targeting .NET 3.5)? Or am I stuck with creating some dummy object in order to load the Assembly
through CreateInstanceFromAndUnwrap
? And if so, why can't I iterate through the loaded assemblies without getting a FileNotFoundException
? What am I doing wrong?
Many thanks for any advice.