C# 如何使用类库的所有引用向类库添加引用?

C# 如何使用类库的所有引用向类库添加引用?,c#,.net,dll,dependencies,.net-assembly,C#,.net,Dll,Dependencies,.net Assembly,我试图解释我的问题 我有自己的windows服务(WS)。此WS引用Helpers.dllHelpers.dll这是我自己的类库,有几个引用。其中一个参考是System.Web.Mvc.dll 例如,如果我将Helpers.dll上的引用添加到我的WS,则Helpers.dll中的所有引用都不会添加到WS(我认为,在大多数情况下,这种行为是正确的) 我试图在Syste.Web.Mvc.dll(Helpers.csproj)上设置引用的Copy Local=True内部属性 如何使用类库的所有引

我试图解释我的问题

我有自己的windows服务(WS)。此WS引用
Helpers.dll
Helpers.dll
这是我自己的类库,有几个引用。其中一个参考是
System.Web.Mvc.dll

例如,如果我将
Helpers.dll
上的引用添加到我的WS,则
Helpers.dll
中的所有引用都不会添加到WS(我认为,在大多数情况下,这种行为是正确的)

我试图在
Syste.Web.Mvc.dll
Helpers.csproj
)上设置引用的
Copy Local=True
内部属性

如何使用类库的所有引用向类库添加引用

您不应该将所有可传递依赖项都添加为程序集引用。引用是从另一个程序集导入类型并使其在代码中可用所必需的

我认为真正的问题是将依赖程序集部署到生成文件夹的过程。试着在问题中使用一些建议:


处理可传递依赖关系(将其正确部署到每个依赖项目)的现代方法是使用PackageManager。NuGet是这方面的事实标准。此外,DNX项目系统中的许多创新旨在解决此类问题,并将NuGet软件包用作一流的构建和部署实体。

首先,我要感谢@Nipheris提供了使用NuGet的答案和建议。使用Nuget真的对我很有帮助


不管怎样,我一直在犯错误。我想尝试使用unity而不是自定义依赖项解析器(
AssemblyHelper
类是该助手的一部分)。在我看来,使用自定义依赖项解析器让我犯了一个错误。

当然,Nuget确实帮助我同意,你应该使用Nuget或其他软件包管理器来管理你的依赖项,就像这样。如果确实必须包含其他程序集,可以尝试类似ILMerge的方法
<Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <Private>True</Private>
</Reference>
public static class AssemblyHelper
{
    private static readonly object _syncLock = new object();

    private static readonly List<Assembly> _assemblies = new List<Assembly>();

    public static ICollection<Assembly> SyncronizedList
    {
        get
        {
            lock (_syncLock)
            {
                return new List<Assembly>(_assemblies);
            }
        }
    }

    static AssemblyHelper()
    {
        AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoad);
        AppDomain.CurrentDomain.DomainUnload += new EventHandler(OnDomainUnload);

        // explicitly register previously loaded assemblies since they was not registered yet
        foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            RegisterAssembly(assembly);
        }
    }

    private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
    {
        if (RegisterAssembly(args.LoadedAssembly))
        {
            ExecuteAllExecuteOnAssemblyLoadMethods(args.LoadedAssembly);
        }
    }

    private static void OnDomainUnload(object sender, EventArgs args)
    {
        foreach (Assembly assembly in SyncronizedList)
        {
            ExecuteAllExecuteOnDomainUnloadMethods(assembly);
        }
    }

    public static bool RegisterAssembly(Assembly assembly)
    {
        if (assembly == null)
        {
            throw new ArgumentNullException("assembly");
        }
        lock (_syncLock)
        {
            if (_assemblies.Contains(assembly))
            {
                return false;
            }
            else
            {
                _assemblies.Add(assembly);
                ExecuteAllExecuteOnAssemblyLoadMethods(assembly);
                return true;
            }
        }
    }

    private static void OnAssemblyLoad(object sender, AssemblyLoadEventArgs args)
    {
        if (RegisterAssembly(args.LoadedAssembly))
        {
            ExecuteAllExecuteOnAssemblyLoadMethods(args.LoadedAssembly);
        }
    }

    private static void OnDomainUnload(object sender, EventArgs args)
    {
        foreach (Assembly assembly in SyncronizedList)
        {
            ExecuteAllExecuteOnDomainUnloadMethods(assembly);
        }
    }

    public static ICollection<MethodInfo> FindAllMethods(Assembly assembly, Type attributeType)
    {
        String assemblyName = "unknown";
        String attributeTypeName = String.Empty;
        try
        {
            if (assembly == null)
            {
                throw new ArgumentNullException("assembly");
            }
            assemblyName = assembly.FullName;
            if (attributeType == null)
            {
                throw new ArgumentNullException("attributeType");
            }
            attributeTypeName = attributeType.FullName;
            List<MethodInfo> lst = new List<MethodInfo>();
            foreach (Type type in assembly.GetTypes())
            {
                foreach (MethodInfo mi in type.GetMethods(
                    BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    if (Attribute.IsDefined(mi, attributeType))
                    {
                        lst.Add(mi);
                    }
                }
            }
            return lst;
        }
        catch (Exception ex)
        {
            //exception
        }
    }

    public static int ExecuteAllMethods(Assembly assembly, Type attributeType)
    {
        int count = 0;
        foreach (MethodInfo mi in FindAllMethods(assembly, attributeType))
        {
            try
            {
                mi.Invoke(null, null);
                count++;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(string.Format("Failed to execute method {0} of {1}, reason: {2}",
                    mi.Name, mi.DeclaringType, Converter.GetExceptionMessageRecursive(ex)));
            }
        }
        return count;
    }


    public static ICollection<MethodInfo> FindAllExecuteOnAssemblyLoadMethods(Assembly assembly)
    {
        return FindAllMethods(assembly, typeof(Attributes.ExecuteOnAssemblyLoadAttribute));
    }

    public static ICollection<MethodInfo> FindAllExecuteOnDomainUnloadMethods(Assembly assembly)
    {
        return FindAllMethods(assembly, typeof(Attributes.ExecuteOnDomainUnloadAttribute));
    }

    public static int ExecuteAllExecuteOnAssemblyLoadMethods(Assembly assembly)
    {
        return ExecuteAllMethods(assembly, typeof(Attributes.ExecuteOnAssemblyLoadAttribute));
    }

    public static int ExecuteAllExecuteOnDomainUnloadMethods(Assembly assembly)
    {
        return ExecuteAllMethods(assembly, typeof(Attributes.ExecuteOnDomainUnloadAttribute));
    }

}