There is such code:

public static FrameworkElement GetParentOfType<T>(FrameworkElement element) { if (element is T) return element; while (true) { element = element.Parent as FrameworkElement; if (element is T) break; } return element; } ... MyPanel myPanel = (MyPanel)GetParentOfType<MyPanel>(myTextBox); MyLabel myLabel = (MyLabel)GetParentOfType<MyLabel>(myTextBox); ... 

But I would like to do something like this:

 public static T GetParentOfType<T>(FrameworkElement element) { if (element is T) return element as T; while (true) { element = element.Parent as FrameworkElement; if (element is T) break; } return element as T; } ... MyPanel myPanel = GetParentOfType<MyPanel>(myTextBox); MyLabel myLabel = GetParentOfType<MyLabel>(myTextBox); ... 

But as I would like it does not work. Where to read about it or how to be? I tried to look for myself, but I don’t know how to formulate a request correctly.

  • How exactly does not work, the exception or not compiled? - Maxim Kamalov
  • The code seems to be correct, explain what the problem is. (I would still write while (element != null) : < ideone.com/wwUNaj>. ) - VladD
  • The second example is not compiled. Regarding the correctness of the exit from the cycle, it is actually written differently. Just cut the code to identify the problem of interest. Try it yourself, not compiled. - krupennikov

2 answers 2

It is necessary here as:

 public static T GetParentOfType<T>(FrameworkElement element) where T : FrameworkElement { if (element is T) return element as T; while (element != null) { element = element.Parent as FrameworkElement; if (element is T) break; } return element as T; } 
  • one
    And then the meaning of using generics? if the output type is limited to a specific class, then it is easier to specify it as the type of the output parameter. - Carma
  • one
    @Carma: where T : FrameworkElement means that T is a class derived from FrameworkElement , just what is required in the question. - VladD
  • one
    If the FrameworkElement is derived from T (and not vice versa), the code will not compile. The difference from your example is in the return type: since we are sure that the type will not be just a FrameworkElement , but even T , you can make a downcast so that users of our method do not have to do this. - VladD pm
  • one
    Well, firstly, we must somehow convey the desired type. Secondly, it is an eponymous caste: in your case, you need Button b = (Button)GetParentOfType(...) , in my Button b = GetParentOfType(...) - VladD
  • one
    @Carma: not really, in your case Button b = GetParentOfType<Button>(...) will not compile. Because FrameworkElement is a less specific type. Try it yourself, it's easy to check. - VladD

Try something like that

 public T ConvertTo<T>(SomeType val) { return (T)Convert.ChangeType(val, Type.GetTypeCode(typeof(T))); } 

Those. use forced type conversion instead of as operator

  • Something like this will work with C ++ templates, but not with the Detney generics, because during compilation there is no information about the type T. - nitrocaster
  • Corrected. Thank. - Carma