I want to load the assembly via the following
var loadedAssembly = Assembly.Load(File.ContentsAsBytes);
the File.ContentAsBytes returns the dll as a byte[]
, via the following
System.IO.File.ReadAllBytes("dll location");
The issue is the loaded assembly (loadedAssembly
) loses its phyisical location
Is there a way to load from a byte[]
and getting a similar result to Assembly.LoadFile
as I need the the result to work with the AppDomain.CurrentDomain.AssemblyResolve
Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.
To load an assembly from a byte array with the trust level of the application domain, use the Load(Byte[], Byte[], SecurityContextSource) method overload.
LoadFrom(String) Loads an assembly given its file name or path.
Instantiating ClassIf an assembly is loaded into the same AppDomain, then the class can be instantiated in the usual way. But if an assembly is loaded into a different AppDomain then it can be instantiated using reflection. Another way is an interface.
A byte array byte[]
is simply a stream of bytes in memory. It has no correlation to any file at all. That byte array could have been read from file, downloaded from a web server, or created spontaneously by a random number generator. There is no extra data that "goes with it".
If you want to maintain the file location where the byte array was originally read from, then you must maintain that data separately in another variable. There is no way to "attach" the extra data to the byte[]
variable.
When you use Assembly.Load
to load the byte array as an assembly, it has no way to know where that byte array came from, because that extra data is not provided to the Load
function.
As a workaround, is there a way you can save your byte array to a temporary file, use Assembly.LoadFile
to give you the data you need and link the Location
back to your original byte array?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With