Skip to content

Instantly share code, notes, and snippets.

@allykzam
Created July 19, 2015 17:33
Show Gist options
  • Save allykzam/c400f1cbfbc67f7242e4 to your computer and use it in GitHub Desktop.
Save allykzam/c400f1cbfbc67f7242e4 to your computer and use it in GitHub Desktop.
Quick test snippet for finding out if two libraries expose the same public API

This is a short test of comparing the public API in two .NET binaries. There isn't any special comparison being done beyond determining whether or not the public APIs match perfectly.

To determine whether or not the two APIs match, build the .cs files like so:

csc /out:Lib1.dll /target:library 10_Lib1.cs
csc /out:Lib2.dll /target:library 20_Lib2.cs

Then edit the file paths in 30_Diff.fsx to match the location of the new DLL files, and then run the script in FSI. The script should output a message indicating whether or not the libraries have the same API.

namespace Library
{
public class TestClass
{
public int Add(int a, int b)
{
return a + b;
}
}
}
namespace Library
{
public class TestClass
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
}
}
let apiMatches file1 file2 =
let f x = x |> Seq.map (fun x -> sprintf "%A" x) |> Seq.toList
let f' (x : string) : (string * string list) list =
System.Reflection.Assembly.LoadFile(x).DefinedTypes
|> Seq.filter (fun x -> x.IsPublic)
|> Seq.map (fun x -> x.FullName, (f x.DeclaredMembers))
|> Seq.toList
(f' file1) = (f' file2)
let file1 = @"C:\Users\amazingant\Documents\DiffTest\Lib1.dll"
let file2 = @"C:\Users\amazingant\Documents\DiffTest\Lib2.dll"
if apiMatches file1 file2
then printfn "Given libraries have matching APIs"
else printfn "Given libraries have different APIs"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment