2

以前は正常に動作していた 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 に切り替えようとしているため、これは実際にはオプションではありません。(言うまでもなく、古いリリースに固執するのは苦痛です。)

4

1 に答える 1