需要无法访问 [英] Requires unreachable
问题描述
我不明白为什么我在下面的代码上(在FindLogicalAncestor上)收到"Requires unreachable"警告。
我在所有上使用-show unreached选项使用代码契约的项目。
如果我注释掉对FindVisualAncestor的调用,那么警告就会消失。
你能解释一个逻辑问题吗?代码,或者这是代码合同问题?
public static class Program
{
public static void GetActiveWindow(Window window,DependencyObject focusedElement)
{
if(focusedElement!= null)
{
window = focusedElement.FindVisualAncestor< Window>();
if(window == null)
{
window = focusedElement.FindLogicalAncestor< Window>();
}
}
}
public static T FindVisualAncestor< T>(此DependencyObject元素)其中T:class
{
return( T)FindVisualAncestor(元素,o => o是T);
}
公共静态对象FindVisualAncestor(此DependencyObject startElement,Predicate< object>条件)
{
返回条件;
}
public static T FindLogicalAncestor< T>(此DependencyObject startElement)
其中T:class
{
Contract.Requires(startElement!= null );
返回默认值(T); //
}
}
你好Brett,
你正在观察的行为应该是轻量级方法间推断的结果。
分析确定如果FindVisualAncestor(DependencyObject)成功,那么它返回一个非空值,所以它永远不会满足条件窗口== null,因此FindLogicalAncestor的前提条件永远不会被执行。
谢谢,
f
I don't understand why I get a 'Requires unreachable' warning on the following code (on FindLogicalAncestor).
I am using the -show unreached option on all projects using code contracts.
if I comment out the call to FindVisualAncestor then the warning goes away.
Are you able to explain a logic problem in the code, or is this a Code contracts issue?
public static class Program
{
public static void GetActiveWindow(Window window, DependencyObject focusedElement)
{
if (focusedElement != null)
{
window = focusedElement.FindVisualAncestor<Window>();
if (window == null)
{
window = focusedElement.FindLogicalAncestor<Window>();
}
}
}
public static T FindVisualAncestor<T>(this DependencyObject element) where T : class
{
return (T)FindVisualAncestor(element, o => o is T);
}
public static object FindVisualAncestor(this DependencyObject startElement, Predicate<object> condition)
{
return condition;
}
public static T FindLogicalAncestor<T>(this DependencyObject startElement)
where T : class
{
Contract.Requires(startElement != null);
return default(T);//
}
}
Hi Brett,
the behavior you are observing should be a consequence of the lightweight inter-method inference.
The analysis determines that if FindVisualAncestor(DependencyObject) succeedes, then it returns a non-null value, so it can never be the case that the condition window ==null is satisfied, and so the precondition of FindLogicalAncestor is never executed.
Thanks,
f
这篇关于需要无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!