我有一个Visual Studio 2015解决方案,我在其中复制并粘贴了许多PostSharp验证属性,这些属性我已在Visual Studio 2013项目中成功使用。
项目全部编译并成功运行。不幸的是,用于测试我的验证属性的单元测试失败了。通过调试,我发现我的所有属性都没有运行。我已经确保PostSharp包在项目引用以及.xproj文件中被正确引用。
我的验证属性代码如下:
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;
}
}
}
我的项目。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是完全支持的,但我似乎无法让这些方面发挥作用。为了让PostSharp在Visual Studio 2015上运行,我需要做什么吗?