2

I have the Following Method used to walk the Visual Tree to find all objects of a Type:

    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj != null)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                if (child != null && child is T)
                {
                    yield return (T)child;
                }

                foreach (T childOfChild in FindVisualChildren<T>(child))
                {
                    yield return childOfChild;
                }
            }
        }
    }

The Problem is the Type is a string value stored in a veriable. Using the Above works fine when passed a Type like the following:

var x = FindVisualChildren<TextBox>(this);

However in my case TextBox is a string stored in a variable we will call item. So I want to do something like this:

var item = "TextBox";
var x = FindVisualChildren<item>(this);

But Item is not a type. So what is the best way to get the Type of the Sting Variable so it can be passed to my method. The Variable will be a TextBox, TextBlock, Grid, StackPanel, DockPanel, or TabControl. Right now I have everything in a Switch Statement and it is working but would like a cleaner way to do the same thing.

user990423
  • 1,397
  • 2
  • 12
  • 32
  • 4
    I believe that you have to use reflection for this: http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – syazdani Nov 03 '15 at 16:19
  • As @syazdani said. You need to get the type from the string using [Type.GetType](https://msdn.microsoft.com/en-us/library/w3f99sx1(v=vs.110).aspx) and from that you can follow Jon Skeet's [answer](http://stackoverflow.com/a/232621/1250301) using [MethodInfo.MakeGenericMethod](https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.makegenericmethod(v=vs.110).aspx) and then `Invoke` it. – Matt Burland Nov 03 '15 at 16:36
  • What is the end goal? You can't _use_ the result at compile-time since the compiler does not know what the type of the collection is. What are you going to _do_ with `x`? – D Stanley Nov 03 '15 at 16:40

1 Answers1

0

As noted in the comments, you need to use reflection.

First you need to obtain the Type that you want to use.

You can do it in two ways. You can create Type from a string that specifies that type name like this:

var type_name = "System.Windows.Controls.TextBox, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";

var type = Type.GetType(type_name, true);

Or you can define the type directly like this:

var type = typeof (TextBox);

Then you need to use reflection to obtain the MethodInfo like this:

var method = typeof (StaticClass).GetMethod("FindVisualChildren", BindingFlags.Static | BindingFlags.Public);

Where StaticClass is the name of the static class that contains the FindVisualChildren method.

Then you can invoke the method like this:

IEnumerable result = (IEnumerable)method.MakeGenericMethod(type).Invoke(null, new object[] { this});

Please note that I am casting to IEnumerable and not IEnumerable<T>.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62