Skip to content

Gets assembly information at run-time & dynamically load / unload assemblies for validation.

License

Notifications You must be signed in to change notification settings

Latency/AssemblyLoader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AssemblyLoader

Assembly Information & Loader via Reflection


Description
CREATED BY: [Latency McLaughlin]
UPDATED: 9/17/2024
FRAMEWORK: net452, netstandard2.0, netstandard2.1, net9.0
LANGUAGE: [C#] preview
OUTPUT TYPE: Library [API]
SUPPORTS: [Visual Studio]
GFX SUBSYS: [None]
TAGS: [AssemblyLoader C# AssemblyInfo]
STATUS:
LICENSE:
VERSION:

Navigation


Gets assembly information at runtime commonly found in Properties within .NET assemblies. Dynamically loads / unloads assemblies and their dependencies within a proxy for validation.

Trying to get information out of an assembly without loading it into the current application domain is not that simple. There is no way to get custom assembly attributes without loading it into the current AppDomain. There is a special assembly loading method, Assembly.ReflectionOnlyLoad(), which uses a "reflection-only" load context. This lets you load assemblies that cannot be executed, but can have their metadata read. You cannot get typed attributes from this kind of assembly, only CustomAttributeData. That class doesn't provide any good way to filter for a specific attribute.

Even worse, a reflection-only load does not load any dependency assemblies, but it forces you to do it manually. That makes it objectively worse than Assembly.Load() which at least gets the dependencies to load automatically.

MEF includes a whole ton of custom reflection code to make this work.

Ultimately, you cannot unload an assembly once it has been loaded. You need to unload the entire app domain, as described in this MSDN article.

Based on the following premises:

Creating an assembly proxy (or wrapper), derived from MarshalByRefObject, so that the CLR can marshal it by reference across AppDomain boundaries. Loading the assembly within this proxy. Performing the reflection inside this proxy and return the data you need. Creating a temporary AppDomain and instantiating the assembly proxy in this AppDomain (AppDomain.CreateInstanceFrom). Unloading the AppDomain as soon as you finished reflecting.

However, you have to keep in mind that reflection on the assembly loaded this way is only possible inside the proxy (the one derived from MarshalByRefObject). It is not possible to return any "reflection object" (anything defined in the System.Reflection namespace, such as Type, MethodInfo, etc.). Trying to access these from another AppDomain (the caller's domain) would result in exceptions.

  • Types called across AppDomain boundaries must inherit MarshalByRefObject.
  • Types called across AppDomain boundaries must be called via an interface.
  • The property LoaderOptimization must be set to LoaderOptimization.MultiDomainHost.
  • Construct a proxy manager that loads assemblies into AppDomains, performs queries, and unloads AppDomains.

This library can be installed using NuGet found here.

The source code for the site is licensed under the MIT license, which you can find in the MIT-LICENSE.txt file.

All graphical assets are licensed under the Creative Commons Attribution 3.0 Unported License.

About

Gets assembly information at run-time & dynamically load / unload assemblies for validation.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages