gpt4 book ai didi

c# - 我可以在框架方法上使用 SuppressMessage 吗?

转载 作者:太空狗 更新时间:2023-10-29 23:41:03 26 4
gpt4 key购买 nike

我想实现 CodeContracts 的以下建议:

CodeContracts: MyModule: Method MyModule.MyClass.MyMethod: 
To mask *all* warnings issued like the precondition add the attribute:

[SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null")]

to the method

System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)

感觉我应该能够使用带有 Target 属性的 SupressMessage 来实现这一点。但是,因为这是一个框架方法,所以我不确定。

//doesn't work
[module: SuppressMessage("Microsoft.Contracts", "RequiresAtCall-propertyAccessor != null", Scope = "Member", Target = "System.Linq.Expressions.Expression.Property(System.Linq.Expressions.Expression,System.Reflection.MethodInfo)", Justification = "This isn't covered by Linq Contracts yet.")]

我如何才能全局抑制此警告,这样我就不必设置基线或抑制所有调用站点警告?

EDIT: The specific usage that requires this measure is:

void mymethod()
{
var myObserver = new PropertyObserver<MyViewModel>();
//this line throws the error, within the n => n.Value expression
myObserver.RegisterHandler(n => n.Value, OnValueChanged);
}

public class PropertyObserver<TPropertySource> where TPropertySource : INotifyPropertyChanged
{
public PropertyObserver<TPropertySource> RegisterHandler(
Expression<Func<TPropertySource, object>> expression,
Action<TPropertySource> handler)
{
//what this does is irrelevant; the violation occurs in the method call
}
}

//n => n.Value decompiles to the following
public static MemberExpression Property (Expression expression, MethodInfo propertyAccessor)
{
//and this line is the message I want to suppress, but it's in the .NET framework.
ContractUtils.RequiresNotNull(propertyAccessor, "propertyAccessor");
ValidateMethodInfo(propertyAccessor);
return Property (expression, GetProperty(propertyAccessor));
}

最佳答案

在对 ranomore 进行更多调查后,代码契约中似乎存在错误。

通过 n => n.Value 访问的类有一个通用的 T Value 属性。如果该类更改为非通用类(具有 object Value),则警告消失。 (具有 object Value 的泛型类也会发出警告)。

当然,这并没有回答最初的问题,但我认为这样做是不可能的。

关于c# - 我可以在框架方法上使用 SuppressMessage 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609110/

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