如何在紧凑框架中从字节数组加载程序集 [英] How to load an assembly from byte array in compact framework

查看:81
本文介绍了如何在紧凑框架中从字节数组加载程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些旧客户,他们使用智能扫描仪和旧的Windows mobile。因此,我一直坚持为这些智能设备开发紧凑的框架。我正在编写一个类库,该类库将为扫描仪硬件的接口提供插件类型机制。我希望能够将来自扫描仪制造商的第三方程序集作为嵌入式资源嵌入到插件DLL中。我这样做是为了避免在我的插件系统尝试查找插件接口的实现时必须反映所有这些第三方DLL。海峡前进。问题是,有了嵌入式资源,我可以获取程序集的字节,但是 System.Reflection.Assembly.LoadAssembly(byte [])在紧凑型中不可用框架。仅 LoadAssembly(AssemblyName) LoadAssembly(String)。如何在运行时从嵌入式资源加载这些程序集?



这是我现在拥有的:

 受保护的void LoadEmbeddedAssemblies()
{
Assembly asm = Assembly.GetCallingAssembly();
foreach(asm.GetManifestResourceNames()中的字符串resName)
{
if(resName.EndsWith(。dll))
{
try
{
//这是一个嵌入式程序集
,使用(Stream s = asm.GetManifestResourceStream(resName))
{
如果(s.Length> Int32.MaxValue)抛出新的错误IOException(程序集很大);
byte [] bytes =新字节[s.Length];
s.Read(bytes,0,Convert.ToInt32(s.Length));

//Assembly.Load(bytes)<-精简框架很烂
}
}
catch(Exception e)
{
Log(new LogMessageRaisedEventArgs( AScannerBase, LoadEmbeddedAssemblies,加载嵌入式程序集时遇到异常,e.Message));
}
}
}
}


解决方案

Compact Framework不支持以这种方式加载程序集。由于运行该应用程序的平台不太可能在给定设备上发生变化,因此只需确定首次运行的设备类型,然后将适当的程序集提取到app文件夹中,加载程序便会为您找到它们。 / p>

I have legacy clients who use smart scanners with old windows mobile on them. As a result, I'm stuck developing in the compact framework for these smart devices. I am in the process of writing a class library which will provide a plugin type mechanism for an interface to the scanner hardware. I would like to be able to embed the third party assemblies from the scanner manufacturer as an embedded resource in the plugins DLL. I want to do this to avoid having to reflect over all of those third party DLL's when my plugin system is trying to find implementations of the interface for the plugins. Pretty strait forward. The issue is that with the embedded resource, I can get the bytes for the assembly, but System.Reflection.Assembly.LoadAssembly(byte[]) is not available in the compact framework. Only LoadAssembly(AssemblyName) and LoadAssembly(String). How can I load these assemblies at runtime from an embedded resource?

This is what I have now:

    protected void LoadEmbeddedAssemblies()
    {
        Assembly asm = Assembly.GetCallingAssembly();
        foreach (string resName in asm.GetManifestResourceNames())
        {
            if (resName.EndsWith(".dll"))
            {
                try
                {
                    //this is an embedded assembly
                    using (Stream s = asm.GetManifestResourceStream(resName))
                    {
                        if (s.Length > Int32.MaxValue) throw new IOException("The assembly is to large");
                        byte[] bytes = new byte[s.Length];                            
                        s.Read(bytes, 0, Convert.ToInt32(s.Length));

                        //Assembly.Load(bytes) <- Compact Framework sucks
                    }
                }
                catch (Exception e)
                {
                    Log(new LogMessageRaisedEventArgs("AScannerBase", "LoadEmbeddedAssemblies", "Exception encountered while loading embedded assembly", e.Message));
                }
            }
        }
    }

解决方案

The Compact Framework doesn't support loading an assembly that way. Since the platforms the app is running on is unlikely to ever change on a given device, just determine the device type of the first run and extract the appropriate assemblies to the app folder, from that point on the loader will find them for you.

这篇关于如何在紧凑框架中从字节数组加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆