如果您不引用所有的类依赖项,可以使用类库吗? [英] Can you use a class library if you don't reference all of it's dependencies?

查看:67
本文介绍了如果您不引用所有的类依赖项,可以使用类库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我澄清一下:

我建立了一个可在多个项目中使用的类库.作为此DLL的一部分,我想通过扩展CookieAuthenticationProvider为Owin Cookies添加一些不同的自定义提供程序,因此我需要包括对Microsoft.Owin.Security.Cookies的引用.这是安全的,因为将使用我的库的较新项目也使用Microsoft.Owin.Security.Cookies.

I have built a class library to be used in several projects. As part of this DLL I want to add a few different custom providers for Owin Cookies by extending CookieAuthenticationProvider so I need to include a reference to Microsoft.Owin.Security.Cookies. This is safe because the newer projects that will use my library also use Microsoft.Owin.Security.Cookies.

但是,有些项目较旧,并且不使用Owin等...如果我包括该库的其他用途,它们会炸掉吗?还是只会在我尝试使用提供程序时炸毁它们(因为他们不能使用它,所以我不会这样做).

However some of the projects are older and dont use Owin etc... Will they blow up if I include the library for other use? Or will they only blow up if I try to use the provider (which I wouldn't since they cant use it).

我想在我的库中放入一些常用的东西,而不必将每个依赖的DLL引用到每个使用它们的项目中.我可以肯定我在做什么,但是我希望有人能告诉我,然后再花很多时间来解决这个问题.另外,如果有更好的方法,我会全力以赴.

I want to put some commonly used things in my library without having to reference every one of it's dependent DLL's to every project that uses them. I'm pretty sure what I'm doing is ok but I was hoping somone could tell me before I waste many hours going forward with this. Also if there is a better way I'm all ears.

推荐答案

规则:

  • 给定程序集可见的所有类型都必须在该程序集引用的程序集中声明.

    ,只要您的类库实际上不在其公共范围内公开使用在 Microsoft.Owin.Security.Cookies 程序集中找到的类型的API,然后其他程序集可以安全地使用您的DLL进行编译,而无需引用该程序集.

  • 在运行时不需要引用的程序集,除非实际需要该程序集中的代码时,即其他一些代码尝试调用该代码.

    general 中,这意味着只要其他程序集引用您的程序集且不引用 Microsoft.Owin.Security.Cookies ,它们也不会调用任何代码在您的程序集中,然后尝试在 Microsoft.Owin.Security.Cookies 中调用代码的程序集中,该程序集不必在运行时出现.
  • All types which are visible to a given assembly must be declared in assemblies referenced by that assembly.

    As long as your class library does not actually expose in its public API the types found in the Microsoft.Owin.Security.Cookies assembly, then other assemblies can safely compile with your DLL without referencing that assembly.

  • A referenced assembly need not be present at runtime, except when code in that assembly is actually needed, i.e. some other code attempts to call that code.

    In general, this means that as long as other assemblies which are referencing your assembly and which don't reference Microsoft.Owin.Security.Cookies also don't call any code in your assembly that would then in turn attempt to call code in Microsoft.Owin.Security.Cookies, that assembly need not be present at runtime.

第二点的棘手部分是,在 Microsoft.Owin.Security.Cookies 中的调用代码"的构成并不总是很清楚.通常,只要您根本不访问程序集中的类型,.NET就不会尝试在该程序集中执行任何代码.但是即使在不一定需要类型的情况下(例如在初始化程序中, static 或其他方式,用于检查接口实现的代码等),也不难偶然访问类型.

The tricky part on that second point is that what constitutes "calling code in Microsoft.Owin.Security.Cookies" is not always clear. Typically, as long as you don't access the types in the assembly at all, .NET won't try to execute any code in that assembly. But it's not hard to accidently access the types even when they are not necessarily needed (e.g. in initializers, static or otherwise, code that checks for interface implementations, etc.).

如果您确实希望客户能够使用引用 Microsoft.Owin.Security.Cookies 的DLL,而不必在运行时提供该DLL,则需要非常小心,以确保您完全支持该方案.可以做到,但是犯错也不难.

If you really want your clients to be able to use your DLL, which references Microsoft.Owin.Security.Cookies, without necessarily needing that DLL to be present at runtime, you will need to be very careful to ensure you've fully supported that scenario. It is possible to do, but it's also not hard to make a mistake.


(我不得不承认,令我惊讶的是,Stack Overflow尚未解决这个有用的问题.似乎现在已经出现了.但是我找不到重复的,因此答案是上面的内容.如果有人知道我忽略了重复的内容,欢迎对此进行适当的通知.)

这篇关于如果您不引用所有的类依赖项,可以使用类库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆