Wednesday, 29 April 2009

Assembly.Load C++ CLI assembly gives Unverifiable code failed policy check

This is one of those problems that I couldn’t find the answer with google.

Trying to load a C++ mixed mode assembly (like a CLR class library) from a byte array instead of a file throws a FileLoadException.

            // ClassLib.dll is a mixed-mode assembly (from a C++ CLR Class Library project

 

            Assembly.Load("ClassLib"); // Works

 

            using (var stream = new FileStream("ClassLib.dll", FileMode.Open, FileAccess.Read))

            {

                var bytes = new byte[stream.Length];

                stream.Read(bytes, 0, bytes.Length);

 

                Assembly.Load(bytes); // System.IO.FileLoadException "Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)"

            }