代码之家  ›  专栏  ›  技术社区  ›  Ajit Singh

使用反射从.NET 64位exe调用.NET 32位dll中的方法

  •  3
  • Ajit Singh  · 技术社区  · 14 年前

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using Host.TestLib;
    
    namespace test
    {
        class Program
        {
            static void Main(string[] args)
            {
                var lib = Assembly.LoadFrom("Simple32bitAssembly.dll");
            }
        }
    }
    

    运行此命令时,会引发以下异常:

    System.BadImageFormatException was unhandled
    Message=Could not load file or assembly 
    'file:///E:\AjitTemp\c\32bit64Bit\ReflectionTest\test\bin\Debug\Simple32bitAssembly.dll' 
    or one of its dependencies. An attempt was made to load a program with an incorrect format.
    

    google建议我需要为这个32位dll创建一个64位包装器,并在我的64位控制台应用程序中使用relection加载这个包装器?这是路吗?任何示例代码都会非常有用。

    1 回复  |  直到 14 年前
        1
  •  4
  •   erlando    14 年前

    如果您特别将这两个程序集作为32位和64位生成的目标,则不能将32位程序集加载到64位进程中(反之亦然)。

    关于你的评论:

    “有些业务限制使我无法将32位dll编译为‘任何CPU’。”

    解决此问题的唯一方法是将32位程序集部署到单独的32位代理进程中。此过程可以通过.NET技术公开功能,例如:

        WCF
        Remoting
        ASP.NET Web Service
    

    缺点是跨进程调用可能代价高昂(尽管您可以在WCF或Remoting中使用命名管道),并且会增加应用程序的复杂性。

    您也不再能够直接在使用64应用程序的组件上使用反射(但是您可以在代理中使用反射)。

    这就是混合架构应用程序的试验。

    推荐文章