2016-09-02 77 views
1

我使用反射加載兩個程序集,然後使用它們中的幾個類型。加載程序集後,加載一個類型,然後用Activator實例化一個類型,當試圖調用「LoadFromXml」方法時,程序集上出現FileNotFound異常。這是以前工作,我不明白什麼改變。另外,我可以從創建的實例中獲取一個屬性,而不會出現問題。它只在調用方法「LoadFromXml」時拋出異常。我剛剛成功加載程序集後找不到文件

private static object CheckForVersion(int version, string constructFilePath, string utilityFilePath) 
    { 
     if (!System.IO.File.Exists(constructFilePath) || !System.IO.File.Exists(utilityFilePath) || version < 7) return null; 

     var utilAssembly = System.Reflection.Assembly.LoadFile(utilityFilePath); 
     var constructAssembly = System.Reflection.Assembly.LoadFile(constructFilePath); 

     var InfoManager = utilAssembly.GetType(String.Format("{0}.InfoManager", utilAssembly.FullName.Split(',')[0])); 
     var ExecutionServerPropertiesConstructType = constructAssembly.GetType(String.Format("{0}.ExecutionServerPropertiesConstruct", constructAssembly.FullName.Split(',')[0])); 

     string dbFolder = (string)(InfoManager.GetProperty("ServerDatabaseFolder").GetValue(null, null)); 

     if (Directory.Exists(dbFolder) == true) 
     { 
      string FilePath = Path.Combine(dbFolder, @"ExecutionServer.config.xml");     //DONT LOCALIZE 

      dynamic theServerProperties = Activator.CreateInstance(ExecutionServerPropertiesConstructType); 

      theServerProperties.LoadFromXml(FilePath); 

      var retVal = new 
      { 
       InstalledProductVersion = version, 
       ServerGuid = (string)InfoManager.GetField("SERVER_GUID").GetValue(null), 
       WorkflowRootnodeGuid = (string)InfoManager.GetField("WORKFLOW_ROOTNODE_GUID").GetValue(null), 
       TaskRootnodeGuid = (string)InfoManager.GetField("TASK_ROOTNODE_GUID").GetValue(null), 
       TriggerRootnodeGuid = (string)InfoManager.GetField("TRIGGER_ROOTNODE_GUID").GetValue(null), 
       ProcessRootnodeGuid = (string)InfoManager.GetField("PROCESS_ROOTNODE_GUID").GetValue(null), 
       ConnectionString = theServerProperties.ConnectionString 
      }; 

      return Newtonsoft.Json.JsonConvert.SerializeObject(retVal); 
     } 

     return null; 
    } 

回答

1

經過一番調查後,似乎該文件已被另一個進程/程序集加載。從Reflection.LoadFile()更改爲Reflection.LoadFrom()可解決此問題。

相關問題