Search code examples
c#.netreflectionassemblies

Can I rely on specific capitalization of .NET assembly names?


In my code, I want to check if a specific assembly is loaded. I have this code:

var assembly = AppDomain.CurrentDomain
    .GetAssemblies()
    .Where(a => a.FullName.StartsWith("Microsoft.WindowsAzure.ServiceRuntime"))
    .SingleOrDefault();

Now, this code relies on specific capitalization of the assembly – the comparison is case-sensitive.

Do I need the comparison to be case-insensitive or can I expect the specific capitalization at all times?


Solution

  • According to this, the runtime treats assembly names as case-insenstive. That is, you won't have two assemblies loaded at the same time with names that only differ in their capitalization.

    So, if you ONLY want to check for a specific assembly name you should do a case-insensitive comparison using this overload of StartsWith with StringComparison .InvariantCultureIgnoreCase to avoid the (very rare) case where the capitalization of an assembly name has changed.

    a.FullName.StartsWith("Microsoft.WindowsAzure.ServiceRuntime",
         StringComparison.InvariantCultureIgnoreCase)