Initialize library on Assembly load

Yes there is – sort of. Use the excellent little utility by by Einar Egilsson, InjectModuleInitializer. Run this executable as a post build step to create a small .cctor function (the module initializer function) that calls a static void function of yours that takes no parameters. It would be nice if compiler gave us the … Read more

Finding all Namespaces in an assembly using Reflection (DotNET)

No, there’s no shortcut for this, although LINQ makes it relatively easy. For example, in C# the raw “set of namespaces” would be: var namespaces = assembly.GetTypes() .Select(t => t.Namespace) .Distinct(); To get the top-level namespace instead you should probably write a method: var topLevel = assembly.GetTypes() .Select(t => GetTopLevelNamespace(t)) .Distinct(); … static string GetTopLevelNamespace(Type … Read more

How to save DLLs in a different folder when compiling in Visual Studio?

There are 2 parts of your question: How to configure solutions to build assemblies/EXE into folders of your choice – this is configured through properties of the project in VS (project properties -> build -> output path). Also value of check “copy local” property on each reference. How to load assemblies files from non-default locations … Read more

mscorlib.dll & System.dll

I work on the CLR/BCL team and just answered your email. Here it is pasted below: Jared’s answer on Stack Overflow is right on. mscorlib.dll is tightly bound to the CLR for the reasons he mentions. Note that mscorlib.dll itself doesn’t contain any native code (as Scott suggests), but there are many places where it … Read more

How to extract an assembly from the GAC?

I used the advice from this article to get an assembly from the GAC. Get DLL Out of The GAC DLLs once deployed in GAC (normally located at c:\windows\assembly) can’t be viewed or used as a normal DLL file. They can’t be directly referenced from VS project. Developers usually keep a copy of the original … Read more

How to use Assembly Binding Redirection to ignore revision and build numbers

Thanks to leppie’s suggestion of using the AppDomain.AssemblyResolve event, I was able to solve a similar problem. Here’s what my code looks like: public void LoadStuff(string assemblyFile) { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); var assembly = Assembly.LoadFrom(assemblyFile); // Now load a bunch of types from the assembly… } Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var name … Read more

Difference between AppDomain.GetAssemblies and BuildManager.GetReferencedAssemblies

The .NET Framework defers loading assemblies into the current AppDomain until they’re needed. For example, if you call into a third-party library only from SomeMethod(), the third-party DLL normally won’t be loaded until the first time SomeMethod() runs. AppDomain.GetAssemblies() gives you all assemblies which have already been loaded into the current AppDomain. BuildManager.GetReferencedAssemblies() (This method … Read more

How do I find the fully qualified name of an assembly?

This is a shameless copy-paste from I Note It Down and is a simple way to get the FQN for the project output: Open Visual Studio Go to Tools –> External Tools –> Add Title: Get Qualified Assembly Name Command: Powershell.exe Arguments: -command “[System.Reflection.AssemblyName]::GetAssemblyName(\”$(TargetPath)\”).FullName” Check “Use Output Window”. The new tool appears under Tools –> … Read more