<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Linq Expressions &#8211; Creating objects</title>
	<atom:link href="http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 14:35:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Improving Performance &#187; Glass</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-2320</link>
		<dc:creator><![CDATA[Improving Performance &#187; Glass]]></dc:creator>
		<pubDate>Sat, 17 Sep 2011 15:10:14 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-2320</guid>
		<description><![CDATA[[...] is based on this post by Roger Alsing.  Dynamic [...]]]></description>
		<content:encoded><![CDATA[<p>[...] is based on this post by Roger Alsing.  Dynamic [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jimmi</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-2287</link>
		<dc:creator><![CDATA[Jimmi]]></dc:creator>
		<pubDate>Tue, 23 Aug 2011 12:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-2287</guid>
		<description><![CDATA[Really awesome]]></description>
		<content:encoded><![CDATA[<p>Really awesome</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jegatheeswaran M</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-2113</link>
		<dc:creator><![CDATA[Jegatheeswaran M]]></dc:creator>
		<pubDate>Thu, 24 Mar 2011 11:19:38 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-2113</guid>
		<description><![CDATA[In the following line

//create an instance:
Created instance = createdActivator (123, &quot;Roger&quot;);

&lt;b&gt;createdActivator&lt;/b&gt; doesn&#039;t allow null values. How to make this method to accept null values too?

When used the following line of code
&lt;b&gt;Created instance = createdActivator(null, &quot;Jegan&quot;);&lt;/b&gt;
It is throwing object reference error.

Can you please help me to resolve this?]]></description>
		<content:encoded><![CDATA[<p>In the following line</p>
<p>//create an instance:<br />
Created instance = createdActivator (123, &#8220;Roger&#8221;);</p>
<p><b>createdActivator</b> doesn&#8217;t allow null values. How to make this method to accept null values too?</p>
<p>When used the following line of code<br />
<b>Created instance = createdActivator(null, &#8220;Jegan&#8221;);</b><br />
It is throwing object reference error.</p>
<p>Can you please help me to resolve this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Don’t use Activator.CreateInstance or ConstructorInfo.Invoke, use compiled lambda expressions - Vagif Abilov&#39;s blog on .NET</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-1588</link>
		<dc:creator><![CDATA[Don’t use Activator.CreateInstance or ConstructorInfo.Invoke, use compiled lambda expressions - Vagif Abilov&#39;s blog on .NET]]></dc:creator>
		<pubDate>Fri, 02 Apr 2010 15:44:02 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-1588</guid>
		<description><![CDATA[[...] Alsing in this post presented a generic method that can be used as a replacement for constructor method invocation. The [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Alsing in this post presented a generic method that can be used as a replacement for constructor method invocation. The [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gustavo Guerra</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-100</link>
		<dc:creator><![CDATA[Gustavo Guerra]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 17:14:37 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-100</guid>
		<description><![CDATA[Your approach is only slightly slower than generating the il

With 1000000 iterations I get this:
constructor.Invoke() -&gt; 01.7241s
lambda expression    -&gt; 00.0186s
dynamicmethod il     -&gt; 00.0154s

What&#039;s curious is that if you lower that number to 10000, the lambda expression is faster
constructor.Invoke() -&gt; 00.01738s
lambda expression    -&gt; 00.00026s
dynamicmethod il     -&gt; 00.00044s

Here&#039;s the code:

using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;

public interface IXPTO {
}

public class XPTO : IXPTO {
	public XPTO() {}
}

public class BenchMark {
	
	public static void Main() {
		
		ConstructorInfo constructor = typeof(XPTO).MakeGenericType(typeof(int)).GetConstructor(Type.EmptyTypes);
		Func&lt;IXPTO&gt; f1 = () =&gt; (IXPTO)constructor.Invoke(new object[0]);
		Func&lt;IXPTO&gt; f2 = (Func&lt;IXPTO&gt;)Expression.Lambda(Expression.Convert(Expression.New(constructor), typeof(IXPTO))).Compile();		
		DynamicMethod method = new DynamicMethod(&quot;CreateIntance&quot;, typeof(IXPTO), Type.EmptyTypes);
		ILGenerator gen = method.GetILGenerator();
		gen.Emit(OpCodes.Newobj, constructor);
		gen.Emit(OpCodes.Ret);
		Func&lt;IXPTO&gt; f3 = (Func&lt;IXPTO&gt;)method.CreateDelegate(typeof(Func&lt;IXPTO&gt;));                
		
		int iterations = 1000000;
		
		Stopwatch watch = Stopwatch.StartNew();
		for (int i = 0; i &lt; iterations; ++i) {
			f1();
		}
		Console.WriteLine(watch.Elapsed);
		watch = Stopwatch.StartNew();
		for (int i = 0; i &lt; iterations; ++i) {
			f2();
		}		
		Console.WriteLine(watch.Elapsed);
		watch = Stopwatch.StartNew();
		for (int i = 0; i &lt; iterations; ++i) {
			f3();
		}		
		Console.WriteLine(watch.Elapsed);
		Console.ReadLine();
	}	
}]]></description>
		<content:encoded><![CDATA[<p>Your approach is only slightly slower than generating the il</p>
<p>With 1000000 iterations I get this:<br />
constructor.Invoke() -&gt; 01.7241s<br />
lambda expression    -&gt; 00.0186s<br />
dynamicmethod il     -&gt; 00.0154s</p>
<p>What&#8217;s curious is that if you lower that number to 10000, the lambda expression is faster<br />
constructor.Invoke() -&gt; 00.01738s<br />
lambda expression    -&gt; 00.00026s<br />
dynamicmethod il     -&gt; 00.00044s</p>
<p>Here&#8217;s the code:</p>
<p>using System;<br />
using System.Diagnostics;<br />
using System.Linq.Expressions;<br />
using System.Reflection;<br />
using System.Reflection.Emit;</p>
<p>public interface IXPTO {<br />
}</p>
<p>public class XPTO : IXPTO {<br />
	public XPTO() {}<br />
}</p>
<p>public class BenchMark {</p>
<p>	public static void Main() {</p>
<p>		ConstructorInfo constructor = typeof(XPTO).MakeGenericType(typeof(int)).GetConstructor(Type.EmptyTypes);<br />
		Func&lt;IXPTO&gt; f1 = () =&gt; (IXPTO)constructor.Invoke(new object[0]);<br />
		Func&lt;IXPTO&gt; f2 = (Func&lt;IXPTO&gt;)Expression.Lambda(Expression.Convert(Expression.New(constructor), typeof(IXPTO))).Compile();<br />
		DynamicMethod method = new DynamicMethod(&#8220;CreateIntance&#8221;, typeof(IXPTO), Type.EmptyTypes);<br />
		ILGenerator gen = method.GetILGenerator();<br />
		gen.Emit(OpCodes.Newobj, constructor);<br />
		gen.Emit(OpCodes.Ret);<br />
		Func&lt;IXPTO&gt; f3 = (Func&lt;IXPTO&gt;)method.CreateDelegate(typeof(Func&lt;IXPTO&gt;));                </p>
<p>		int iterations = 1000000;</p>
<p>		Stopwatch watch = Stopwatch.StartNew();<br />
		for (int i = 0; i &lt; iterations; ++i) {<br />
			f1();<br />
		}<br />
		Console.WriteLine(watch.Elapsed);<br />
		watch = Stopwatch.StartNew();<br />
		for (int i = 0; i &lt; iterations; ++i) {<br />
			f2();<br />
		}<br />
		Console.WriteLine(watch.Elapsed);<br />
		watch = Stopwatch.StartNew();<br />
		for (int i = 0; i &lt; iterations; ++i) {<br />
			f3();<br />
		}<br />
		Console.WriteLine(watch.Elapsed);<br />
		Console.ReadLine();<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-97</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 15:56:50 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-97</guid>
		<description><![CDATA[Yes, I&#039;ve had my fair share of IL emit when I wrote NAspect (subclass proxy based AOP framework for .NET)

I had to use reflector and dissassemble code all the time to get my emits right.
Especially the pain with types that need boxing etc.
&quot;Should I box now or is it already boxed?&quot;

That kind of stuff was a real PITA.]]></description>
		<content:encoded><![CDATA[<p>Yes, I&#8217;ve had my fair share of IL emit when I wrote NAspect (subclass proxy based AOP framework for .NET)</p>
<p>I had to use reflector and dissassemble code all the time to get my emits right.<br />
Especially the pain with types that need boxing etc.<br />
&#8220;Should I box now or is it already boxed?&#8221;</p>
<p>That kind of stuff was a real PITA.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Gravell</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-96</link>
		<dc:creator><![CDATA[Marc Gravell]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 15:56:38 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-96</guid>
		<description><![CDATA[&gt; Btw. you forgot to add field readers in your lib

Not sure if that was a suggestion or a plug; if the former, we simply didn&#039;t need them (besides, I&#039;m not a big fan of too much knowledge of fields outside the declaring class).

Re setters... obviously Expression doesn&#039;t support this; but if you *need* fast &quot;set&quot; access to fields, you might consider some variant of HyperDescriptor:
http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx
This also has the advantage of working on 2.0; at the moment it targets properties, but as the metrics show, it doesn&#039;t exactly suffer for this...]]></description>
		<content:encoded><![CDATA[<p>&gt; Btw. you forgot to add field readers in your lib</p>
<p>Not sure if that was a suggestion or a plug; if the former, we simply didn&#8217;t need them (besides, I&#8217;m not a big fan of too much knowledge of fields outside the declaring class).</p>
<p>Re setters&#8230; obviously Expression doesn&#8217;t support this; but if you *need* fast &#8220;set&#8221; access to fields, you might consider some variant of HyperDescriptor:<br />
<a href="http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx</a><br />
This also has the advantage of working on 2.0; at the moment it targets properties, but as the metrics show, it doesn&#8217;t exactly suffer for this&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Gravell</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-95</link>
		<dc:creator><![CDATA[Marc Gravell]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 15:47:04 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-95</guid>
		<description><![CDATA[I&#039;m of the view that we are making good and appropriate use of some under-advertised functionality. If you&#039;ve ever written IL, then you&#039;ll know how much of a gift this is by comparison! My only gripe is that you can&#039;t use property-setters (except for in initializers) - a shame, but I can appreciate why. Still; useful anyway - C# goes &quot;meta&quot;.]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m of the view that we are making good and appropriate use of some under-advertised functionality. If you&#8217;ve ever written IL, then you&#8217;ll know how much of a gift this is by comparison! My only gripe is that you can&#8217;t use property-setters (except for in initializers) &#8211; a shame, but I can appreciate why. Still; useful anyway &#8211; C# goes &#8220;meta&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-93</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 14:33:33 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-93</guid>
		<description><![CDATA[I tend to use and abuse new features when I find them.
Its the best way to learn new techniques, atleast for me.

And atleast this code wasnt _that_ similair.
The generic operator code was more scary :-)

Btw. you forgot to add field readers in your lib ;-)
http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/]]></description>
		<content:encoded><![CDATA[<p>I tend to use and abuse new features when I find them.<br />
Its the best way to learn new techniques, atleast for me.</p>
<p>And atleast this code wasnt _that_ similair.<br />
The generic operator code was more scary :-)</p>
<p>Btw. you forgot to add field readers in your lib ;-)<br />
<a href="http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/" rel="nofollow">http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Gravell</title>
		<link>http://rogeralsing.com/2008/02/28/linq-expressions-creating-objects/#comment-92</link>
		<dc:creator><![CDATA[Marc Gravell]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 14:05:14 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=77#comment-92</guid>
		<description><![CDATA[I&#039;m going to upset you again... MiscUtil; there are some extension methods on Type (in &quot;TypeExt&quot;) that might look familiar...

But good to see that I&#039;m not the only person crazy enough to be using Expressions in this way!]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m going to upset you again&#8230; MiscUtil; there are some extension methods on Type (in &#8220;TypeExt&#8221;) that might look familiar&#8230;</p>
<p>But good to see that I&#8217;m not the only person crazy enough to be using Expressions in this way!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

