如何测试某些程序集是否已加载到内存中?

hau*_*ous 5 .net c# dll

我有一些代码使用 Crystal Reports 运行时库来生成和丢弃一个小虚拟报告,以确保在用户创建真实报告之前将库及时加载到内存中。(这是一个“感知性能”问题。)当用户生成报告时,性能得到显着改善,因此显然一切正常。

现在我需要编写一个单元测试来证明 Crystal 库确实已按预期加载到内存中 - 但是我尝试测试其中的内容并Assembly.GetExecutingAssembly().GetModules()没有帮助。(GetCallingAssembly().GetModules()也好不到哪里去。)

如何从单元测试中检查这些程序集是否已加载?

TIA

Ram*_*kar 4

以下代码示例使用 GetAssemblies 方法获取已加载到应用程序域中的所有程序集的列表。然后,程序集将显示到控制台。

public static void Main() 
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        //Provide the current application domain evidence for the assembly.
        Evidence asEvidence = currentDomain.Evidence;
        //Load the assembly from the application directory using a simple name. 

        //Create an assembly called CustomLibrary to run this sample.
        currentDomain.Load("CustomLibrary",asEvidence);

        //Make an array for the list of assemblies.
        Assembly[] assems = currentDomain.GetAssemblies();

        //List the assemblies in the current application domain.
        Console.WriteLine("List of assemblies loaded in current appdomain:");
            foreach (Assembly assem in assems)
                Console.WriteLine(assem.ToString());
    }
Run Code Online (Sandbox Code Playgroud)

PS:要运行此代码示例,您需要创建一个名为 CustomLibrary.dll 的程序集,或更改传递给 GetAssemblies 方法的程序集名称。

请参阅MSDN上的此处