向接口添加运算符支持(.NET 6 中的预览功能)
Adding operator support to interfaces (Preview Feature in .NET 6)
只是一个警告,这个问题需要安装 .NET 6 预览版
我正在尝试在 C# 中创建一个接口,允许 +
运算符类似于它在 Microsoft INumber<T>
.
中的实现方式
Interfaces.cs
using System;
using System.Runtime.Versioning;
namespace InterfaceTest;
[RequiresPreviewFeatures]
public interface IExtendedArray<T> : IAdditionOperators<IExtendedArray<T>, IExtendedArray<T>, IExtendedArray<T>>
where T : INumber<T>
{
Array Data { get; }
}
[RequiresPreviewFeatures]
public interface IExtendedFloatingPointArray<T> : IExtendedArray<T>
where T : IFloatingPoint<T>
{
}
InterfaceTest.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
</ItemGroup>
</Project>
但是,此代码生成
error CS8920: The interface 'InterfaceTest.IExtendedArray' cannot be used as type parameter 'TSelf' in the generic type or method 'IAdditionOperators<TSelf, TOther, TResult>'. The constraint interface 'System.IAdditionOperators<InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray>' or its base interface has static abstract members.
有没有办法用自定义类型实现这个?
dotnet --list-sdks
显示我已安装 6.0.100-rc.1.21458.32
。但我刚刚通过 Visual Studio 2022 Preview 4 安装了它。
我终于能够重现您的问题 - 需要安装 VS 2022 预览版(2019 版编译代码很好但在运行时失败)或从终端使用 dotnet build
。
如果你想用 INumber
做一些类似的事情,你需要遵循与 TSelf
类型引用相同的模式(如果我没记错的话它被称为 curiously recurring template pattern):
[RequiresPreviewFeatures]
public interface IExtendedArray<TSelf, T> : IAdditionOperators<TSelf, TSelf, TSelf> where TSelf : IExtendedArray<TSelf, T> where T : INumber<T>
{
T[] Data { get; }
}
[RequiresPreviewFeatures]
public interface IExtendedFloatingPointArray<TSelf, T> : IExtendedArray<TSelf, T>
where TSelf : IExtendedFloatingPointArray<TSelf, T>
where T : IFloatingPoint<T>
{
}
只是一个警告,这个问题需要安装 .NET 6 预览版
我正在尝试在 C# 中创建一个接口,允许 +
运算符类似于它在 Microsoft INumber<T>
.
Interfaces.cs
using System;
using System.Runtime.Versioning;
namespace InterfaceTest;
[RequiresPreviewFeatures]
public interface IExtendedArray<T> : IAdditionOperators<IExtendedArray<T>, IExtendedArray<T>, IExtendedArray<T>>
where T : INumber<T>
{
Array Data { get; }
}
[RequiresPreviewFeatures]
public interface IExtendedFloatingPointArray<T> : IExtendedArray<T>
where T : IFloatingPoint<T>
{
}
InterfaceTest.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<EnablePreviewFeatures>true</EnablePreviewFeatures>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>false</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.Experimental" Version="6.0.0-preview.7.21377.19" />
</ItemGroup>
</Project>
但是,此代码生成
error CS8920: The interface 'InterfaceTest.IExtendedArray' cannot be used as type parameter 'TSelf' in the generic type or method 'IAdditionOperators<TSelf, TOther, TResult>'. The constraint interface 'System.IAdditionOperators<InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray, InterfaceTest.IExtendedArray>' or its base interface has static abstract members.
有没有办法用自定义类型实现这个?
dotnet --list-sdks
显示我已安装 6.0.100-rc.1.21458.32
。但我刚刚通过 Visual Studio 2022 Preview 4 安装了它。
我终于能够重现您的问题 - 需要安装 VS 2022 预览版(2019 版编译代码很好但在运行时失败)或从终端使用 dotnet build
。
如果你想用 INumber
做一些类似的事情,你需要遵循与 TSelf
类型引用相同的模式(如果我没记错的话它被称为 curiously recurring template pattern):
[RequiresPreviewFeatures]
public interface IExtendedArray<TSelf, T> : IAdditionOperators<TSelf, TSelf, TSelf> where TSelf : IExtendedArray<TSelf, T> where T : INumber<T>
{
T[] Data { get; }
}
[RequiresPreviewFeatures]
public interface IExtendedFloatingPointArray<TSelf, T> : IExtendedArray<TSelf, T>
where TSelf : IExtendedFloatingPointArray<TSelf, T>
where T : IFloatingPoint<T>
{
}