繁体   English   中英

PostSharp提供MissingMethodException

[英]PostSharp gives MissingMethodException

我为PostSharp开发了一个自定义方面,该方面过去可以正常工作,但是由于我更新了项目以使用最新的PostSharp NuGet而失败了。 方面如下:

[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。 (更不用说,卡在旧版本上很痛苦。)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM