Skip to content

Can't get or set SvgRenderer Foreground/Background colors in .NETStandard2.0 library that is then used in a .NET5+ application #628

Description

@bstadick

Issue

The change in commit b45be40 removed the use of SvgRenderer.Color struct for the .NETStandard2.0 version of the ZXing.Net library in favor of System.Drawing.Color for the SvgRenderer.Foreground and SvgRenderer.Background. The .NET5+ versions of the ZXing.Net library still uses the SvgRenderer.Color struct for these properties.

The issue becomes if you create a .NETStandard2.0 library that uses ZXing.Net it will use System.Drawing.Color to set the SvgRenderer.Foreground or SvgRenderer.Background. If you then consume this library in an application that targets .NET5+ it will use the ZXing.Net version that instead uses SvgRenderer.Color. Because of this mismatch at runtime you get an exception of type or similar to System.MissingMethodException : Method not found: 'Void ZXing.Rendering.SvgRenderer.set_Background(System.Drawing.Color)'..

Background

.NETStandard is meant to conform to the consuming .NET framework, so having this mismatch in method signatures breaks that contract. Either .NET5+ needs to be made to also use System.Drawing.Color or the .NETStandard2.0 needs to be reverted to use SvgRenderer.Color.

The same issue would apply to PixelDataRenderer as well as .NETCore targeted apps. Both would be fixed with the below suggested change. It looks like .NETFramework4.6+ correctly conforms with the use of System.Drawing.Color. I didn't look into the other older or less used frameworks. For more on what frameworks of .NET need to support .NETStandard2.0 is found here. https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0#select-net-standard-version

Proposed Fix

The underlying issue may be that the intention was that .NET5+ would conform to .NETStandard2.0 choice for using System.Drawing.Color, but the pre-processor condition NETSTANDARD2_0_OR_GREATER is not defined in the csproj file. Any place in https://github.com/micjahn/ZXing.Net/blob/master/Source/lib/netstandard/ZXing.Net.csproj that manually defines NETSTANDARD2_0 also needs to manually define NETSTANDARD2_0_OR_GREATER. The _OR_GREATER definition isn't given for free here since NETSTANDARD2_0 is being manually defined for .NET target frameworks that would otherwise not define it.

e.g.

<PropertyGroup Condition="'$(TargetFramework)'=='net5.0'">
  <DefineConstants>$(DefineConstants);NETSTANDARD2_0;NETSTANDARD2_0_OR_GREATER</DefineConstants>
</PropertyGroup>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions