title: Loading an Assembly if a certain Attribute is present
body: <p>Ok, here is the deal:</p>
  
  <p>I want to load a user defined <code>Assembly</code> into my <code>AppDomain</code>, <em>but</em> I only want to do so if the specified <code>Assembly</code> matches some requirements. In my case, it must have, among other requirements, an <code>Assembly</code> level <code>Attribute</code> we could call <code>MandatoryAssemblyAttribute</code>.</p>
  
  <p>There are two paths I can go as far as I see:</p>
  
  <ol>
  <li><p>Load the <code>Assembly</code> into my current AppDomain and check if the <code>Attribute</code> is present. Easy but inconvenient as I'm stuck with the loaded Assembly even if it doesnt have the <code>MandatoryAssemblyAttribute</code>. Not good.</p></li>
  <li><p>I can create a new <code>AppDomain</code> and load the <code>Assembly</code> from there and check if my good old <code>MandatoryAddemblyAttribute</code> is present. If it is, dump the created AppDomain and go ahead and load the <code>Assembly</code> into my <code>CurrentAppDomain</code>, if not, just dump the new <code>AppDomain</code>, tell the user, and have him try again.</p></li>
  </ol>
  
  <p>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:<a href="https://stackoverflow.com/questions/88717/loading-dlls-into-a-separate-appdomain">Loading DLLs into a separate AppDomain</a></p>
  
  <p>The problem I see with this solution is that you actually have to <em>know</em> a type (full name) in the <code>Assembly</code> you want to load to begin with. That is not a solution I like. The main point is trying to plug in an arbitrary <code>Assembly</code> that matches some requirements and through attributes discover what types to use. There is no knowledge beforehand of what types the <code>Assembly</code> will have. Of course I could make it a requirement that any <code>Assembly</code> meant to be used this way should implement some dummy class in order to offer an "entry point" for <code>CreateInstanceFromAndUnwrap</code>. I'd rather not.</p>
  
  <p>Also, if I go ahead and do something along this line:</p>
  
  <pre><code>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);
            }
       }
  }
  </code></pre>
  
  <p>This will work fine, but if then I go ahead and do the following:</p>
  
  <pre><code>foreach (var ass in domain.GetAssemblies()) //Do not fret, I would run this before unloading the AppDomain
      Console.WriteLine(ass.FullName); 
  </code></pre>
  
  <p>I get a <code>FileNotFoundException</code>. Why?</p>
  
  <p>Another path I could take is this one: <a href="http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/74d85ae0-b9bf-4e2b-bd83-e6ce0a855e56/" rel="nofollow noreferrer">How load DLL in separate AppDomain </a> But I'm not getting any luck either. I'm getting a <code>FileNotFoundException</code> whenever I choose some random .NET <code>Assembly</code> 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.</p>
  
  <p>Is there another way to do this barring <code>MEF</code> (I am not targeting .NET 3.5)? Or am I stuck with creating some dummy object in order to load the <code>Assembly</code> through <code>CreateInstanceFromAndUnwrap</code>? And if so, why can't I iterate through the loaded assemblies without getting a <code>FileNotFoundException</code>? What am I doing wrong?</p>
  
  <p>Many thanks for any advice.</p>