2015-09-07 29 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

这是可耻的。 我正在使用后期绑定,并且忘记手动复制程序集,因此程序没有装载文件存在检查程序,它使用旧程序。 对不起,伙计们。