C# 在我刚刚成功加载程序集后未找到文件

C# 在我刚刚成功加载程序集后未找到文件,c#,reflection,activator,C#,Reflection,Activator,我使用反射加载两个程序集,然后使用其中的一些类型。加载程序集、加载类型并使用Activator实例化类型后,在尝试调用“LoadFromXml”方法时,程序集上会出现FileNotFound异常。这是以前的工作,我不知道什么改变了。此外,我可以从创建的实例中毫无问题地获取属性。它仅在调用方法“LoadFromXml”时引发异常 private static object CheckForVersion(int-version,string-constructFilePath,string-uti

我使用反射加载两个程序集,然后使用其中的一些类型。加载程序集、加载类型并使用Activator实例化类型后,在尝试调用“LoadFromXml”方法时,程序集上会出现FileNotFound异常。这是以前的工作,我不知道什么改变了。此外,我可以从创建的实例中毫无问题地获取属性。它仅在调用方法“LoadFromXml”时引发异常

private static object CheckForVersion(int-version,string-constructFilePath,string-utilityFilePath)
{
如果(!System.IO.File.Exists(constructFilePath)| |!System.IO.File.Exists(utilityFilePath)| | version<7)返回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”);//不本地化
动态theServerProperties=Activator.CreateInstance(ExecutionServerPropertiesConstructType);
ServerProperties.LoadFromXml(文件路径);
var retVal=new
{
InstalledProductVersion=版本,
ServerGuid=(字符串)InfoManager.GetField(“服务器GUID”).GetValue(null),
WorkflowRootnodeGuid=(字符串)InfoManager.GetField(“工作流\u根节点\u GUID”).GetValue(null),
TaskRootnodeGuid=(字符串)InfoManager.GetField(“TASK\u ROOTNODE\u GUID”).GetValue(null),
TriggerRootnodeGuid=(字符串)InfoManager.GetField(“TRIGGER\u ROOTNODE\u GUID”).GetValue(null),
ProcessRootnodeGuid=(字符串)InfoManager.GetField(“PROCESS\u ROOTNODE\u GUID”).GetValue(null),
ConnectionString=服务器属性。ConnectionString
};
返回Newtonsoft.Json.JsonConvert.SerializeObject(retVal);
}
返回null;
}

经过进一步调查,该文件似乎已经由另一个进程/程序集加载。将Reflection.LoadFile()更改为Reflection.LoadFrom()可以解决此问题

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