From a89533aea76aabfa7e1d06fdae49ec1f6b1cff0e Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sat, 23 Sep 2023 19:46:30 +0200 Subject: [PATCH] Fixed issues with seemingly incompatible language features in .NET 6 --- .../ScreenCapture.NET.DX11.csproj | 3 +- .../ScreenCapture.NET.DX9.csproj | 3 +- ScreenCapture.NET/Model/CaptureZone.cs | 11 ++++++- ScreenCapture.NET/Model/Colors/ColorABGR.cs | 7 ++++- ScreenCapture.NET/Model/Colors/ColorARGB.cs | 7 ++++- ScreenCapture.NET/Model/Colors/ColorBGR.cs | 7 ++++- ScreenCapture.NET/Model/Colors/ColorBGRA.cs | 9 ++++-- ScreenCapture.NET/Model/Colors/ColorRGB.cs | 7 ++++- ScreenCapture.NET/Model/Colors/ColorRGBA.cs | 5 ++++ ScreenCapture.NET/Model/Colors/IColor.cs | 4 +++ ScreenCapture.NET/Model/Image.cs | 29 +++++++++++++++++-- .../ScreenCapture.NET.Tests.csproj | 2 +- 12 files changed, 81 insertions(+), 13 deletions(-) diff --git a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj index 7e5a672..6c23a98 100644 --- a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj +++ b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj @@ -1,6 +1,7 @@  - net7.0-windows;net6.0-windows + net7.0;net6.0 + win-x64 latest enable true diff --git a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj index e3e3fb1..8dc8e32 100644 --- a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj +++ b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj @@ -1,6 +1,7 @@  - net7.0-windows;net6.0-windows + net7.0;net6.0 + win-x64 latest enable true diff --git a/ScreenCapture.NET/Model/CaptureZone.cs b/ScreenCapture.NET/Model/CaptureZone.cs index fca089c..7b75431 100644 --- a/ScreenCapture.NET/Model/CaptureZone.cs +++ b/ScreenCapture.NET/Model/CaptureZone.cs @@ -20,12 +20,21 @@ public sealed class CaptureZone : ICaptureZone /// public Display Display { get; } +#if NET7_0_OR_GREATER /// public ColorFormat ColorFormat { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => TColor.ColorFormat; } +#else + /// + public ColorFormat ColorFormat + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => default(TColor).Net6ColorFormat; + } +#endif /// public int X { get; internal set; } @@ -95,7 +104,7 @@ public sealed class CaptureZone : ICaptureZone /// public bool IsUpdateRequested { get; private set; } - #endregion +#endregion #region Events diff --git a/ScreenCapture.NET/Model/Colors/ColorABGR.cs b/ScreenCapture.NET/Model/Colors/ColorABGR.cs index 00cfa36..bf0676c 100644 --- a/ScreenCapture.NET/Model/Colors/ColorABGR.cs +++ b/ScreenCapture.NET/Model/Colors/ColorABGR.cs @@ -17,6 +17,11 @@ public readonly struct ColorABGR : IColor /// public static ColorFormat ColorFormat => ColorFormat.ABGR; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _a; private readonly byte _b; private readonly byte _g; @@ -39,7 +44,7 @@ public readonly struct ColorABGR : IColor #endregion #region Constructors - + /// /// Initializes a new instance of the class. /// diff --git a/ScreenCapture.NET/Model/Colors/ColorARGB.cs b/ScreenCapture.NET/Model/Colors/ColorARGB.cs index 2758be5..26e6561 100644 --- a/ScreenCapture.NET/Model/Colors/ColorARGB.cs +++ b/ScreenCapture.NET/Model/Colors/ColorARGB.cs @@ -13,10 +13,15 @@ namespace ScreenCapture.NET; public readonly struct ColorARGB : IColor { #region Properties & Fields - + /// public static ColorFormat ColorFormat => ColorFormat.ARGB; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _a; private readonly byte _r; private readonly byte _g; diff --git a/ScreenCapture.NET/Model/Colors/ColorBGR.cs b/ScreenCapture.NET/Model/Colors/ColorBGR.cs index e6f1a12..f837f17 100644 --- a/ScreenCapture.NET/Model/Colors/ColorBGR.cs +++ b/ScreenCapture.NET/Model/Colors/ColorBGR.cs @@ -13,10 +13,15 @@ namespace ScreenCapture.NET; public readonly struct ColorBGR : IColor { #region Properties & Fields - + /// public static ColorFormat ColorFormat => ColorFormat.BGR; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _b; private readonly byte _g; private readonly byte _r; diff --git a/ScreenCapture.NET/Model/Colors/ColorBGRA.cs b/ScreenCapture.NET/Model/Colors/ColorBGRA.cs index 1db7ea3..0689148 100644 --- a/ScreenCapture.NET/Model/Colors/ColorBGRA.cs +++ b/ScreenCapture.NET/Model/Colors/ColorBGRA.cs @@ -13,10 +13,15 @@ namespace ScreenCapture.NET; public readonly struct ColorBGRA : IColor { #region Properties & Fields - + /// public static ColorFormat ColorFormat => ColorFormat.BGRA; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _b; private readonly byte _g; private readonly byte _r; @@ -36,7 +41,7 @@ public readonly struct ColorBGRA : IColor public byte A => _a; // ReSharper restore ConvertToAutoPropertyWhenPossible - #endregion +#endregion #region Constructors diff --git a/ScreenCapture.NET/Model/Colors/ColorRGB.cs b/ScreenCapture.NET/Model/Colors/ColorRGB.cs index 36dd932..cbc7e86 100644 --- a/ScreenCapture.NET/Model/Colors/ColorRGB.cs +++ b/ScreenCapture.NET/Model/Colors/ColorRGB.cs @@ -13,10 +13,15 @@ namespace ScreenCapture.NET; public readonly struct ColorRGB : IColor { #region Properties & Fields - + /// public static ColorFormat ColorFormat => ColorFormat.RGB; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _r; private readonly byte _g; private readonly byte _b; diff --git a/ScreenCapture.NET/Model/Colors/ColorRGBA.cs b/ScreenCapture.NET/Model/Colors/ColorRGBA.cs index d51a7a1..df3379a 100644 --- a/ScreenCapture.NET/Model/Colors/ColorRGBA.cs +++ b/ScreenCapture.NET/Model/Colors/ColorRGBA.cs @@ -17,6 +17,11 @@ public readonly struct ColorRGBA : IColor /// public static ColorFormat ColorFormat => ColorFormat.RGBA; +#if !NET7_0_OR_GREATER + /// + public ColorFormat Net6ColorFormat => ColorFormat; +#endif + private readonly byte _r; private readonly byte _g; private readonly byte _b; diff --git a/ScreenCapture.NET/Model/Colors/IColor.cs b/ScreenCapture.NET/Model/Colors/IColor.cs index 826c8db..8a1c3d1 100644 --- a/ScreenCapture.NET/Model/Colors/IColor.cs +++ b/ScreenCapture.NET/Model/Colors/IColor.cs @@ -27,8 +27,12 @@ public interface IColor /// byte A { get; } +#if NET7_0_OR_GREATER /// /// Gets the color-format of this color. /// public static virtual ColorFormat ColorFormat => throw new NotSupportedException(); +#else + public ColorFormat Net6ColorFormat { get; } +#endif } \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Image.cs b/ScreenCapture.NET/Model/Image.cs index 2eb99a0..4cd6bf1 100644 --- a/ScreenCapture.NET/Model/Image.cs +++ b/ScreenCapture.NET/Model/Image.cs @@ -18,8 +18,21 @@ public sealed class Image : IImage private readonly int _y; private readonly int _stride; +#if NET7_0_OR_GREATER /// - public ColorFormat ColorFormat => TColor.ColorFormat; + public ColorFormat ColorFormat + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => TColor.ColorFormat; + } +#else + /// + public ColorFormat ColorFormat + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => default(TColor).Net6ColorFormat; + } +#endif /// public int Width { get; } @@ -28,7 +41,7 @@ public sealed class Image : IImage public int Height { get; } /// - public int SizeInBytes => Width * Height * TColor.ColorFormat.BytesPerPixel; + public int SizeInBytes => Width * Height * ColorFormat.BytesPerPixel; #endregion @@ -96,7 +109,7 @@ public sealed class Image : IImage if (destination == null) throw new ArgumentNullException(nameof(destination)); if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination)); - int targetStride = Width * TColor.ColorFormat.BytesPerPixel; + int targetStride = Width * ColorFormat.BytesPerPixel; IImage.IImageRows rows = Rows; Span target = destination; foreach (IImage.IImageRow row in rows) @@ -211,8 +224,13 @@ public sealed class Image : IImage /// public int Length => _length; +#if NET7_0_OR_GREATER /// public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; +#else + /// + public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel; +#endif #endregion @@ -349,8 +367,13 @@ public sealed class Image : IImage /// public int Length => _length; +#if NET7_0_OR_GREATER /// public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; +#else + /// + public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel; +#endif #endregion diff --git a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj index b4b9251..467a6f5 100644 --- a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj +++ b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net7.0;net6.0 enable false