Can you use a class library if you are not referencing all of its dependencies?

Let me clarify:

I created a class library that will be used in several projects. As part of this DLL, I want to add several different custom providers for Owin Cookies, expanding CookieAuthenticationProvider, so I need to include a link to Microsoft.Owin.Security.Cookies. This is safe because newer projects that will use my library also use Microsoft.Owin.Security.Cookies.

However, some of the projects are older and do not use Owin, etc. Will they explode if I turn on the library for another use? Or they will only explode if I try to use a provider (which I would not do, since they cannot use it).

I want to put some commonly used things into my library without linking each of the dependent DLLs to every project that uses them. I am pretty sure that I am doing this normally, but I was hoping that someone would be able to tell me before spending a lot of time on it. Also, if there is a better way, I'm all ears.

+6
source share
1 answer

Rules:

  • All types visible for this assembly must be declared in the assemblies referenced by this assembly.

    Unless your class library actually expands the types found in Microsoft.Owin.Security.Cookies in its open API, then other assemblies can safely compile your DLL without reference to this assembly.

  • A related assembly should not be present at runtime, unless the code in this assembly is really needed, that is, some other code tries to call this code.

    In general, this means that as long as other assemblies that reference your assembly and that do not reference Microsoft.Owin.Security.Cookies also do not call any code in your assembly, which would then try to call the code in Microsoft.Owin.Security.Cookies that the assembly should not be present at run time.

The hard part of this second point is that what constitutes the "call code in Microsoft.Owin.Security.Cookies " is not always explicit. As a rule, until you get access to assembly types at all, .NET will not try to execute any code in this assembly. But it’s easy to accidentally access these types, even if they are not necessary (for example, in initializers, static or otherwise, code that checks the implementation of the interface, etc.).

If you really want your clients to be able to use your DLL that references Microsoft.Owin.Security.Cookies , it is not necessary that this DLL is present at run time, you need to be very careful to make sure that you fully support this script It can be done, but it’s also not difficult to make a mistake.


(I must admit, I am surprised that this useful question has not yet been addressed in “Stack Overflow.” It seems like it would be so far. But I could not find a duplicate, hence the answer above. Does anyone know about the duplicate, which I forgot, I welcome any relevant notice of this.)

+4
source

All Articles