我有一个Visual Studio 2015的解决方案,其中我复制并粘贴了一些PostSharp验证属性,这些属性之前在Visual Studio 2013项目中使用成功。该项目编译和运行都成功。然而,用于测试我的验证属性的单元测试失败了。通过调试,我发现我的所有属性都没有被执行。我已确保在项目引用以及.xproj文件中正确引用了PostSharp包。我的验证属性代码如下:
using System;
using PostSharp.Aspects;
using PostSharp.Reflection;
namespace Foo.Domain.Model.ValidationAttributes
{
/// <summary>
/// Validates the parameter to strictly not be null (empty and whitespace is valid). Throws a System.ArgumentNullException if assigned a null value.
/// </summary>
[Serializable]
public sealed class NotNullAttribute : LocationInterceptionAspect, ILocationValidationAspect<object>
{
public NotNullAttribute()
{
}
public override void OnSetValue(LocationInterceptionArgs args)
{
Exception ex = ValidateValue(args.Value, args.LocationName, args.Location.LocationKind);
if (ex != null)
{
throw ex;
}
args.ProceedSetValue();
}
public Exception ValidateValue(object value, string locationName, LocationKind locationKind)
{
return value == null ? new ArgumentNullException(locationName, string.Format("The parameter '{0}' may not be null.", locationName)) : null;
}
}
}
我的project.json显示了添加的依赖项:
{
"version": "1.0.0-*",
"description": "MyProject Domain Class Library",
"tags": [ "MyProject" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"PostSharp": "4.1.21",
"PostSharp.Patterns.Common": "4.1.21",
"PostSharp.Patterns.Model": "4.1.21"
},
"frameworks": {
"net451": { }
}
}
我的.xproj文件显示了添加的PostSharp目标(我已检查它存在于路径中):
<Import Project="..\packages\PostSharp\4.1.21\tools\PostSharp.targets" />
从PostSharp文档中可以看出,VS2015得到了充分支持,但我似乎无法使这些方面工作。需要做些什么才能在Visual Studio 2015上运行PostSharp?