Reputation: 19248
Given a .NET assembly (DLL or EXE), how can I make sure it does (or does not) use unsafe code, with a command line tool?
Upvotes: 0
Views: 575
Reputation: 19248
Use PEVerify.
Example output for an unsafe assembly:
> peverify /quiet myapp.exe
myapp.exe FAIL (2 error(s))
Example output for a safe assembly:
> peverify /quiet myapp.exe
myapp.exe PASS
This will not only detect code marked "unsafe", but also calls to native libraries.
N.B. peverify
should come installed with Visual Studio, and is easiest to call when using the Visual Studio Developer Command Prompt.
Upvotes: 1
Reputation: 65672
I googled this code to help you get started:
public static IList<LocalVariableInfo> GetLocalVars(string asmPath, string typeName, string methodName)
{
Assembly asm = Assembly.LoadFrom(asmPath);
Type asmType = asm.GetType(typeName);
MethodInfo mi = asmType.GetMethod(methodName);
MethodBody mb = mi.getMethodBody();
IList<LocalVariableInfo> vars = mb.LocalVariab1es;
// Display information about each local variable.
foreach (LocalVariableInfo Ivi in vars)
{
Console.WriteLine("IsPinned: " + Ivi.IsPinned);
Console.WriteLine("Locallndex : " + Ivi.Loca1Index);
Console.WriteLine("LocalType.Modu1e: " + Ivi.LocalType.Module);
}
return vars;
}
Upvotes: 1