<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Reference &#187; reflection</title>
	<atom:link href="http://sullivan.net/blog/tag/reflection/feed/" rel="self" type="application/rss+xml" />
	<link>http://sullivan.net/blog</link>
	<description>A collection of code for my reference (and perhaps other people too)</description>
	<lastBuildDate>Wed, 25 Jan 2012 17:53:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Find a child UIElement</title>
		<link>http://sullivan.net/blog/2009/08/find-a-child-uielement/</link>
		<comments>http://sullivan.net/blog/2009/08/find-a-child-uielement/#comments</comments>
		<pubDate>Thu, 20 Aug 2009 14:00:18 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=101</guid>
		<description><![CDATA[Here is a nice little method that will find the first occurance of child UIElement. XAML can get long and complex. This helped me find specific elements so I can set focus on them or do some other type of operation. Call the method like this UIElement result = FocusOnFirstElement(myLayoutRoot, typeof(Expander)); Here is the method: [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a nice little method that will find the first occurance of child UIElement.</p>
<p>XAML can get long and complex. This helped me find specific elements so I can set focus on them or do some other type of operation.</p>
<p>Call the method like this</p>
<pre class="brush: c#">
UIElement result = FocusOnFirstElement(myLayoutRoot, typeof(Expander));
</pre>
<p>Here is the method:</p>
<pre class="brush: c#">
/// &lt;summary&gt;
/// Find the first UIElement of a specific type within the specified root element.
/// UIElements in WPF have child elements under one of the following properties: Content, Child, Items, Children
/// This loops thru itself looking for any child elements.
/// &lt;/summary&gt;
/// &lt;param name=&quot;rootElement&quot;&gt;&lt;/param&gt;
/// &lt;param name=&quot;typeToFind&quot;&gt;&lt;/param&gt;
/// &lt;returns&gt;UIElement or null if the element was not found&lt;/returns&gt;
public UIElement FocusOnFirstElement(UIElement rootElement, Type typeToFind)
{
    bool found = false;
    UIElement currentElement = rootElement;

    var x1 = currentElement.GetType().GetProperty(&quot;Content&quot;);
    if (x1 != null)
    {
        var content = currentElement.GetType().GetProperty(&quot;Content&quot;).GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (content == null)
            return null;

        if (content.GetType() == typeToFind)
            return content as UIElement;

        return FocusOnFirstElement(content as UIElement, typeToFind);
    }

    var x2 = currentElement.GetType().GetProperty(&quot;Child&quot;);
    if (x2 != null)
    {
        var child = currentElement.GetType().GetProperty(&quot;Child&quot;).GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (child == null)
            return null;

        if (child.GetType() == typeToFind)
            return child as UIElement;

        return FocusOnFirstElement(child as UIElement, typeToFind);
    }

    var x4 = currentElement.GetType().GetProperty(&quot;Items&quot;);
    if (x4 != null)
    {
        ItemCollection items = (ItemCollection)currentElement.GetType().GetProperty(&quot;Items&quot;).GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (items == null)
            return null;

        foreach (var item in items)
        {
            if (item.GetType() == typeToFind)
                return item as UIElement;

            UIElement retunedElement = FocusOnFirstElement(item as UIElement, typeToFind);
            if (retunedElement != null)
                return retunedElement;
        }
    }

    var x5 = currentElement.GetType().GetProperty(&quot;Children&quot;);
    if (x5 != null)
    {
        UIElementCollection children = (UIElementCollection)currentElement.GetType().GetProperty(&quot;Children&quot;).GetValue(currentElement, BindingFlags.GetProperty, null, null, null);

        if (children == null)
            return null;

        foreach (var item in children)
        {
            if (item.GetType() == typeToFind)
                return item as UIElement;

            UIElement retunedElement = FocusOnFirstElement(item as UIElement, typeToFind);
            if (retunedElement != null)
                return retunedElement;
        }
    }

    return null;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/08/find-a-child-uielement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting a property from a generic object</title>
		<link>http://sullivan.net/blog/2009/07/getting-a-property-from-a-generic-object/</link>
		<comments>http://sullivan.net/blog/2009/07/getting-a-property-from-a-generic-object/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 08:58:10 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=57</guid>
		<description><![CDATA[If you have an object of an unknown type and you need a property from that object, you can easily get it using reflection. var value = myObject.GetType().GetProperty(&#34;Id&#34;).GetValue(myObject, BindingFlags.GetProperty, null, null, null); int id = Convert.ToInt32(value); You must know the name of the property you need: "Id" The object name appears twice - at the [...]]]></description>
			<content:encoded><![CDATA[<p>If you have an object of an unknown type and you need a property from that object, you can easily get it using reflection.</p>
<pre class="brush: c#">
var value = myObject.GetType().GetProperty(&quot;Id&quot;).GetValue(myObject, BindingFlags.GetProperty, null, null, null);
int id = Convert.ToInt32(value);
</pre>
<p>You must know the name of the property you need: "Id"<br />
The object name appears twice - at the beginning and the first element of GetValue()<br />
The BindingFlag is GetProperty because you are getting the property. duh!<br />
The last 3 items are most often null unless your object is more complicated (indexed properties, etc)</p>
<p>The result is an object so you must then cast it to the required type.</p>
<p>If you want the property type, it is even more simple:</p>
<pre class="brush: c#">
Type idType = myObject.GetType().GetProperty(&quot;Id&quot;).PropertyType;
</pre>
<p>Simple?</p>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/getting-a-property-from-a-generic-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling a generic method using reflection</title>
		<link>http://sullivan.net/blog/2009/07/18/</link>
		<comments>http://sullivan.net/blog/2009/07/18/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 06:59:05 +0000</pubDate>
		<dc:creator>Darren</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://sullivan.net/blog/?p=18</guid>
		<description><![CDATA[Because I often forget syntax I find it useful to keep examples on my blog for reference. This 1st example: Call a generic method using reflection The 2nd example: Call a generic method with a return value using reflection The 3rd example: Call a static generic method in another class with a return value using [...]]]></description>
			<content:encoded><![CDATA[<p>Because I often forget syntax I find it useful to keep examples on my blog for reference.</p>
<ul>
<li>This 1st example: Call a generic method using reflection</li>
</ul>
<ul>
<li> The 2nd example: Call a generic method with a return value using reflection</li>
</ul>
<ul>
<li> The 3rd example: Call a static generic method in another class with a return value using reflection</li>
</ul>
<ul>
<li> The 4th example: Call a static generic method with a return value using reflection</li>
</ul>
<ul>
<li> The 5th example: Calling a method with an OUT value using reflection (return value is not filled!!!!)</li>
</ul>
<pre class="brush: c#">
public void RunTest()
{
    Type type1 = typeof(string);
    Type type2 = typeof(int);

    // Example 1
    // Call a generic method using reflection
    MethodInfo m1 = this.GetType().GetMethod(&quot;Test1&quot;).MakeGenericMethod(new Type[] { type1, type2 });
    m1.Invoke(this, new object[] { &quot;test&quot;, 11 });

    // Example 2
    // Call a generic method with a return value using reflection
    MethodInfo m2 = this.GetType().GetMethod(&quot;Test2&quot;).MakeGenericMethod(new Type[] { type1, type2 });
    string return2 = (string)m2.Invoke(this, new object[] { &quot;test&quot;, 22 });

    // Example 3
    // Call a static generic method in another class with a return value using reflection
    MethodInfo m3 = typeof(Example3Class).GetMethod(&quot;Test3&quot;).MakeGenericMethod(new Type[] { type1, type2 });
    string return3 = (string)m3.Invoke(null, new object[] { &quot;test&quot;, 33 });

    // Example 4
    // Call a static generic method with a return value using reflection
    MethodInfo m4 = this.GetType().GetMethod(&quot;Test4&quot;).MakeGenericMethod(new Type[] { type1, type2 });
    string return4 = (string)m4.Invoke(null, new object[] { &quot;test&quot;, 44 });

    // Example 5
    // Calling a method with an OUT value using reflection
    // The trick here is to pass in an array of properties and access the &#039;out&#039; property of the array after the call.
    // This was not possible in the previous examples because I create a new object array in the invoke call.
    string outValue = string.Empty;
    object[] parameters = new object[] { &quot;test&quot;, 55, outValue }
    MethodInfo m5 = this.GetType().GetMethod(&quot;Test5&quot;).MakeGenericMethod(new Type[] { type1, type2 });
    string return5 = (string)m5.Invoke(this, parameters);

    // Now you can access the &#039;out&#039; parameter by accessing:
    outValue = (string)parameters[2];
}

// Example 1 method
public void Test1(string x, int y)
{ }

// Example 2 method
public string Test2(string x, int y)
{
    return &quot;return 2&quot;;
}

// Example 4 method
public static string Test4(string x, int y)
{
    return &quot;return 4&quot;;
}

// Example 5 method
public string Test5(string x, int y, out string z)
{
    z = &quot;out 5&quot;;
    return &quot;return 5&quot;;
}
</pre>
<p>The class for Example 3:</p>
<pre class="brush: c#">
public class Example3Class
{
    public Example3Class()
    {    }

    // Example 3 method
    public static string Test3(string x, int y)
    {
        return &quot;return 3&quot;;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://sullivan.net/blog/2009/07/18/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

