gpt4 book ai didi

c# - PostSharp 给出 MissingMethodException

转载 作者:太空宇宙 更新时间:2023-11-03 13:33:35 25 4
gpt4 key购买 nike

我为 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, ...) 应用验证方面。

关于c# - PostSharp 给出 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19531792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com