2015-09-07 28 views
-1

簡單的主機應用程序通過特殊的接口搜索程序集,並從它們中導入代表列表,現在它的代碼爲Func<string,string>。 然後它可以執行任何Func<T,T>並且沒有問題。 當Func中的任何一個嘗試訪問不存在的文件時,都會啓動問題。C#後期綁定和文件例外

沒有try-catch塊,沒有File.Exists不幫助 - 當函數試圖訪問一個文件(無論如何,閱讀,獲取流,檢查等) - 整個應用程序只是失敗,並在mscorlibFileNotFound

這是如何解決的?應用程序非常重要,我不能在應用程序中執行文件檢查,只能在程序集中進行。

UPD:是的,該代表包含async邏輯。

UPD2:代碼部件:

try 
    { 
     if(!File.Exists(filePath)) return null; 

       using (StreamWriter writer = new StreamWriter(destinationFilePath)) 
       { 
        using (StreamReader reader = new StreamReader(filePath)) 
        { 
        //some logic there 
        } 
       } 
    } 
    catch 
    { 

    } 

異常在File.Exists拋出()。

此代碼用於導入程序集。

Commands = new Dictionary<string, Func<string, string>>(); 
foreach (string f in fileNames) 
       { 

        Assembly asm = Assembly.LoadFrom(f); 
        var types = asm.GetTypes(); 
        foreach(Type t in types) 
        { 

         if (t.GetInterface("IMountPoint") != null) 
         { 

          var obj = Activator.CreateInstance(t); 
          var cmds = ((IMountPoint)obj).Init(EntryPoint); 
          foreach (var cmd in cmds) 
          { 
           if (!Commands.ContainsKey(cmd.Key.Trim().ToUpper())) 
           { 
            Commands.Add(cmd.Key.Trim().ToUpper(), cmd.Value); 
           } 

          } 
         } 
        } 
       } 

而這個代碼運行代表:

string input = Console.ReadLine(); 
string res = Commands[command_key](input); 
+5

你能提供一些代碼嗎? – Waescher

+1

這些程序集是否加載在同一個appdomain中? – rene

+0

你想要什麼類型的代碼?解決方案很大,我認爲我寫了所有需要的東西。 –

回答

0

這是可恥的。 我正在使用後期綁定,並且忘記手動複製程序集,因此程序沒有裝載文件存在檢查程序,它使用舊程序。 對不起,夥計們。