本文介绍了动态加载从android_assets文件夹中的dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是有可能装入的Assembly.Load 的DLL时它在资源文件夹?



我有特定于平台的DLL的窗户和机器人。我试着使用android特定DLL作为资产(也生成操作),但是当我尝试将其加载到我的应用程序它没有发现(我使用了路径:文件:/// android_asset / MYDLL.DLL ),这只是发生,如果我构建应用程序的发布,在调试我得到了必要的路径来加载库,而不是从资产的文件夹。



我能做些什么?是否有其他地方,在那里我可以把我的DLL是从我的应用程序加载?


解决方案

下面是写出来的一个例子装配到Plugins文件夹中,然后在和装载在运行时阅读。



请确保您有你想有去文件夹设置为资产的组件构建AndroidAsset的动作。见下面的截图



请注意:您可能需要扩展名更改为.MP3。请参见 href=\"http://stackoverflow.com/questions/5566821/cant-get-file-descriptor-for-asset\">。我没有这个问题,虽然。





一旦你这样做,你应该能够通过使用资产管理器来获得的资产。您可以装载它们或与他们做什么。这里是读取到内存和书写出了名的样本。



常量字符串pluginPath =插件;

VAR pluginAssets = Assets.List(pluginPath);
的foreach(在pluginAssets VAR pluginAsset)
{
var文件= Assets.Open(pluginPath + Java.IO.File.Separator + pluginAsset);

使用(VAR memStream =新的MemoryStream())
{
file.CopyTo(memStream);

//做一些有趣的。
无功组件= System.Reflection.Assembly.Load(memStream.ToArray());
Console.WriteLine(的String.Format(加载:{0},assembly.FullName));
}

}

在释放模式,请留心该单为Android是要执行你的库的静态分析来进行尺寸优化。如果您在此之后加载组件,那么您可能没有应已包含的功能。下面的截图显示了一个发布版本的标准链接配置。有一些标志和配置,您可以添加到您的代码,以帮助阻止链接(),但我不知道会有太多,你可以动态加载的库做




is it possible to load an dll with Assembly.Load when it's in the asset folder?

I have platform specific dll's for windows and android. I tried to use the android specific dll as asset (also in the build action), but when I try to load it into my app it's not found (I used the path: file:///android_asset/mydll.dll), and this only happens, if i build the app as release, in debug i get the necessary paths to load the library, but not from the assets folder.

What can I do? Is there another place, where I can put my dll to be loaded from my app?

解决方案

Here is an example of writing out assemblies to a Plugins folder and then reading them in and loading at runtime.

Make sure that you have your assemblies that you want to have go to the Assets folder set to a Build Action of AndroidAsset. See screenshot below.

Please Note: You might need to change the extension to .mp3. See here. I didn't have this issue though.

Once you do that, you should be able to get the assets by using the Asset Manager. You can load them up or do whatever with them. Here is a sample of reading them into memory and the writing out the name.

const String pluginPath = "Plugins";

var pluginAssets = Assets.List(pluginPath);
foreach (var pluginAsset in pluginAssets)
{
    var file = Assets.Open(pluginPath + Java.IO.File.Separator + pluginAsset);

    using (var memStream = new MemoryStream())
    {
        file.CopyTo(memStream);

        //do something fun.
        var assembly = System.Reflection.Assembly.Load(memStream.ToArray());
        Console.WriteLine(String.Format("Loaded: {0}", assembly.FullName));
    }

}

In release mode, please be mindful that the Mono for Android is going to perform a static analysis of your libraries to perform a size optimization. If you are loading assemblies after this, then you may not have features that should have been included. The screenshot below shows the standard linking configuration for a release build. There are some flags and configurations that you can add to your code to help prevent linking (Xamarin Docs on Linking), but I am not sure that there will be too much that you can do for dynamically loaded libraries.

这篇关于动态加载从android_assets文件夹中的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 03:01