[C#引用子文件夹中的dll

问题描述 投票:0回答:1

我不知道我做错了什么。

我的项目就是这样:

MainProject

子项目(引用我的期望的DLL)

我有我的(MainProject的)app.config:

<configuration>
  <configSections>
    <section name="DirectoryServerConfiguration" type="System.Configuration.NameValueSectionHandler"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
    <section name="GeneralConfiguration" type="System.Configuration.NameValueSectionHandler"/>
    <section name="ServerConnectionConfiguration" type="System.Configuration.NameValueSectionHandler"/>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings,Microsoft.Practices.EnterpriseLibrary.Caching"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
  </startup>

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="ReferencesDLL" />
    </assemblyBinding>
  </runtime>

  <GeneralConfiguration configSource="ConfigFiles\GeneralConfiguration.config"/>
  <cachingConfiguration configSource="ConfigFiles\cachingConfiguration.config"/>
  <DirectoryServerConfiguration configSource="ConfigFiles\YPAdress.config"/>
  <log4net configSource="ConfigFiles\log4net.config"/>
</configuration>

我的编译库就是这样:

  • 调试
    • ConfigFiles \(在app.config中定义的所有.config文件)
    • ReferencesDLL \(子项目需要的所有Dll)
    • ((所有其他编译文件:.exe,.dll等。)

如您所见,我有我的privatePath定义,但是在运行该应用程序时,它找不到SubProject所需的dll。 (.config文件没有问题)我不知道我做错了什么:(。

c# visual-studio dll dll-reference
1个回答
1
投票

您可以使用AppDomain.AssemblyResolve事件处理程序在已知位置手动查找程序集:

static void Run()
{
    AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

    // Do work...

    AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
}

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    var dllName = new AssemblyName(args.Name).Name + ".dll";
    var assemblyPath = Assembly.GetExecutingAssembly().Location;

    var referenceDirectory = new DirectoryInfo(Path.Combine(Path.GetDirectoryName(assemblyPath), "ReferencesDLL"));
    if (!referenceDirectory.Exists)
        return null; // Can't find the reference directory

    var assemblyFile = referenceDirectory.EnumerateFiles(dllName, SearchOption.TopDirectoryOnly).FirstOrDefault();
    if (assemblyFile == null)
        return null; // Can't find a matching dll

    return Assembly.LoadFrom(assemblyFile.FullName);
}
© www.soinside.com 2019 - 2024. All rights reserved.