You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FluentAssertions的BeOfType<>因RuntimeType验证失败的原因咨询

问题原因解析

你搞混了两种完全不同的验证逻辑:

  • BeOfType<T>()的作用是验证对象自身的运行时类型是否为T。这里autoMapAttribute.SourceType是一个Type抽象类的实例(实际底层实现是内部类System.RuntimeType),而IbiSiliconProductStepping是你的业务类型,两者没有继承关系,所以验证必然失败。
  • Be(typeof(IbiSiliconProductStepping))是验证对象的值是否等于目标类型的Type实例。你真正的需求是确认SourceType存储的类型就是IbiSiliconProductStepping,所以这个方法才是正确的验证方式。

举个更直白的例子:如果有变量var strType = typeof(string);,用strType.Should().BeOfType<string>()肯定失败——因为strType是RuntimeType对象,不是字符串;而strType.Should().Be(typeof(string))会成功,因为这是在验证该Type对象指向的确实是string类型。

内容的提问来源于stack exchange,提问作者Gargoyle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:32:34