2012-12-25 69 views
0

我創建了一個程序使用下面的代碼加載動態組件:動態加載組件

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); 
     } 
    } 
} 

,我試圖加載的項目是這些文件

namespace QRReader 
{ 
    public sealed class QRReader : IBarcodeReader 
    { 
     public bool IsType() 
     { 
     return true; 
     } 

     public string GetValue() 
     { 
     return "HEJ"; 
     } 
    } 

    public interface IBarcodeReader 
    { 
     bool IsType(); 
     string GetValue(); 
    } 
} 

,但我得到這個錯誤

FileLoadException was unhandled by user code 
The assembly name or code base was illegal. (Exception HRESULT: 0x80131047) 

name -variable設置爲 C:\Users\Lasse\AppData\Local\Packages\93e3b2c9-7ef8-4537-be39-d0f3e93ca100_e85ydygyad1dy\LocalState\BarcodeReaders\QRReader.winmd

+0

如果你嘗試加載使用反射大會? –

+1

WinRT不允許從任意路徑加載程序集。所有可執行文件必須打包在一起,並且必須存儲在AppBase文件夾中。否則,這是一個安全且可驗證的應用程序的基本功能。因此,動態加載程序集通常很少有意義。 –

+0

@HansPassant我希望,我可以加載它,因爲我找到的線程說,自從Assembly.Load方法丟失後我不可能 – The87Boy

回答

0

我在網上看到的一切都表明,微軟已經將它作爲運行時環境(WinRT和UWP)的故意安全功能,它不可能在運行時加載程序集。這是一個在UWP中停止限制功能的節目。它或多或少地使平臺變得沒有用處,因爲如果對於給定的客戶進行定製,應用程序供應商將不得不拆分應用程序並將它的版本部署到每個客戶的商店中。

請採取了投票,允許在運行時動態加載組件此功能請求的時間: https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/18145291-dynamically-load-assembly