2
我爲PostSharp開發了一個自定義方面,該方法用於正常工作,但現在已經更新我的項目以使用最新的PostSharp NuGet失敗。方面如下: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。 (更不用提,被卡在一箇舊版本是一種痛苦。)