Assembly.LoadFrom using md5 hash not working

Question!

My Assembly.LoadFrom(path, hashValue, hashAlgorithm) is not working, failing with a "located assembly does not match assembly reference" exception.

This is in powershell 2.

I have determined the hash value by first loading the assembly using Load(bytes) which works fine. Using that result, I access the MD5 hash value from the evidence, as follows:

$a= [System.Reflection.Assembly]::Load($bytes)  
$h=$a.Evidence|? {$_ -is [System.Security.Policy.Hash]}  
$md5= $h.MD5  

I bring the hash value into another powershell session, so I have a clean AppDomain, and attempt this:

$md5=[byte[]] (values that I determined)  
[System.Reflection.Assembly]::LoadFrom("pathToMyAssembly", $md5, "MD5")  

that's what fails. Any ideas?



Answers
The LoadFrom method you are calling is only available from .NET 4 onwards. Seeing Powershell still runs on .NET 3.5, calling this wont work.

Perhaps you can just pass null to the 2nd parameter. EG:

[System.Reflection.Assembly]::LoadFrom("pathToMyAssembly", null, $md5, "MD5")  

According to MSDN, the above method is now obsolete, but for 3.5 you have no other choice :)

By : leppie


This video can help you solving your question :)
By: admin