Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assemblies 如何在内存中加载依赖程序集以使用Assembly.load()方法_Assemblies - Fatal编程技术网

Assemblies 如何在内存中加载依赖程序集以使用Assembly.load()方法

Assemblies 如何在内存中加载依赖程序集以使用Assembly.load()方法,assemblies,Assemblies,我有三个dll ProjBL.dll、ProjDL.dll和ProjMC.dll ProjBL.dll是业务对象dll ProjDL.dll是数据访问层方法dll ProjMC.dll是包含属性的主类dll ProjDL.dll依赖于ProjMC.dll,ProjBL.dll依赖于ProjDL.dll 我已使用Assembly.load()方法从具有指定文件夹的D:驱动器上的文件夹中加载了内存中的ProjBL.dll 当前它给出错误“未找到依赖程序集之一” 使用的方法如下所示 Direc

我有三个dll ProjBL.dll、ProjDL.dll和ProjMC.dll

ProjBL.dll是业务对象dll

ProjDL.dll是数据访问层方法dll

ProjMC.dll是包含属性的主类dll

ProjDL.dll依赖于ProjMC.dll,ProjBL.dll依赖于ProjDL.dll

我已使用Assembly.load()方法从具有指定文件夹的D:驱动器上的文件夹中加载了内存中的ProjBL.dll

当前它给出错误“未找到依赖程序集之一”

使用的方法如下所示

    DirectoryInfo dllDirectory = new DirectoryInfo(folderPath); 
    FileInfo[] dllFiles = dllDirectory.GetFiles("*.dll"); 
    int dllCount = dllFiles.Length; 
    FileStream fs = null;         
    if (dllCount > 0) 
    { 
        long streamLength = 0; 
        for (int fileCount = 0; fileCount < dllCount; fileCount++) 
        { 
            fs = new FileStream(dllFiles[fileCount].FullName, FileMode.Open); 
            streamLength += fs.Length; 
            fs.Close(); 
        } 
        byte[] memory = new byte[streamLength]; 
        byte[] memory1 = null; 
        byte[] memory2 = null; 
        byte[] memory3 = null; 

        fs = new FileStream(dllFiles[0].FullName, FileMode.Open); 
        BinaryReader br = new BinaryReader(fs); 
        memory1 = br.ReadBytes(Convert.ToInt32(fs.Length));  // Loads ProjMC.dll 

        fs = new FileStream(dllFiles[1].FullName, FileMode.Open); 
        br = new BinaryReader(fs); 
        memory2 = br.ReadBytes(Convert.ToInt32(fs.Length));  // Loads ProjDA.dll 


        fs = new FileStream(dllFiles[2].FullName, FileMode.Open); 
        br = new BinaryReader(fs); 
        memory3 = br.ReadBytes(Convert.ToInt32(fs.Length));   // Loads ProjBL.dll 

        fs.Close(); 
        br.Close(); 

        memory1.CopyTo(memory, 0); 
        memory2.CopyTo(memory, memory1.Length); 
        memory3.CopyTo(memory, (memory1.Length + memory2.Length)); 

        assemblyUsed = Assembly.Load(memory);   

    }         
    return assemblyUsed;   
DirectoryInfo dllDirectory=newdirectoryinfo(folderPath);
FileInfo[]dllFiles=dllDirectory.GetFiles(“*.dll”);
int dllCount=dllFiles.Length;
FileStream fs=null;
如果(dllCount>0)
{ 
长流长度=0;
对于(int fileCount=0;fileCount
为什么这么复杂
Assembly.LoadFrom(string)
将很好地完成这项工作。或者您试图实现一些非常奇怪的行为?

因为ProjBL.dll需要ProjDL.dll,而ProjDL.dll需要ProjMC.dll,所以在加载ProjBL.dll时,CLR需要能够找到ProjDL.dll和ProjMC.dll(以及任何其他依赖项)


如果它们与可执行文件位于同一目录或probepath中,它应该能够找到它们。您可以使用fuslogvw查看CLR在哪里查找依赖程序集。

当当前应用程序查找程序集时,它会查找多个位置(bin文件夹、gac等)。如果找不到,则开发人员需要手动告诉应用程序要查找的位置。可以通过截取AssemblyResolve事件并使用事件参数通知CLR程序集的位置来完成此操作

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
....................
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
   var assemblyPath=
          Path.GetFullPath("..\\..\\..\\example\\" + args.Name.Substring(0,  args.Name.IndexOf(",")) + ".dll");

    return Assembly.LoadFrom(assemblyPath);
}