잘 작동했지만 이전 PostSharp NuGet을 사용하도록 프로젝트를 업데이트 한 이후에 실패한 PostSharp에 대한 사용자 정의 aspect를 개발했습니다.PostSharp가 MissingMethodException을 제공합니다
[HasConstraint]
public sealed class NotDefaultAttribute : LocationContractAttribute, ILocationValidationAspect<ValueType>, ILocationValidationAspect, IAspect
{
public NotDefaultAttribute()
{
ErrorMessage = "The {2} cannot be empty.";
}
public Exception ValidateValue(ValueType value, string locationName, LocationKind locationKind)
{
if (value == null)
return null;
var type = value.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (value.Equals(Activator.CreateInstance(type.GetGenericArguments()[0])))
return CreateArgumentException(value, locationName, locationKind);
}
else if (value.Equals(Activator.CreateInstance(type)))
return CreateArgumentNullException(value, locationName, locationKind);
return null;
}
}
그리고 측면의 샘플 사용은 다음과 같습니다 : 다음과 같은 측면이다
public class Example
{
[NotDefault]
public DateTime Timestamp { get; set; }
}
코드는 다음 예제와 같이 속성을 설정하려고
...
new Example().Timestamp = DateTime.UtcNow;
... 나는 MissingMethodException
다음 얻을 :
System.MissingMethodException: Method not found: 'System.Exception NotDefaultAttribute.ValidateValue(System.ValueType, System.String, PostSharp.Reflection.LocationKind)'.
at Example.set_Timestamp(DateTime value)
PostSharp를 3.0.36으로 다운 그레이드하면이 문제가 해결됩니다. 그러나 3.0.38 이상이 필요한 .NET 4.5.1로 전환 할 때 실제로는 옵션이 아닙니다. (오래된 버전에 고통을 것입니다 갇혀되고, 언급 할 필요가 없을 것입니다.)