Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CultureName of System assembly in .NET

Tags:

c#

.net

mono

While writing some code handling assemblies in C# I noticed inconsistencies in field values of Assembly object (example of System assembly):

> typeof(string).Assembly.FullName
"mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

but when accessing CultureName field of AssemblyName object directly, its value is an empty string:

> typeof(string).Assembly.GetName().CultureName
""

When I run the same test on Linux (mono 3.99) I got another result:

> typeof(string).Assembly.GetName().CultureName
"neutral"

Why does .NET behaves that way? I couldn't find any information on msdn regarding default value of CultureName field in AssemblyName class or meaning of an empty string. Does an empty string always refers to "neutral" culture name?

like image 957
houen Avatar asked Oct 31 '22 05:10

houen


1 Answers

The CultureName property was introduced in .Net 4.5 and the value of that property for an Assembly with the neutral culture must be equal to an empty string like the name of the invariant culture because the CultureName uses theCultureInfo property (source):

public String CultureName
{
    get
    {
        return (_CultureInfo == null) ? null : _CultureInfo.Name;
    }
}

But in Mono the CultureName property has a different implementation (source). Why different? I think developers of Mono know the answer.

public string CultureName { 
    get { 
        if (cultureinfo == null) 
            return null; 
        if (cultureinfo.LCID == CultureInfo.InvariantCulture.LCID) 
            return "neutral"; 
        return cultureinfo.Name; 
    } 
} 

So if you want to check that an AssemblyName has the neutral culture, use the code below:

if (object.Equals(assemblyName.CultureInfo, CultureInfo.InvariantCulture))
{ /* neutral culture */ }
like image 144
Yoh Deadfall Avatar answered Nov 15 '22 03:11

Yoh Deadfall