【问题标题】:Get Assembly Names in Current Application Domain获取当前应用程序域中的程序集名称
【发布时间】:2010-11-24 05:14:38
【问题描述】:

我想在当前应用程序域中获取程序集友好名称,因此我写了这样的内容:

static void Main(string[] args)
    {
        foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
        {
          Console.WriteLine(item.FullName);

        }
    }

但问题是这是我得到的输出,而不是我希望看到的输出:

mscorlib,版本=2.0.0.0, 文化=中性, PublicKeyToken=b77a5c561934e0 应用程序域,版本=1.0.0.0, Culture=neutral, PublicKeyToken=null

其实我期待这些名字:

alt text http://www.pixelshack.us/images/xjfkrjgwqiag9s6o76x6.png

如果我弄错了什么,谁能告诉我。

提前致谢。

或者我期待的名字不是程序集?

【问题讨论】:

    标签: c# .net applicationdomain


    【解决方案1】:

    使用 Assemly.GetName().Name 或使用反射来查找 AssemblyTitleAttribute 并使用该值。

    【讨论】:

      【解决方案2】:

      在运行时获取所有引用程序集的列表是不可能的。即使您在 Visual Studio 项目中引用它,如果您不使用它们,C# 编译器也会忽略它们,因此它们根本不会进入您的输出文件 (exe/dll)。

      对于其余的程序集,它们在实际使用之前不会被加载。

      AppDomain.CurrentDomain.GetAssemblies() 为您提供所有已加载程序集的数组,此列表可能与您在 Visual Studio 项目中看到的非常不同。

      【讨论】:

      • 感谢您的出色回答。真诚的。
      【解决方案3】:

      当你使用反射时,你不会总是得到漂亮的命名空间名称,你会得到程序集的真实名称。

      您也不会获得所有引用的库,只会获得当前加载的库。如果在代码上方添加“XmlDocument foo = new XmlDocument()”,System.XML 就会出现。

          static void Main(string[] args)
          {
              XmlDocument foo = new XmlDocument();
      
              foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
              {
                  Console.WriteLine(item.FullName);
      
              }
          }
      

      输出:

      mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
      ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
      System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
      System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
      

      【讨论】:

        【解决方案4】:
            foreach(var assem in AppDomain.CurrentDomain.GetAssemblies())
            {
                Console.WriteLine(assem.GetName().Name);
        
            }
        

        Assembly.GetName() 返回具有 Name 属性的 AssemblyName 对象。这就是你要找的。​​p>

        【讨论】:

        • 仍然返回“MsCorLib”
        猜你喜欢
        • 2013-06-07
        • 1970-01-01
        • 2013-06-25
        • 2011-11-19
        • 1970-01-01
        • 2013-01-01
        • 2022-12-30
        • 1970-01-01
        • 2013-04-09
        相关资源
        最近更新 更多