Fixed issues with seemingly incompatible language features in .NET 6

This commit is contained in:
Darth Affe 2023-09-23 19:46:30 +02:00
parent d5e17f7370
commit a89533aea7
12 changed files with 81 additions and 13 deletions

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net7.0-windows;net6.0-windows</TargetFrameworks> <TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFrameworks>net7.0-windows;net6.0-windows</TargetFrameworks> <TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<LangVersion>latest</LangVersion> <LangVersion>latest</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

View File

@ -20,12 +20,21 @@ public sealed class CaptureZone<TColor> : ICaptureZone
/// <inheritdoc /> /// <inheritdoc />
public Display Display { get; } public Display Display { get; }
#if NET7_0_OR_GREATER
/// <inheritdoc /> /// <inheritdoc />
public ColorFormat ColorFormat public ColorFormat ColorFormat
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TColor.ColorFormat; get => TColor.ColorFormat;
} }
#else
/// <inheritdoc />
public ColorFormat ColorFormat
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => default(TColor).Net6ColorFormat;
}
#endif
/// <inheritdoc /> /// <inheritdoc />
public int X { get; internal set; } public int X { get; internal set; }
@ -95,7 +104,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
/// <inheritdoc /> /// <inheritdoc />
public bool IsUpdateRequested { get; private set; } public bool IsUpdateRequested { get; private set; }
#endregion #endregion
#region Events #region Events

View File

@ -17,6 +17,11 @@ public readonly struct ColorABGR : IColor
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.ABGR; public static ColorFormat ColorFormat => ColorFormat.ABGR;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _a; private readonly byte _a;
private readonly byte _b; private readonly byte _b;
private readonly byte _g; private readonly byte _g;
@ -39,7 +44,7 @@ public readonly struct ColorABGR : IColor
#endregion #endregion
#region Constructors #region Constructors
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ColorABGR"/> class. /// Initializes a new instance of the <see cref="ColorABGR"/> class.
/// </summary> /// </summary>

View File

@ -13,10 +13,15 @@ namespace ScreenCapture.NET;
public readonly struct ColorARGB : IColor public readonly struct ColorARGB : IColor
{ {
#region Properties & Fields #region Properties & Fields
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.ARGB; public static ColorFormat ColorFormat => ColorFormat.ARGB;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _a; private readonly byte _a;
private readonly byte _r; private readonly byte _r;
private readonly byte _g; private readonly byte _g;

View File

@ -13,10 +13,15 @@ namespace ScreenCapture.NET;
public readonly struct ColorBGR : IColor public readonly struct ColorBGR : IColor
{ {
#region Properties & Fields #region Properties & Fields
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.BGR; public static ColorFormat ColorFormat => ColorFormat.BGR;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _b; private readonly byte _b;
private readonly byte _g; private readonly byte _g;
private readonly byte _r; private readonly byte _r;

View File

@ -13,10 +13,15 @@ namespace ScreenCapture.NET;
public readonly struct ColorBGRA : IColor public readonly struct ColorBGRA : IColor
{ {
#region Properties & Fields #region Properties & Fields
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.BGRA; public static ColorFormat ColorFormat => ColorFormat.BGRA;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _b; private readonly byte _b;
private readonly byte _g; private readonly byte _g;
private readonly byte _r; private readonly byte _r;
@ -36,7 +41,7 @@ public readonly struct ColorBGRA : IColor
public byte A => _a; public byte A => _a;
// ReSharper restore ConvertToAutoPropertyWhenPossible // ReSharper restore ConvertToAutoPropertyWhenPossible
#endregion #endregion
#region Constructors #region Constructors

View File

@ -13,10 +13,15 @@ namespace ScreenCapture.NET;
public readonly struct ColorRGB : IColor public readonly struct ColorRGB : IColor
{ {
#region Properties & Fields #region Properties & Fields
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.RGB; public static ColorFormat ColorFormat => ColorFormat.RGB;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _r; private readonly byte _r;
private readonly byte _g; private readonly byte _g;
private readonly byte _b; private readonly byte _b;

View File

@ -17,6 +17,11 @@ public readonly struct ColorRGBA : IColor
/// <inheritdoc /> /// <inheritdoc />
public static ColorFormat ColorFormat => ColorFormat.RGBA; public static ColorFormat ColorFormat => ColorFormat.RGBA;
#if !NET7_0_OR_GREATER
/// <inheritdoc />
public ColorFormat Net6ColorFormat => ColorFormat;
#endif
private readonly byte _r; private readonly byte _r;
private readonly byte _g; private readonly byte _g;
private readonly byte _b; private readonly byte _b;

View File

@ -27,8 +27,12 @@ public interface IColor
/// </summary> /// </summary>
byte A { get; } byte A { get; }
#if NET7_0_OR_GREATER
/// <summary> /// <summary>
/// Gets the color-format of this color. /// Gets the color-format of this color.
/// </summary> /// </summary>
public static virtual ColorFormat ColorFormat => throw new NotSupportedException(); public static virtual ColorFormat ColorFormat => throw new NotSupportedException();
#else
public ColorFormat Net6ColorFormat { get; }
#endif
} }

View File

@ -18,8 +18,21 @@ public sealed class Image<TColor> : IImage
private readonly int _y; private readonly int _y;
private readonly int _stride; private readonly int _stride;
#if NET7_0_OR_GREATER
/// <inheritdoc /> /// <inheritdoc />
public ColorFormat ColorFormat => TColor.ColorFormat; public ColorFormat ColorFormat
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TColor.ColorFormat;
}
#else
/// <inheritdoc />
public ColorFormat ColorFormat
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => default(TColor).Net6ColorFormat;
}
#endif
/// <inheritdoc /> /// <inheritdoc />
public int Width { get; } public int Width { get; }
@ -28,7 +41,7 @@ public sealed class Image<TColor> : IImage
public int Height { get; } public int Height { get; }
/// <inheritdoc /> /// <inheritdoc />
public int SizeInBytes => Width * Height * TColor.ColorFormat.BytesPerPixel; public int SizeInBytes => Width * Height * ColorFormat.BytesPerPixel;
#endregion #endregion
@ -96,7 +109,7 @@ public sealed class Image<TColor> : IImage
if (destination == null) throw new ArgumentNullException(nameof(destination)); 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)); 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; IImage.IImageRows rows = Rows;
Span<byte> target = destination; Span<byte> target = destination;
foreach (IImage.IImageRow row in rows) foreach (IImage.IImageRow row in rows)
@ -211,8 +224,13 @@ public sealed class Image<TColor> : IImage
/// <inheritdoc /> /// <inheritdoc />
public int Length => _length; public int Length => _length;
#if NET7_0_OR_GREATER
/// <inheritdoc /> /// <inheritdoc />
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
#else
/// <inheritdoc />
public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel;
#endif
#endregion #endregion
@ -349,8 +367,13 @@ public sealed class Image<TColor> : IImage
/// <inheritdoc /> /// <inheritdoc />
public int Length => _length; public int Length => _length;
#if NET7_0_OR_GREATER
/// <inheritdoc /> /// <inheritdoc />
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
#else
/// <inheritdoc />
public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel;
#endif
#endregion #endregion

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>