程序集引用里面的“Culture=neutral”是什么意思?

C#将Culture分为的Neutral Culture 和 Specific Culture,
微软推荐使用 SpecificCulture(其实还有一类Invariant Culture微软不推荐使用,仅仅是周期较短的演示系统开发时推荐使用)
当我们看到 en-US,es-ES等表示culture的名称时,可以这样进行识别
横线前面的部分称为Neutral Culture,它是“与某种语言关联但不与国家/地区关联的区域性”的含义
横线后面的部分称为Specific Culture ,它就是和某个地区国家有关的了
通常采用这种写法,因此我们能很容易分辨这两种Culture,如:fr-FR
只有一种特殊情况,就是中文,它的关系是这样的
zh-CHS Chinese (Simplified) , Neutral
zh-CN Chinese - China
zh-CHT Chinese (Traditional) , Neutral
zh-TW Chinese - Taiwan
zh-HK Chinese - Hong Kong SAR
zh-MO Chinese - Macao SAR
zh-SG Chinese - Singapore
可以看到,主要就是 Neutral通常应该是两位的,但这里的Neutral:zh-CHS和zh-CHT有6位,这是比较容易混淆的,而真正的Specific Culture是:zh-CN,zh-TW ,zh-HK,zh-MO,zh-SG 。
指出这个特例也是本文的目的。
  如何使用程序集的密钥?   
  ·您是指如何将程序集签名吗?   
  A:您可以使用   sn.exe   生成一个密钥文件,然后在项目属性里指定要使用的密钥文件。   
  ·您是指如何通过一个已经签名的程序集的   PublicKeyToken   来导出密钥文件?   
  A:这个不大可能吧?   
    
  ·如何取得   PublicKeyToken?   
  可以参加示例代码:   
  using   System;   
  using   System.Reflection;   
  using   System.Threading;   
  using   System.IO;   
  using   System.Globalization;   
  using   System.Reflection.Emit;   
  using   System.Configuration.Assemblies;   
  using   System.Text;   
    
  public   class   AssemblyName_CodeBase   
  {   
        public   static   void   MakeAssembly(AssemblyName   myAssemblyName,   string   fileName)   
        {   
              //   Get   the   assembly   builder   from   the   application   domain   associated   with   the   current   thread.   
              AssemblyBuilder   myAssemblyBuilder   =   Thread.GetDomain().DefineDynamicAssembly(myAssemblyName,   AssemblyBuilderAccess.RunAndSave);   
              //   Create   a   dynamic   module   in   the   assembly.   
              ModuleBuilder   myModuleBuilder   =   myAssemblyBuilder.DefineDynamicModule("MyModule",   fileName);   
              //   Create   a   type   in   the   module.   
              TypeBuilder   myTypeBuilder   =   myModuleBuilder.DefineType("MyType");   
              //   Create   a   method   called   'Main'.   
              MethodBuilder   myMethodBuilder   =   myTypeBuilder.DefineMethod("Main",   MethodAttributes.Public   |   MethodAttributes.HideBySig   |   
                    MethodAttributes.Static,   typeof(void),   null);   
              //   Get   the   Intermediate   Language   generator   for   the   method.   
              ILGenerator   myILGenerator   =   myMethodBuilder.GetILGenerator();   
              //   Use   the   utility   method   to   generate   the   IL   instructions   that   print   a   string   to   the   console.   
              myILGenerator.EmitWriteLine("Hello   World!");   
              //   Generate   the   'ret'   IL   instruction.   
              myILGenerator.Emit(OpCodes.Ret);   
              //   End   the   creation   of   the   type.   
              myTypeBuilder.CreateType();   
              //   Set   the   method   with   name   'Main'   as   the   entry   point   in   the   assembly.   
              myAssemblyBuilder.SetEntryPoint(myMethodBuilder);   
              myAssemblyBuilder.Save(fileName);   
        }   
    
        public   static   void   Main()   
        {   
              //   Create   a   dynamic   assembly   with   name   'MyAssembly'   and   build   version   '1.0.0.2001'.   
              AssemblyName   myAssemblyName   =   new   AssemblyName();   
              //   Set   the   codebase   to   the   physical   directory   were   the   assembly   resides.   
              myAssemblyName.CodeBase   =   String.Concat("file:///",   Directory.GetCurrentDirectory());   
              //   Set   the   culture   information   of   the   assembly   to   'English-American'.   
              myAssemblyName.CultureInfo   =   new   CultureInfo("en-US");   
              //   Set   the   hash   algoritm   to   'SHA1'.   
              myAssemblyName.HashAlgorithm   =   AssemblyHashAlgorithm.SHA1;   
              myAssemblyName.VersionCompatibility   =   AssemblyVersionCompatibility.SameProcess;   
              myAssemblyName.Flags   =   AssemblyNameFlags.PublicKey;   
              //   Provide   this   assembly   with   a   strong   name.   
              myAssemblyName.KeyPair   =   new   StrongNameKeyPair(File.Open("KeyPair.snk",   FileMode.Open,   FileAccess.Read));   
              myAssemblyName.Name   =   "MyAssembly";   
              myAssemblyName.Version   =   new   Version("1.0.0.2001");   
              MakeAssembly(myAssemblyName,   "MyAssembly.exe");   
                
              //   Get   the   assemblies   loaded   in   the   current   application   domain.   
              Assembly[]   myAssemblies   =   Thread.GetDomain().GetAssemblies();   
    
              //   Get   the   dynamic   assembly   named   'MyAssembly'.     
              Assembly   myAssembly   =   null;   
              for(int   i   =   0;   i   <   myAssemblies.Length;   i++)   
                    if(String.Compare(myAssemblies[i].GetName().Name,   "MyAssembly")   ==   0)   
                          myAssembly   =   myAssemblies[i];   
    
              //   Display   the   full   assembly   information   to   the   console.   
              if(myAssembly   !=   null)   
              {   
                    Console.WriteLine("\nDisplaying   the   full   assembly   name.\n");   
                    Console.WriteLine(myAssembly.GetName().FullName);   
                    Console.WriteLine("\nDisplaying   the   public   key.\n");   
                    byte   []pk;   
                    pk   =   myAssembly.GetName().GetPublicKey();   
      for   (int   i=0;i<pk.GetLength(0);i++)   
    Console.Write   ("{0:x}",   pk[i]);   
                    Console.WriteLine();   
                    Console.WriteLine("\nDisplaying   the   public   key   token.\n");   
                    byte   []pt;   
                    pt   =   myAssembly.GetName().GetPublicKeyToken();   //   此行取得   PublicKeyToken   
      for   (int   i=0;i<pt.GetLength(0);i++)   
    Console.Write   ("{0:x}",   pt[i]);   
              }   
        }   
  } 
原文地址:https://www.cnblogs.com/zjoch/p/2671618.html