2013-10-23 44 views
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。 (更不用提,被卡在一个旧版本是一种痛苦。)

回答

1

的描述用例的支持在PostSharp版本3.1.27 实施。 从该版本开始,可以对任何值类型属性应用验证方面ValidateValue(ValueType value, ...)

相关问题