<?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; generics</title>
	<atom:link href="http://sullivan.net/blog/tag/generics/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>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>

