我有一个可为空的 c# 10 .net 6 项目,其扩展方法为ThrowIfNull
using System;
using System.Runtime.CompilerServices;
#nullable enable
public static class NullExtensions
{
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
)
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return argument;
}
}
}
Run Code Online (Sandbox Code Playgroud)
扩展方法隐式转换string?为string但它不适用于其他基本类型,例如int?orbool?
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
string? foo = "foo";
string nonNullableFoo = foo.ThrowIfNull(); // success from "string?" to "string"
Console.WriteLine(nonNullableFoo);
bool? baz = true;
bool nonNullableBaz = baz.ThrowIfNull(); // success from "string?" to "string"
Console.WriteLine(nonNullableFoo);
int? bar = 2;
int nonNullableBar = bar.ThrowIfNull(); // error: Cannot implicitly convert type 'int?' to 'int'
Console.WriteLine(nonNullableBar);
}
}
Run Code Online (Sandbox Code Playgroud)
如何使扩展隐式转换int?and bool??
这是完整的 dotnet 小提琴https://dotnetfiddle.net/LiQ8NL
您可以通过为不可空引用类型提供一种扩展方法并为非托管(例如 int、bool、...)类型提供另一种扩展方法来实现您的目标。请注意,非托管类型需要强制转换。
public static class NullExtensions
{
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
) where T : notnull
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return argument;
}
}
public static T ThrowIfNull<T>(
this T? argument,
string? message = default,
[CallerArgumentExpression("argument")] string? paramName = default
) where T : unmanaged
{
if (argument is null)
{
throw new ArgumentNullException(paramName, message);
}
else
{
return (T)argument;
}
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
int? foo = 42;
int bar = foo.ThrowIfNull();
Console.WriteLine(bar);
string? baz = "Hello";
string quus = baz.ThrowIfNull();
Console.WriteLine(quus);
// Comment out either this
baz = null;
quus = baz.ThrowIfNull();
// Or this
foo = null;
bar = foo.ThrowIfNull();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13170 次 |
| 最近记录: |