Search code examples
c#visual-studionugetnuget-packagesystem.drawing

Why unable to remove System.Drawing and replace with NuGet package System.Drawing.Common


I'm trying to remove the framework reference of System.Drawing and use the NuGet package of System.Drawing.Common instead

However, when I remove System.Drawing, after I install System.Drawing.Common from NuGet Package Manager, I see System.Drawing is back in my references.

I want to remove it and use the NuGet version only

Is that possible?


Solution

  • Using fuget to look at the package's nuspec, we can see the following:

    <frameworkAssemblies>
      <frameworkAssembly assemblyName="mscorlib" targetFramework=".NETFramework4.6.1" />
      <frameworkAssembly assemblyName="System" targetFramework=".NETFramework4.6.1" />
      <frameworkAssembly assemblyName="System.Drawing" targetFramework=".NETFramework4.6.1" />
    </frameworkAssemblies>
    

    So, the package is telling NuGet that System.Drawingis needed.

    If you're using packages.config to import NuGet packages into your project, the way is works, by design, is that NuGet will tell Visual Studio's project system to add these references at package install time. Therefore, installing the package will add the reference back.

    Using NuGet packages with PackageReference (needs Visual Studio 2017, I can't remember which version, maybe 15.2 or higher, but maybe it worked in 15.0) will not do the same. With PackageReference, only the package reference is added to your csproj, and then transitive dependencies are handled automatically through the build system, without modifying your csproj. However, not all project types support PackageReference (and as previously stated, VS2015 and earlier don't support it either).