使用 C#,如果事件处理程序无法加载程序集依赖项,如何停止执行 XLL 函数?

Using C#, how do I stop execution of an XLL function if an assembly dependency cannot be loaded by an event handler?

提问人:RobBaker 提问时间:11/17/2023 更新时间:11/17/2023 访问量:8

问:

我有一个 C# XLL(Excel 加载项),它(除其他外)使用第三方程序集(在运行时加载)从数据库中获取数据。在正常情况下,我使用 ResolveEventHandler 捕获解析程序并手动加载所需的程序集及其依赖项,如下所示:

private static Assembly AssemblyResolve(object sender, ResolveEventArgs args)
{ // This is an event handler for catching missing assemblies at runtime - designed to find and load the SIMExporterEbmlImport assembly and its dependencies
    // Assume a fixed path for where the assembly is located (can update this later to look in multiple versions if needed)
    // Should also add a switch block to only look for expected dependencies in this location,
    // and throw a helpful error if some unexpected dependency is missing.
    string dependentAssemblyName = new AssemblyName(args.Name).Name;
    string dependentAssemblyLocation = "C:\\ProgramData\\EB Bridging Tools\\SimExporter\\" + dependentAssemblyName + ".dll";


    // Try to load (will end up back here if lower dependencies are not met)
    try
    {
        return Assembly.LoadFile(dependentAssemblyLocation);
    }
    catch
    {
        MessageBox.Show(dependentAssemblyName + ".dll is missing. Please ensure it is installed to" + Common.nl
           + "C:\\ProgramData\\EB Bridging Tools\\SimExporter", "DataTool - Get EB Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return null;
    }
}

catch 块是我第一次尝试处理用户机器根本没有所需的组件(或者它可能在错误的位置)的情况。我的问题是:阻止需要触发事件处理程序的最终调用方法的最佳方法是什么,即不要杀死我的整个 XLL 运行,而只是中止调用任何需要缺失程序集的方法?

c#

评论


答: 暂无答案