Tuesday, May 10, 2011

C# – Generics and the Where Clause

public static T FindVisualParent<T>(DependencyObject obj)
where T : DependencyObject
{
if (obj == null)
return null;
T correctlyTyped = obj as T;
if (obj != null)
return correctlyTyped;
if (obj is Visual)
{
obj = VisualTreeHelper.GetParent(obj);
}
else
{
FrameworkContentElement fce = obj as FrameworkContentElement;
if (fce != null)
obj = fce.Parent;
else
throw new ArgumentException(
“Cannot Walk Parent Tree of ” + obj.GetType().ToString());
}
return FindVisualParent<T>(obj);
}

No comments:

Post a Comment