<?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: Late-Bound Invocations with DynamicMethod</title>
	<atom:link href="http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Wed, 22 May 2013 13:39: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: Edika</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1801</link>
		<dc:creator>Edika</dc:creator>
		<pubDate>Fri, 21 Aug 2009 13:41:23 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1801</guid>
		<description>OK understood ;)

Sorry for the double post....</description>
		<content:encoded><![CDATA[<p>OK understood <img src='http://lostechies.com/jimmybogard/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Sorry for the double post&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1800</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Fri, 21 Aug 2009 12:30:18 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1800</guid>
		<description>@Edika

I suppose it was my fault for not posting the profiling code...but the profiling here was specifically a tight loop around only the reflection calls, not any of the AutoMapper API.  I have done more profiling around the whole of AutoMapper, and the bottlenecks just aren&#039;t the reflection stuff.

The issue I run into is that many decisions on what to do are runtime and value (and not type) specific.  Some of these I&#039;m just stuck with.</description>
		<content:encoded><![CDATA[<p>@Edika</p>
<p>I suppose it was my fault for not posting the profiling code&#8230;but the profiling here was specifically a tight loop around only the reflection calls, not any of the AutoMapper API.  I have done more profiling around the whole of AutoMapper, and the bottlenecks just aren&#8217;t the reflection stuff.</p>
<p>The issue I run into is that many decisions on what to do are runtime and value (and not type) specific.  Some of these I&#8217;m just stuck with.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Edika</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1799</link>
		<dc:creator>Edika</dc:creator>
		<pubDate>Fri, 21 Aug 2009 10:26:19 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1799</guid>
		<description>Hi,
I don&#039;t know how AutoMapper works, but i&#039;ve test your sample with my custom ORM. After some test I&#039;ve notice my solution is much faster then this (in unit test 44 seconds Vs 81 seconds).

In  my ORM I&#039;ve created a cache to store the needed information about PropertyInfo and FieldInfo of each business class of my application.
When I need to set or get a value I simply used the stored PropertyInfo or FieldInfo retrieved from the cache to access or set the value. So it seems that the costs of the Reflection is not the SetValue or GetValue operation but only the GetPropertyInfo or GetFieldInfo.
When I query the cache to get the business class information, if the information are not in the cache, ones are retrieved using Reflection and stored in the cache.
In this way the Reflection are used only one time per business class.
If you want I can post my example test project.

Ciao</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I don&#8217;t know how AutoMapper works, but i&#8217;ve test your sample with my custom ORM. After some test I&#8217;ve notice my solution is much faster then this (in unit test 44 seconds Vs 81 seconds).</p>
<p>In  my ORM I&#8217;ve created a cache to store the needed information about PropertyInfo and FieldInfo of each business class of my application.<br />
When I need to set or get a value I simply used the stored PropertyInfo or FieldInfo retrieved from the cache to access or set the value. So it seems that the costs of the Reflection is not the SetValue or GetValue operation but only the GetPropertyInfo or GetFieldInfo.<br />
When I query the cache to get the business class information, if the information are not in the cache, ones are retrieved using Reflection and stored in the cache.<br />
In this way the Reflection are used only one time per business class.<br />
If you want I can post my example test project.</p>
<p>Ciao</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Edika</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1798</link>
		<dc:creator>Edika</dc:creator>
		<pubDate>Fri, 21 Aug 2009 10:25:25 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1798</guid>
		<description>Hi,
I don&#039;t know how AutoMapper works, but i&#039;ve test your sample with my custom ORM. After some test I&#039;ve notice my solution is much faster then this (in unit test 44 seconds Vs 81 seconds).

In  my ORM I&#039;ve created a cache to store the needed information about PropertyInfo and FieldInfo of each business class of my application.
When I need to set or get a value I simply used the stored PropertyInfo or FieldInfo retrieved from the cache to access or set the value. So it seems that the costs of the Reflection is not the SetValue or GetValue operation but only the GetPropertyInfo or GetFieldInfo.
When I query the cache to get the business class information, if the information are not in the cache, ones are retrieved using Reflection and stored in the cache.
In this way the Reflection are used only one time per business class.
If you want I can post my example test project.

Ciao</description>
		<content:encoded><![CDATA[<p>Hi,<br />
I don&#8217;t know how AutoMapper works, but i&#8217;ve test your sample with my custom ORM. After some test I&#8217;ve notice my solution is much faster then this (in unit test 44 seconds Vs 81 seconds).</p>
<p>In  my ORM I&#8217;ve created a cache to store the needed information about PropertyInfo and FieldInfo of each business class of my application.<br />
When I need to set or get a value I simply used the stored PropertyInfo or FieldInfo retrieved from the cache to access or set the value. So it seems that the costs of the Reflection is not the SetValue or GetValue operation but only the GetPropertyInfo or GetFieldInfo.<br />
When I query the cache to get the business class information, if the information are not in the cache, ones are retrieved using Reflection and stored in the cache.<br />
In this way the Reflection are used only one time per business class.<br />
If you want I can post my example test project.</p>
<p>Ciao</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artem Govorov</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1797</link>
		<dc:creator>Artem Govorov</dc:creator>
		<pubDate>Thu, 06 Aug 2009 23:34:49 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1797</guid>
		<description>@Jimmy,

I have done some testing: your solution is ~2 times faster on value types, very insignificantly faster on reference types. 

On the other hand, in my solution creation of LateBoundPropertySet is ~7 times faster comparing to yours. 

I guess for Automapper scenarios LateBoundPropertySet delegate usage performance is much more important than creation performance, so your solution is better for Automapper. Anyway, I suggested my alternative not so much from performance perspective, but just as an example how the same thing could be solved without IL generation (thus much simpler, easier to support and less error prone - though once written this piece of code doesn&#039;t really need much support).</description>
		<content:encoded><![CDATA[<p>@Jimmy,</p>
<p>I have done some testing: your solution is ~2 times faster on value types, very insignificantly faster on reference types. </p>
<p>On the other hand, in my solution creation of LateBoundPropertySet is ~7 times faster comparing to yours. </p>
<p>I guess for Automapper scenarios LateBoundPropertySet delegate usage performance is much more important than creation performance, so your solution is better for Automapper. Anyway, I suggested my alternative not so much from performance perspective, but just as an example how the same thing could be solved without IL generation (thus much simpler, easier to support and less error prone &#8211; though once written this piece of code doesn&#8217;t really need much support).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1796</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Thu, 06 Aug 2009 12:29:08 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1796</guid>
		<description>@Artem

Yeah, I tried Delegate.CreateDelegate, but found it to be slower.  Have you run any numbers on it?

@Mikael

The problem is that AutoMapper doesn&#039;t know about these types at compile time, only runtime.  All I have to work with is MemberInfo objects.  To AutoMapper, everything is an object.

@Michael

Thanks!  I think I still need to use unbox.any, as I have to cast the incoming type to the type of the setter.  Unbox.any either unboxes to the value type, or does castclass if it&#039;s a reference type, so it&#039;s a two-for-one deal.</description>
		<content:encoded><![CDATA[<p>@Artem</p>
<p>Yeah, I tried Delegate.CreateDelegate, but found it to be slower.  Have you run any numbers on it?</p>
<p>@Mikael</p>
<p>The problem is that AutoMapper doesn&#8217;t know about these types at compile time, only runtime.  All I have to work with is MemberInfo objects.  To AutoMapper, everything is an object.</p>
<p>@Michael</p>
<p>Thanks!  I think I still need to use unbox.any, as I have to cast the incoming type to the type of the setter.  Unbox.any either unboxes to the value type, or does castclass if it&#8217;s a reference type, so it&#8217;s a two-for-one deal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Sevestre</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1795</link>
		<dc:creator>Michael Sevestre</dc:creator>
		<pubDate>Thu, 06 Aug 2009 11:23:12 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1795</guid>
		<description>Thanks for sharing Jimmy,
Instead of doing the unboxing, you could use sthg like that:
gen.Emit(OpCodes.Ldarg_1);
UnboxIfNeeded(field.FieldType, gen);
gen.Emit(OpCodes.Stfld, field);

where UnboxIfNeeded is
private static void UnboxIfNeeded(Type type, ILGenerator generator)
    {
        if (type.IsValueType)
        {
            generator.Emit(OpCodes.Unbox_Any, type);
        }
    }

one &quot;if&quot; more but no unboxing if not needed

FYI:
I ran into some issue using some IL generation with DynamicMethod in the past when dealing with private field. 
So I tried to use your CreateSet(FieldInfo field) method this morning with a purely private field (type int, no setter/getter) to see how it would behave. I got following error 
&quot;operation could destabilize the runtime&quot;.... scary :-)</description>
		<content:encoded><![CDATA[<p>Thanks for sharing Jimmy,<br />
Instead of doing the unboxing, you could use sthg like that:<br />
gen.Emit(OpCodes.Ldarg_1);<br />
UnboxIfNeeded(field.FieldType, gen);<br />
gen.Emit(OpCodes.Stfld, field);</p>
<p>where UnboxIfNeeded is<br />
private static void UnboxIfNeeded(Type type, ILGenerator generator)<br />
    {<br />
        if (type.IsValueType)<br />
        {<br />
            generator.Emit(OpCodes.Unbox_Any, type);<br />
        }<br />
    }</p>
<p>one &#8220;if&#8221; more but no unboxing if not needed</p>
<p>FYI:<br />
I ran into some issue using some IL generation with DynamicMethod in the past when dealing with private field.<br />
So I tried to use your CreateSet(FieldInfo field) method this morning with a purely private field (type int, no setter/getter) to see how it would behave. I got following error<br />
&#8220;operation could destabilize the runtime&#8221;&#8230;. scary <img src='http://lostechies.com/jimmybogard/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mikael Svenson</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1794</link>
		<dc:creator>Mikael Svenson</dc:creator>
		<pubDate>Thu, 06 Aug 2009 07:40:51 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1794</guid>
		<description>You could generate several DynamicMethods which took the correct types in, instead of doing the boxing/unboxing. Then keep a list of them and invoke the proper one as you pass in source/dest types.

class DelegateFactory&lt;TTarget,TValue&gt;
...
DelegateFactory&lt;Source, int&gt;.LateBoundPropertySet callback = DelegateFactory&lt;Source, int&gt;.CreateSet(property);

It might be easier to generate those using CodeDomProvider instead as you don&#039;t have to type IL.

</description>
		<content:encoded><![CDATA[<p>You could generate several DynamicMethods which took the correct types in, instead of doing the boxing/unboxing. Then keep a list of them and invoke the proper one as you pass in source/dest types.</p>
<p>class DelegateFactory<ttarget ,TValue><br />
&#8230;<br />
DelegateFactory<source , int>.LateBoundPropertySet callback = DelegateFactory</source><source , int>.CreateSet(property);</p>
<p>It might be easier to generate those using CodeDomProvider instead as you don&#8217;t have to type IL.</p>
<p></source></ttarget></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artem Govorov</title>
		<link>http://lostechies.com/jimmybogard/2009/08/06/late-bound-invocations-with-dynamicmethod/#comment-1793</link>
		<dc:creator>Artem Govorov</dc:creator>
		<pubDate>Thu, 06 Aug 2009 03:24:40 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/08/05/late-bound-invocations-with-dynamicmethod.aspx#comment-1793</guid>
		<description>Hi Jimmy,

I had a similar task and here is how I solved it without IL:

       public delegate void LateBoundPropertySet(object target, object value);

        public static LateBoundPropertySet CreateSet(PropertyInfo property)
        {
            var setterType = typeof(Action&lt;,&gt;).MakeGenericType(property.DeclaringType, property.PropertyType);
            var propertyWriter = typeof(PropertyWriter&lt;,&gt;).MakeGenericType(property.DeclaringType, property.PropertyType);
            var setterDelegate = Delegate.CreateDelegate(setterType, property.GetSetMethod());
            var writer = (IPropertyWriter)Activator.CreateInstance(propertyWriter, setterDelegate);
            return writer.SetValue;
        }

        private interface IPropertyWriter
        {
            void SetValue(object instance, object value);
        }

        private class PropertyWriter&lt;TInstance, TProperty&gt; : IPropertyWriter
        {
            private readonly Action&lt;TInstance, TProperty&gt; _setValueDelegate;

            public PropertyWriter(Action&lt;TInstance, TProperty&gt; setValueDelegate)
            {
                _setValueDelegate = setValueDelegate;
            }

            public void SetValue(object instance, object value)
            {
                _setValueDelegate((TInstance)instance, (TProperty)value);
            }
        }</description>
		<content:encoded><![CDATA[<p>Hi Jimmy,</p>
<p>I had a similar task and here is how I solved it without IL:</p>
<p>       public delegate void LateBoundPropertySet(object target, object value);</p>
<p>        public static LateBoundPropertySet CreateSet(PropertyInfo property)<br />
        {<br />
            var setterType = typeof(Action< ,>).MakeGenericType(property.DeclaringType, property.PropertyType);<br />
            var propertyWriter = typeof(PropertyWriter< ,>).MakeGenericType(property.DeclaringType, property.PropertyType);<br />
            var setterDelegate = Delegate.CreateDelegate(setterType, property.GetSetMethod());<br />
            var writer = (IPropertyWriter)Activator.CreateInstance(propertyWriter, setterDelegate);<br />
            return writer.SetValue;<br />
        }</p>
<p>        private interface IPropertyWriter<br />
        {<br />
            void SetValue(object instance, object value);<br />
        }</p>
<p>        private class PropertyWriter<tinstance , TProperty> : IPropertyWriter<br />
        {<br />
            private readonly Action</tinstance><tinstance , TProperty> _setValueDelegate;</p>
<p>            public PropertyWriter(Action</tinstance><tinstance , TProperty> setValueDelegate)<br />
            {<br />
                _setValueDelegate = setValueDelegate;<br />
            }</p>
<p>            public void SetValue(object instance, object value)<br />
            {<br />
                _setValueDelegate((TInstance)instance, (TProperty)value);<br />
            }<br />
        }</tinstance></p>
]]></content:encoded>
	</item>
</channel>
</rss>
