开发者

Log versions of all used DLLs

I want to log the versions of all DLLs my .NET-application uses. It doesn't matter if the log-output is generated on startup or on first开发者_JS百科 use of each DLL.

The first solution which came to my mind was to iterate over all DLL files which reside in the same directory as my assembly. But is this the best option I have? Is there any better way to do this? It's important that the solution should also work on .NET-Compact-Framework.


Edit: I tested and verified that this works on the .NET Compact Framework.

You could do this using Mono.Cecil, a powerful tool that allows you to open and inspect .NET assemblies at the IL level without even loading them into an AppDomain.

string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

// If using Mono.Cecil 0.6.9.0:
AssemblyDefinition myAssembly = AssemblyFactory.GetAssembly(path);

// If using Mono.Cecil 0.9.1.0:
AssemblyDefinition myAssembly = AssemblyDefinition.ReadAssembly(path);

// from there, you can inspect the assembly's references
foreach (ModuleDefinition module in myAssembly.Modules)
{
    foreach (AssemblyNameReference assemblyReference in module.AssemblyReferences)
    {
        // do something with the reference e.g get name, version, etc
        string fullName = assemblyReference.FullName;
        Version version = assemblyReference.Version;
    }
}


I am not sure if it is available on CF, but in normal .NET, I would simply use AppDomain.GetAssemblies().

You can use the AppDomain.AssemblyLoad event to notify you when an assembly is loaded.


You might be able to use the Dependency Walker, and just use the command-line options to execute it as a subprocess. According to the FAQ, it works against Windows CE modules, though it doesn't run on CE devices, so that may not suit your needs.


Use the AppDomain.AssemblyLoad event.

e.g.

AppDomain.CurrentDomain.AssemblyLoad+=(s, e) => 
  Console.WriteLine("loaded: "+e.LoadedAssembly.FullName);


public void LogAssemblies()
{
  foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
   { 
     AssemblyName assemblyName = asm.GetName();
     string simpleName = assemblyName.Name;
     string version = assemblyName.Version.ToString();
     // do something with these
  }
}

EDIT: Woops, looks like AppDomain.GetAssemblies() is not in compact framework.


I guess an interesting algorithm would be to serialize the assemblies you're using, then, upon startup, you deserialize your configuration and compare with what version you have of each DLL. Then, upon version update, you serialize a new version of your configuration and log your assembly name and version to your logfile or so.

Keep in mind that the most important thing to consider is get the thing does what you need it to do. Nothing else matters. Once it does the job you require, there's perhaps room for improvement. But first, get it working! You'll simply see what needs to be improved afterwards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜