2

本地或全局(GAC / .NET)DLL 引用,它们有多贵?

当然,我们永远不应该引用我们不使用的东西,但出于好奇,我会问:引用整个 .NET 框架会是一个很大的性能问题吗?

一个类似的更实际的问题是:是否值得在项目中组合类似的命名空间以最小化需要引用的 DLL 文件(只要在任何情况下我都必须使用这些 DLL 中的每个代码)?

4

1 回答 1

6

仅当您执行一个方法时才会加载引用,该方法使用引用的 .dll 中的类型。所以即使你引用了整个 .NET 框架,它也不会被自动加载。

class Program
{
    static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyLoad += CurrentDomain_AssemblyLoad;
        Console.WriteLine("=====Working with URI...=====");
        WorkWithUri();

        Console.WriteLine("=====Working with XML...=====");
        WorkWithXml();
    }

    private static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
    {
        Console.WriteLine(args.LoadedAssembly.FullName + " has been loaded.");
    }

    private static void WorkWithUri()
    {
        var uri = new Uri("c:\\");
    }

    private static void WorkWithXml()
    {
        XDocument xml = new XDocument();
    }
}

和输出:

=====Working with URI...=====
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
=====Working with XML...=====
System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 has been loaded.
Press any key to continue . . .
于 2016-02-16T16:00:58.933 回答