【发布时间】:2021-10-24 21:03:36
【问题描述】:
我以前知道this question has been asked multiple times,但是I've checked all I could not find any solution 那里没有一个专门处理/讨论可空类型。
用例
我想动态生成测试数据。为此,我传入的value 可以有不同的类型,我需要将它(通过CreateSomething)转换为某些数据类型,例如一个特殊格式的字符串。为此,我有如下所示的辅助函数。
一个示例是生成一个示例 XML 文件,以供以后在生产中针对系统进行测试时使用。然后使用value 断言正确的预期输出。
请注意,对于某些类型,我需要特殊处理,例如将布尔值转换为整数(如下所示),这就是我需要开关的原因。此外,生成的字符串提到了测试数据的类型(尽管它与 C# 中的所有类型不匹配/使用其他名称,例如布尔值被定义为整数)。
可能的例子
在 C# 8.0 中,这是可能的:
public static string GetSomething(object? value) =>
value switch
{
bool boolValue => CreateSomething("Integer", boolValue
? 1
: 0),
string stringValue => CreateSomething("String", stringValue),
int numericValue => CreateSomething("String", numericValue.ToString()),
null => CreateSomething("String", value),
_ => throw new ArgumentOutOfRangeException(nameof(value))
};
CreateSomething 是这样定义的:
string CreateSomething(string customType, object? value)
问题
但是,对于null 案例(如上所示),我还想处理类型。
正如您在其他情况下看到的那样,我想将所有bool 值作为第一个参数传递给Integer,并将String 传递给所有其他参数。
当然,value 是并保持null,但这是意料之中的,我只想保留信息/传递有关它应该期望什么类型的信息/在我的测试数据中定义事物,因为否则它被定义为另一种数据类型,因此不会产生尽可能真实的测试数据(不考虑类型)。
尝试
我想在那里使用第二个 switch 语句/或使用.GetType,但当然,在null 上的.GetType 不会像.GetType 那样工作,会抛出NullReferenceException。
我还研究了通过Nullable.GetUnderlyingType(value) 获取可为空的底层类型,但这也不起作用。而this question to get the type from null 只有使用泛型的答案,在我的情况下我不想要。
毕竟,我确实有这个不错的 C# switch type pattern matching 语句可以用于所有类型 - 为什么我不能也将它用于可空类型?
想法解决方案
毕竟,这将是理想的:
/* Code does not work! Only demonstration! */
public static string GetSomething(object? value) =>
value switch
{
bool? boolValue => CreateSomething("Integer", boolValue == null
? null : (boolValue ? 1 : 0),
string? stringValue => CreateSomething("String", stringValue),
int? numericValue => CreateSomething("String", numericValue?.ToString()),
_ => throw new ArgumentOutOfRangeException(nameof(value))
};
当然,我试过了,还是不行。
它只是向我显示错误 CS8400(“功能类型模式在 C# 8.0 中不可用。请使用语言版本 9.0 或更高版本。”)和“类型测试模式需要指定变量或在之后丢弃。”。 即使我使用 C# 9,它也会破坏语法,而不再向我显示任何显式编译器错误。
【问题讨论】:
-
您要完成的工作不是很清楚。您能否举一些调用您的方法的示例以及您希望它们做什么?
-
我认为您必须包含您期望的类型,不是吗?如果您的上一个示例有效,则无论何时您传递
null,bool?模式都会匹配,因为null不包含存储信息。无法判断null是int?还是bool?或Foo?,因为它不是对任何东西的引用。 -
@DM 是的,谢谢,显然没有办法让它像我想要的那样运行。看来我需要使用泛型或
Type参数来传递类型。
标签: c# .net-core switch-statement nullable c#-8.0