<?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; Access private fields</title>
	<atom:link href="http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/</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: Kornner Studios (@kornnerstudios)</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-2327</link>
		<dc:creator><![CDATA[Kornner Studios (@kornnerstudios)]]></dc:creator>
		<pubDate>Wed, 21 Sep 2011 21:00:11 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-2327</guid>
		<description><![CDATA[Roger,

I&#039;ve created an article that builds on the ideas presented in this blog entry: http://www.codeproject.com/KB/recipes/LinqExpressionAccessors.aspx

I solved the issues of not being able to *set* a private member&#039;s value, and also cases where you need to set a value type&#039;s (struct) private member. Since you have to pass the value-type instance as a parameter (which then becomes a copy itself), you have to create a lambda which takes the &#039;this&#039; by-ref. Since you can&#039;t do that out of the box with the Func delegate, I created a specialized delegate for such accessor cases.

Thanks!]]></description>
		<content:encoded><![CDATA[<p>Roger,</p>
<p>I&#8217;ve created an article that builds on the ideas presented in this blog entry: <a href="http://www.codeproject.com/KB/recipes/LinqExpressionAccessors.aspx" rel="nofollow">http://www.codeproject.com/KB/recipes/LinqExpressionAccessors.aspx</a></p>
<p>I solved the issues of not being able to *set* a private member&#8217;s value, and also cases where you need to set a value type&#8217;s (struct) private member. Since you have to pass the value-type instance as a parameter (which then becomes a copy itself), you have to create a lambda which takes the &#8216;this&#8217; by-ref. Since you can&#8217;t do that out of the box with the Func delegate, I created a specialized delegate for such accessor cases.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Generic Method Invocation with Expression Trees &#171; Solutionizing .NET</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-991</link>
		<dc:creator><![CDATA[Generic Method Invocation with Expression Trees &#171; Solutionizing .NET]]></dc:creator>
		<pubDate>Tue, 20 Jan 2009 09:04:37 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-991</guid>
		<description><![CDATA[[...] Now we can dive into our expression trees. As a warm-up, here&#8217;s a relatively simple cached field accessor, inspired by Roger Alsing&#8217;s great post: [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Now we can dive into our expression trees. As a warm-up, here&#8217;s a relatively simple cached field accessor, inspired by Roger Alsing&#8217;s great post: [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fahad</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-918</link>
		<dc:creator><![CDATA[Fahad]]></dc:creator>
		<pubDate>Mon, 05 Jan 2009 05:43:30 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-918</guid>
		<description><![CDATA[Hi Roger,

I would like to know how to call an Expression on a ParameterExpression. I generated a GroupBy Expression thru this code,

&gt;&gt;
        public IQueryable GenerateGroupByExpression(string groupByName) {
            ParameterExpression paramExpression = Expression.Parameter(SourceType, SourceType.Name);
            MemberExpression memExp = Expression.PropertyOrField(paramExpression, groupByName);
            LambdaExpression lambda = Expression.Lambda(memExp, paramExpression);
            //.GroupBy()
            var groupedSource = this.Source.Provider.CreateQuery(Expression.Call(
                typeof(Queryable),
                &quot;GroupBy&quot;,
                new Type[] { 
                        SourceType, 
                        SourceType.GetProperty(groupByName).PropertyType
                    },
                Source.Expression,
                lambda
                )
             );
            //.Select()
            ParameterExpression groupparamExpression = Expression.Parameter(groupedSource.ElementType, &quot;g&quot;);
            List bindings = new List();
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&quot;Key&quot;)[0], Expression.PropertyOrField(groupparamExpression, &quot;Key&quot;)));
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&quot;Count&quot;)[0], Expression.Call(typeof(Queryable),
                &quot;Count&quot;,
                new Type[] { groupedSource.ElementType },
                new Expression[] { groupedSource.Expression })));
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&quot;Details&quot;)[0], groupparamExpression));
            Expression e = Expression.MemberInit(Expression.New(typeof(GroupContext)), bindings.ToArray());
            //g=&gt;prop.Propertyname
            LambdaExpression selectLambda = Expression.Lambda(e, groupparamExpression);
            return groupedSource.Provider.CreateQuery(Expression.Call(
                typeof(Queryable),
                &quot;Select&quot;,
                new Type[] { groupedSource.ElementType, typeof(GroupContext) },
                new Expression[] { groupedSource.Expression, selectLambda }
            ));
        }

&gt;&gt;

I have a GroupContext class with Key, Count and Details as parameters in it. Let me know your thoughts on this Dynamic query.

-Fahad]]></description>
		<content:encoded><![CDATA[<p>Hi Roger,</p>
<p>I would like to know how to call an Expression on a ParameterExpression. I generated a GroupBy Expression thru this code,</p>
<p>&gt;&gt;<br />
        public IQueryable GenerateGroupByExpression(string groupByName) {<br />
            ParameterExpression paramExpression = Expression.Parameter(SourceType, SourceType.Name);<br />
            MemberExpression memExp = Expression.PropertyOrField(paramExpression, groupByName);<br />
            LambdaExpression lambda = Expression.Lambda(memExp, paramExpression);<br />
            //.GroupBy()<br />
            var groupedSource = this.Source.Provider.CreateQuery(Expression.Call(<br />
                typeof(Queryable),<br />
                &#8220;GroupBy&#8221;,<br />
                new Type[] {<br />
                        SourceType,<br />
                        SourceType.GetProperty(groupByName).PropertyType<br />
                    },<br />
                Source.Expression,<br />
                lambda<br />
                )<br />
             );<br />
            //.Select()<br />
            ParameterExpression groupparamExpression = Expression.Parameter(groupedSource.ElementType, &#8220;g&#8221;);<br />
            List bindings = new List();<br />
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&#8220;Key&#8221;)[0], Expression.PropertyOrField(groupparamExpression, &#8220;Key&#8221;)));<br />
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&#8220;Count&#8221;)[0], Expression.Call(typeof(Queryable),<br />
                &#8220;Count&#8221;,<br />
                new Type[] { groupedSource.ElementType },<br />
                new Expression[] { groupedSource.Expression })));<br />
            bindings.Add(Expression.Bind(typeof(GroupContext).GetMember(&#8220;Details&#8221;)[0], groupparamExpression));<br />
            Expression e = Expression.MemberInit(Expression.New(typeof(GroupContext)), bindings.ToArray());<br />
            //g=&gt;prop.Propertyname<br />
            LambdaExpression selectLambda = Expression.Lambda(e, groupparamExpression);<br />
            return groupedSource.Provider.CreateQuery(Expression.Call(<br />
                typeof(Queryable),<br />
                &#8220;Select&#8221;,<br />
                new Type[] { groupedSource.ElementType, typeof(GroupContext) },<br />
                new Expression[] { groupedSource.Expression, selectLambda }<br />
            ));<br />
        }</p>
<p>&gt;&gt;</p>
<p>I have a GroupContext class with Key, Count and Details as parameters in it. Let me know your thoughts on this Dynamic query.</p>
<p>-Fahad</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Linq Expressions: Assign private fields (C# 4) &#171; Roger Alsing Weblog</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-294</link>
		<dc:creator><![CDATA[Linq Expressions: Assign private fields (C# 4) &#171; Roger Alsing Weblog]]></dc:creator>
		<pubDate>Mon, 03 Nov 2008 01:24:39 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-294</guid>
		<description><![CDATA[[...] my old post &#8220;Linq Expressions: Access private fields&#8221; is by far my most read blog entry, I figured that I have to throw you some more [...]]]></description>
		<content:encoded><![CDATA[<p>[...] my old post &#8220;Linq Expressions: Access private fields&#8221; is by far my most read blog entry, I figured that I have to throw you some more [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Gillson</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-274</link>
		<dc:creator><![CDATA[Mike Gillson]]></dc:creator>
		<pubDate>Wed, 01 Oct 2008 17:02:25 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-274</guid>
		<description><![CDATA[I was skeptical of your testing, so I tried it myself.  I measured 0.1 seconds for Lambda and 5.3 seconds for reflection.
However, I need to assign both fields and properties inside an automated data class.
If you ever determine how to do the assignments I would like to know.  My data class code is fast but if I ever have very large data sets, the reflection time could begin to show in my performance.]]></description>
		<content:encoded><![CDATA[<p>I was skeptical of your testing, so I tried it myself.  I measured 0.1 seconds for Lambda and 5.3 seconds for reflection.<br />
However, I need to assign both fields and properties inside an automated data class.<br />
If you ever determine how to do the assignments I would like to know.  My data class code is fast but if I ever have very large data sets, the reflection time could begin to show in my performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-101</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 18:40:24 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-101</guid>
		<description><![CDATA[&gt;&gt;Is it? I didn’t think it was; I’d love to see a sample of this?

I think I might have been a bit too fast on that conclusion.

Calling set properties is possible since you can make method calls and setters are simply a void method in IL.

But apparently its not possible to assign field values through expressions.

So my bad on that one. :-/
I did know that you cant use assignments in expressions in normal code, so I should have figured out that its not possible when building expressions either...
*equips donkey ears*]]></description>
		<content:encoded><![CDATA[<p>&gt;&gt;Is it? I didn’t think it was; I’d love to see a sample of this?</p>
<p>I think I might have been a bit too fast on that conclusion.</p>
<p>Calling set properties is possible since you can make method calls and setters are simply a void method in IL.</p>
<p>But apparently its not possible to assign field values through expressions.</p>
<p>So my bad on that one. :-/<br />
I did know that you cant use assignments in expressions in normal code, so I should have figured out that its not possible when building expressions either&#8230;<br />
*equips donkey ears*</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marc Gravell</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-99</link>
		<dc:creator><![CDATA[Marc Gravell]]></dc:creator>
		<pubDate>Thu, 28 Feb 2008 16:02:38 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-99</guid>
		<description><![CDATA[&gt; ok I didnt provide a sample on how to write data, but thats doable in the same way
Is it? I didn&#039;t think it was; I&#039;d love to see a sample of this?

Also: if the main objective is speed (rather than &quot;private&quot;), then another viable alternative [property based] is HyperDescriptor; fundamentally the same concept, but uses Reflection.Emit [it predates 3.5]; but has the advantage that it can &quot;set&quot;, and doesn&#039;t need any special coding (in fact, the caller doesn&#039;t even know you&#039;ve switched to a faster implementation, as long as they are using PropertyDescriptor to start with):

http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx]]></description>
		<content:encoded><![CDATA[<p>&gt; ok I didnt provide a sample on how to write data, but thats doable in the same way<br />
Is it? I didn&#8217;t think it was; I&#8217;d love to see a sample of this?</p>
<p>Also: if the main objective is speed (rather than &#8220;private&#8221;), then another viable alternative [property based] is HyperDescriptor; fundamentally the same concept, but uses Reflection.Emit [it predates 3.5]; but has the advantage that it can &#8220;set&#8221;, and doesn&#8217;t need any special coding (in fact, the caller doesn&#8217;t even know you&#8217;ve switched to a faster implementation, as long as they are using PropertyDescriptor to start with):</p>
<p><a href="http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx" rel="nofollow">http://www.codeproject.com/KB/cs/HyperPropertyDescriptor.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-84</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 14:50:23 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-84</guid>
		<description><![CDATA[@Rob

Well I guess you could simply use reflection to find the field names, and then use those names to create your fieldAccessors and store the fieldaccessors in a dictionary.

that way, you would only need to fetch the field names once per type and then you can use the fieldAccessors every time you need to read/write data.

(ok I didnt provide a sample on how to write data, but thats doable in the same way, you just need to alter the lambda generator slightly)]]></description>
		<content:encoded><![CDATA[<p>@Rob</p>
<p>Well I guess you could simply use reflection to find the field names, and then use those names to create your fieldAccessors and store the fieldaccessors in a dictionary.</p>
<p>that way, you would only need to fetch the field names once per type and then you can use the fieldAccessors every time you need to read/write data.</p>
<p>(ok I didnt provide a sample on how to write data, but thats doable in the same way, you just need to alter the lambda generator slightly)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rob</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-83</link>
		<dc:creator><![CDATA[Rob]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 14:44:23 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-83</guid>
		<description><![CDATA[This is really cool. I&#039;m currently using reflection to get ALL the private fields in a class and then loop over them.  Do you know if there is a similar approach that I could use with Linq that would provide the same speed increase?]]></description>
		<content:encoded><![CDATA[<p>This is really cool. I&#8217;m currently using reflection to get ALL the private fields in a class and then loop over them.  Do you know if there is a similar approach that I could use with Linq that would provide the same speed increase?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Roger Alsing</title>
		<link>http://rogeralsing.com/2008/02/26/linq-expressions-access-private-fields/#comment-76</link>
		<dc:creator><![CDATA[Roger Alsing]]></dc:creator>
		<pubDate>Tue, 26 Feb 2008 23:12:28 +0000</pubDate>
		<guid isPermaLink="false">http://rogeralsing.wordpress.com/?p=75#comment-76</guid>
		<description><![CDATA[Yes the compilation phase is quite slow, much slower than the reflection approach.

So if you intend to read a single field once, then reflection will be better.

But for any scenario where you are likely to access the same field on different objects, then the lambda approach is much better.
In such case you will most likely have to cache the field accessor in a dictionary or some other lookup structure.]]></description>
		<content:encoded><![CDATA[<p>Yes the compilation phase is quite slow, much slower than the reflection approach.</p>
<p>So if you intend to read a single field once, then reflection will be better.</p>
<p>But for any scenario where you are likely to access the same field on different objects, then the lambda approach is much better.<br />
In such case you will most likely have to cache the field accessor in a dictionary or some other lookup structure.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

