Why in this case, an unused constructor causes assembly dependency?

There is a thin point here in the build dependencies that I would like to understand. I have a project that uses SharpDX through a custom shell, for example:

SharpDX.dll <- Wrapper.dll <- Project.dll

There is a type in Wrapper.dll such as:

public class D3DWrapperTypeA { //public D3DWrapperTypeA(SharpDX.Device device) { // //} public D3DWrapperTypeA(IntPtr devicePointer) { SharpDX.Device device = new SharpDX.Device(devicePointer); // etc } } 

In this class, if I uncomment the commented constructor, then Project.dll should refer to SharpDX.dll, even if it does not use the constructor.

However, I also have another type of shell:

 public class WrapperTypeB { public SharpDX.Device GetDevice(int adapter) { // etc } public IntPtr GetDevicePointer(int adapter) { return GetDevice(adapter).NativePointer; } } 

And here, until I actually use the GetDevice method, which returns the SharpDX object, Project.dll does not need to reference SharpDX.dll.

Why does even an unused constructor that accepts a parameter of type SharpDX call a dependency on SharpDX, while an unused method that returns a parameter of type SharpDX does not work?

+6
source share
2 answers

It is unclear how you know that you have an addiction. However, you will have compilation time . The runtime dependency will be much more difficult to explain.

A compile-time dependency exists because the C # compiler insists that it needs to know all the constructor overloads so that it can invoke the correct one. He will not be able to do this if he does not know anything about SharpDX.Device, if he cannot load metadata for SharpDX.dll.

This is not the case with the method that SharpDX.Device returns, the return type is never used to determine which overload is correct.

+6
source

In both cases, your Project.dll has a dependency on SharpDX.dll and this is completely normal. In both cases, SharpDX.dll will be copied to your bin output folder and used at runtime.

In the first case, you do not need to explicitly add a link to SharpDX.dll if you never try to call the constructor of the D3DWrapperTypeA class. Even if you try to call the second constructor, you still need a link.

The same is true for the second case: as long as you never call the GetDevice method, you do not need to add an explicit link.

0
source

Source: https://habr.com/ru/post/952663/


All Articles