<p>I'm writing a utility for myself, partly as an exercise in learning C# Reflection and partly because I actually want the resulting tool for my own use.</p>

<p>What I'm after is basically pointing the application at an assembly and choosing a given class from which to select properties that should be included in an exported HTML form as fields. That form will be then used in my ASP.NET MVC app as the beginning of a View.</p>

<p>As I'm using Subsonic objects for the applications where I want to use, this should be reasonable and I figured that, by wanting to include things like differing output HTML depending on data type, Reflection was the way to get this done.</p>

<p>What I'm looking for, however, seems to be elusive. I'm trying to take the DLL/EXE that's chosen through the OpenFileDialog as the starting point and load it:</p>

<pre><code>String FilePath = Path.GetDirectoryName(FileName);
System.Reflection.Assembly o = System.Reflection.Assembly.LoadFile(FileName);
</code></pre>

<p>That works fine, but because Subsonic-generated objects actually are full of object types that are defined in Subsonic.dll, etc., those dependent objects aren't loaded. Enter:</p>

<pre><code>AssemblyName[] ReferencedAssemblies = o.GetReferencedAssemblies();
</code></pre>

<p>That, too, contains exactly what I would expect it to. However, what I'm trying to figure out is how to load those assemblies so that my digging into my objects will work properly. I understand that if those assemblies were in the GAC or in the directory of the running executable, I could just load them by their name, but that isn't likely to be the case for this use case and it's my primary use case.</p>

<p>So, what it boils down to is how do I load a given assembly and all of its arbitrary assemblies starting with a filename and resulting in a completely Reflection-browsable tree of types, properties, methods, etc.</p>

<p>I know that tools like Reflector do this, I just can't find the syntax for getting at it. </p>

[{'answer_id': 59243, 'author': 'Kent Boogaart', 'author_id': 5380, 'author_profile': 'https://Stackoverflow.com/users/5380', 'pm_score': 5, 'selected': True, 'text': "<p>Couple of options here:</p>\n\n<ol>\n<li>Attach to <code>AppDomain.AssemblyResolve</code> and do another <code>LoadFile</code> based on the requested assembly.</li>\n<li>Spin up another <code>AppDomain</code> with the directory as its base and load the assemblies in that <code>AppDomain</code>.</li>\n</ol>\n\n<p>I'd highly recommend pursuing option 2, since that will likely be cleaner and allow you to unload all those assemblies after. Also, consider loading assemblies in the reflection-only context if you only need to reflect over them (see <code>Assembly.ReflectionOnlyLoad</code>).</p>\n"}, {'answer_id': 37060921, 'author': 'mathume', 'author_id': 400694, 'author_profile': 'https://Stackoverflow.com/users/400694', 'pm_score': 3, 'selected': False, 'text': '<p>I worked out <a href="https://stackoverflow.com/users/5380/kent-boogaart">Kent Boogaart</a>\'s second option.\nEssentially I had to:</p>\n\n<p>1.) Implement the <code>ResolveEventHandler</code> in a separate class, inheriting from <code>MarshalByRefObject</code> and adding the <code>Serializable</code> attribute.</p>\n\n<p>2.) Add the current <code>ApplicationBase</code>, essentially where the event handler\'s dll is, to the <code>AppDomain</code> <code>PrivateBinPath</code>.</p>\n\n<p>You can find the code on <a href="https://github.com/mathume/ResolveDependencies" rel="nofollow noreferrer">github</a>.</p>\n'}]
['https://Stackoverflow.com/questions/59220', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1124/']