<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Getting stuck in the weeds</title>
	<atom:link href="http://lostechies.com/jimmybogard/2009/08/17/getting-stuck-in-the-weeds/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2009/08/17/getting-stuck-in-the-weeds/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Sat, 25 May 2013 16:53:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Eric Todd</title>
		<link>http://lostechies.com/jimmybogard/2009/08/17/getting-stuck-in-the-weeds/#comment-1821</link>
		<dc:creator>Eric Todd</dc:creator>
		<pubDate>Wed, 26 Aug 2009 04:11:10 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/16/getting-stuck-in-the-weeds.aspx#comment-1821</guid>
		<description>No samples seem to demonstrate using DynamicMethod with value types.
I think this should solve your problem.  I&#039;ve tried to extract this snippet from my codebase in the simplest way possible.  I do a lot of dynamic reflection and ran into this problem a while back.

var dynamicMethod = GetDynamicMethod(); // Pick your own implementation
var generator = dynamicMethod.GetILGenerator();
var method = property.GetSetMethod(true);

if(method != null)
{

	if(!method.IsStatic)
	{
		generator.LoadArgument(0);

		if(method.DeclaringType.IsValueType)
		{
			generator.Emit(OpCodes.Unbox,method.DeclaringType);
		}
		generator.LoadArgument(1);
	}
	else
	{
		generator.LoadArgument(0);
	}

	if(method.DeclaringType.IsValueType)
	{
		generator.Emit(OpCodes.Unbox_Any,method.DeclaringType);
	}

	if((method.CallingConvention &amp; CallingConventions.VarArgs) != 0)
	{
		if(method.IsFinal &#124;&#124; !method.IsVirtual)
		{
			generator.EmitCall(OpCodes.Call,method,null);
		}
		else
		{
			generator.EmitCall(OpCodes.Callvirt,method,null);
		}
	}
	else
	{
		if(method.IsFinal &#124;&#124; !method.IsVirtual)
		{
			generator.Emit(OpCodes.Call,method);
		}
		else
		{
			generator.Emit(OpCodes.Callvirt,method);
		}
	}
	generator.Emit(OpCodes.Ret);

}</description>
		<content:encoded><![CDATA[<p>No samples seem to demonstrate using DynamicMethod with value types.<br />
I think this should solve your problem.  I&#8217;ve tried to extract this snippet from my codebase in the simplest way possible.  I do a lot of dynamic reflection and ran into this problem a while back.</p>
<p>var dynamicMethod = GetDynamicMethod(); // Pick your own implementation<br />
var generator = dynamicMethod.GetILGenerator();<br />
var method = property.GetSetMethod(true);</p>
<p>if(method != null)<br />
{</p>
<p>	if(!method.IsStatic)<br />
	{<br />
		generator.LoadArgument(0);</p>
<p>		if(method.DeclaringType.IsValueType)<br />
		{<br />
			generator.Emit(OpCodes.Unbox,method.DeclaringType);<br />
		}<br />
		generator.LoadArgument(1);<br />
	}<br />
	else<br />
	{<br />
		generator.LoadArgument(0);<br />
	}</p>
<p>	if(method.DeclaringType.IsValueType)<br />
	{<br />
		generator.Emit(OpCodes.Unbox_Any,method.DeclaringType);<br />
	}</p>
<p>	if((method.CallingConvention &#038; CallingConventions.VarArgs) != 0)<br />
	{<br />
		if(method.IsFinal || !method.IsVirtual)<br />
		{<br />
			generator.EmitCall(OpCodes.Call,method,null);<br />
		}<br />
		else<br />
		{<br />
			generator.EmitCall(OpCodes.Callvirt,method,null);<br />
		}<br />
	}<br />
	else<br />
	{<br />
		if(method.IsFinal || !method.IsVirtual)<br />
		{<br />
			generator.Emit(OpCodes.Call,method);<br />
		}<br />
		else<br />
		{<br />
			generator.Emit(OpCodes.Callvirt,method);<br />
		}<br />
	}<br />
	generator.Emit(OpCodes.Ret);</p>
<p>}</p>
]]></content:encoded>
	</item>
</channel>
</rss>
