首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PostSharp给出MissingMethodException

PostSharp给出MissingMethodException
EN

Stack Overflow用户
提问于 2013-10-23 02:37:20
回答 1查看 384关注 0票数 2

我已经为PostSharp开发了一个自定义方面,它过去工作得很好,但是现在我已经更新了我的项目以使用最新的PostSharp NuGet,所以失败了。具体内容如下:

代码语言:javascript
复制
[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;
    }
}

该方面的示例使用如下:

代码语言:javascript
复制
public class Example
{
    [NotDefault]
    public DateTime Timestamp { get; set; }
}

当代码试图设置属性时,如本例所示.

代码语言:javascript
复制
new Example().Timestamp = DateTime.UtcNow;

..。我得到以下MissingMethodException

代码语言:javascript
复制
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解决了这个问题。但是,这并不是一个真正的选择,因为我们即将切换到.NET 4.5.1,它需要3.0.38或更高版本。(更不用说,坚持旧版本是一件痛苦的事。)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-26 11:08:11

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19531792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档