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

5
我有一些代码,使用Crystal Reports运行时库生成并丢弃一个小的虚拟报告,以确保在用户创建真正的报告之前及时将库加载到内存中。这是一个“感知性能”问题。当用户生成报告时,性能明显提高,因此显然所有工作都正常。
现在我需要编写一个单元测试来证明Crystal库确实在期望的时间内已经加载到内存中-但是,我尝试使用Assembly.GetExecutingAssembly().GetModules()测试所存在的内容没有帮助。(GetCallingAssembly().GetModules()也不好用。)
从我的单元测试中如何检查这些程序集是否已被加载?
TIA
1个回答

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());
    }

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

使用 Process.GetModules() 方法和这种方式的结果是否有所不同? - haughtonomous
这个不再起作用了。 System.NotSupportedException:“此方法隐式使用已被.NET Framework弃用的CAS策略。为了出于兼容性原因启用CAS策略,请使用NetFx40_LegacySecurityPolicy配置开关。有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkID=155570。” - TomatenSalat
请使用以下代码: var assemblies = AppDomain.CurrentDomain.GetAssemblies(); - TomatenSalat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接