DLL 引用有多昂贵?

How expensive is a DLL reference?

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

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

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

仅当您执行使用引用的 .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 . . .