在aspnet-api-versioning中,我发现了一个码块:
DefaultApiControllerFilter( IEnumerable<IApiControllerSpecification> pecifications )
{
Arg.NotNull( specifications, nameof( specifications ) );
this.specifications = specifications.ToArray();
}感兴趣的块是来自Microsoft名称空间的Arg.NotNull( value, "text" );。
并且在代码中有几个类似的断言。另一个例子是来自System.Diagnostics.Contracts的Contract.Requires()
已尝试搜索有关工作原理的Microsoft文档,但未找到相关信息。
因此,也许可以帮助了解它是如何工作的:像postsharp代码重写一样,提供运行时条件检查作为Debug.Assert,或者可能只是抛出异常(但它在文档中没有提到)?
发布于 2019-02-18 18:48:01
发布于 2019-02-18 20:03:50
下载库源代码后,编译并查看编译后的代码,发现Microsoft::Arg只是一个带有方法的shared code project
internal static void NotNull<T>(T value, string name) where T : class
{
if ((object) value == null) throw new ArgumentNullException(name);
}Contract.Requires(condition)是一个Code Contract Assert代码生成扩展,由于没有assert post build事件,它不会生成任何代码。The similar sutuation on stackowerflow。
https://stackoverflow.com/questions/54744884
复制相似问题