在Entity Framework的源代码(链接)中,我找到了这一行:
public virtual IRelationalTransaction Transaction
{ get; [param: NotNull] protected set; }
Run Code Online (Sandbox Code Playgroud)
这[param: NotNull]部分对我来说很奇怪.知道这是什么样的C#语法吗?我熟悉属性和参数,但不熟悉这种组合.
NotNull的定义是这样的:
[AttributeUsage(
AttributeTargets.Method | AttributeTargets.Parameter |
AttributeTargets.Property | AttributeTargets.Delegate |
AttributeTargets.Field)]
internal sealed class NotNullAttribute : Attribute
{
}
Run Code Online (Sandbox Code Playgroud)
我希望它被简单地用作[NotNull]但param在这里做什么?
我正在看asp.net vnext 工程指南,并注意到他们建议使用NotNull属性而不是显式检查null和throw 的输入参数ArgumentNullException.让我感到困惑的是,根据这个指南,它足以声明这个属性,并且检查代码将在编译时生成到方法体中.我试图在我的测试项目中做到这一点,但魔术没有发生,即它抛出异常System.NullReferenceException而不是System.ArgumentNullException.这应该如何工作?他们是否会在编译时使用一些AOP库来注入检查代码?