本文介绍了需要无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我在下面的代码上(在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); //
}

}

 

解决方案

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);//
		}

	}
解决方案


这篇关于需要无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:58