C# WinRT中的动态加载程序集
我使用以下代码创建了一个加载动态程序集的程序:C# WinRT中的动态加载程序集,c#,windows-runtime,C#,Windows Runtime,我使用以下代码创建了一个加载动态程序集的程序: using System; using System.Collections.Generic; using System.Reflection; using System.Threading.Tasks; namespace BarcodeReader { public class Parsing { private static string _FolderName = "BarcodeReaders"; p
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
namespace BarcodeReader
{
public class Parsing
{
private static string _FolderName = "BarcodeReaders";
private static bool _Initialized = false;
private static IEnumerable<IBarcodeReader> _Objs;
/// Parse the picture
/// <returns>The value from the picture</returns>
public static async Task<string> ParsePicture()
{
// Check if this class has not been initialized, and if it hasn't initialize it
if (!_Initialized)
{
await InitializeAsync();
}
foreach (var Obj in _Objs)
{
if (Obj.IsType())
{
return Obj.GetValue();
}
}
return null;
}
private static async Task InitializeAsync()
{
// Get the folder
var Folder = await GetFolder();
// Get the Files in the Folder
var Files = await Folder.GetFilesAsync();
// Initialize the objects and set them
_Objs = InitializeObjects(Files);
// Set it as initialized
_Initialized = true;
}
private static IEnumerable<IBarcodeReader> InitializeObjects(IEnumerable<Windows.Storage.StorageFile> Files)
{
foreach (var File in Files)
{
string Name = File.Path;
var Assembly = System.Reflection.Assembly.Load(new AssemblyName(Name));
foreach (var Typ in Assembly.ExportedTypes)
{
var TypInfo = Typ.GetTypeInfo();
foreach (var Interf in TypInfo.ImplementedInterfaces)
{
if (Interf.Name.Equals("IBarcodeReader"))
{
yield return (IBarcodeReader)Activator.CreateInstance(Typ);
}
}
}
}
}
private static async Task<bool> BarcodeFolderExist(Windows.Storage.StorageFolder Folder)
{
// Get all folders
var Folders = await Folder.GetFoldersAsync();
// For each folder, check if it is the Folder we are searching and if it is return true
foreach (var Foldr in Folders)
{
if (Foldr.Name.Equals(_FolderName))
{
return true;
}
}
// Return false as the folder was not found
return false;
}
private static async Task<Windows.Storage.StorageFolder> GetFolder()
{
// Get the local-folder
var Folder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Check if the folder does not exist, and if it does not create it
if (!await BarcodeFolderExist(Folder))
{
await Folder.CreateFolderAsync(_FolderName);
}
return await Folder.GetFolderAsync(_FolderName);
}
}
}
但是我得到了这个错误
FileLoadException was unhandled by user code
The assembly name or code base was illegal. (Exception HRESULT: 0x80131047)
name
-变量设置为
C:\Users\Lasse\AppData\Local\Packages\93e3b2c9-7ef8-4537-be39-d0f3e93ca100\u e85ydygyyad1dy\LocalState\barcodereders\QRReader.winmd
我在互联网上读到的所有东西都说,微软故意将其作为运行时环境(WinRT和UWP)的安全特性,不可能在运行时加载程序集。这是UWP中的显示停止限制功能。这或多或少会使平台变得无用,因为如果给定客户有定制,应用程序供应商将不得不拆分应用程序,并为每个客户向应用商店部署一个版本
请花时间就允许在运行时动态加载程序集向上投票此功能请求:
如果您尝试使用反射加载程序集,该怎么办?WinRT不允许从任意路径加载程序集。所有可执行文件必须打包在一起,并且必须存储在AppBase文件夹中。否则,应用程序的一个基本功能就是安全和可验证的。因此,动态加载程序集通常没有什么意义。@HansPassant我希望,我可以加载它,因为我找到的线程说,我不可能加载它,因为Assembly.load-method丢失了
FileLoadException was unhandled by user code
The assembly name or code base was illegal. (Exception HRESULT: 0x80131047)