Aidan
Aidan

Reputation: 4891

Saving an Assembly as a byte array suitable for Assembly.Load

I notice that Assembly.LoadFrom has the following overload

public static Assembly Load(
    byte[] rawAssembly
)

How do I save an assembly as a byte array in order to create it like this?

Context : I want to write a test harness that will ensure backward compatability of a service. I want to load canned versions of the client into my harness and call the service from many different versions. I think saving the old versions as byte[] would allow me to freeze them.

Upvotes: 3

Views: 2160

Answers (2)

Gregory A Beamer
Gregory A Beamer

Reputation: 17010

Like so:

byte[] assemblyBytes = File.ReadAllByes(assemblyPath);

NOTE that you will have to load dependent assemblies first.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499740

If you have old versions as files (just as they were normally built) that's all you need. You can read those into a byte array (e.g. with File.ReadAllBytes) if you need to.

It sounds like you just need to keep the old binaries in source control.

Upvotes: 5

Related Questions