<?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: How to avoid passing property names as strings using C# 3.0 Expression Trees</title>
	<atom:link href="http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/</link>
	<description></description>
	<lastBuildDate>Fri, 18 May 2012 20:58:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: knocte</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-30053</link>
		<dc:creator>knocte</dc:creator>
		<pubDate>Fri, 26 Nov 2010 13:56:00 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-30053</guid>
		<description>Is this different from this solution[1]?

If yes, is it better?

I wonder if you could include it in some helper library so we can use it more easily. How about Cadenza? (The former Mono.Rocks) Can you commit it there?

[1] http://blogs.microsoft.co.il/blogs/arik/archive/2010/11/17/no-more-magic-strings-presenting-string-of.aspx</description>
		<content:encoded><![CDATA[<p>Is this different from this solution[1]?</p>
<p>If yes, is it better?</p>
<p>I wonder if you could include it in some helper library so we can use it more easily. How about Cadenza? (The former Mono.Rocks) Can you commit it there?</p>
<p>[1] <a href="http://blogs.microsoft.co.il/blogs/arik/archive/2010/11/17/no-more-magic-strings-presenting-string-of.aspx" rel="nofollow">http://blogs.microsoft.co.il/blogs/arik/archive/2010/11/17/no-more-magic-strings-presenting-string-of.aspx</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: air jordan 24</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29937</link>
		<dc:creator>air jordan 24</dc:creator>
		<pubDate>Wed, 07 Jul 2010 10:46:27 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29937</guid>
		<description>Hhe article&#039;s content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as that worth you to see. Believe me these websites wont let you down. &lt;a href=&quot;http://www.2ued.com/&quot; rel=&quot;nofollow&quot;&gt;hip hop clothes&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Hhe article&#39;s content rich variety which make us move for our mood after reading this article. surprise, here you will find what you want! Recently, I found some wedsites which commodity is colorful of fashion. Such as that worth you to see. Believe me these websites wont let you down. <a href="http://www.2ued.com/" rel="nofollow">hip hop clothes</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mitchdcba</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29867</link>
		<dc:creator>mitchdcba</dc:creator>
		<pubDate>Wed, 28 Apr 2010 13:46:10 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29867</guid>
		<description>........</description>
		<content:encoded><![CDATA[<p>&#8230;&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan Zlatev</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29750</link>
		<dc:creator>Ivan Zlatev</dc:creator>
		<pubDate>Sun, 13 Dec 2009 18:22:05 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29750</guid>
		<description>You are right. I have updated the post with support for the Convert expression so that implicit and explicit casting are supported as well.</description>
		<content:encoded><![CDATA[<p>You are right. I have updated the post with support for the Convert expression so that implicit and explicit casting are supported as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: henon</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29749</link>
		<dc:creator>henon</dc:creator>
		<pubDate>Sun, 13 Dec 2009 17:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29749</guid>
		<description>I improved it to work with valuetype properties too (which isn&#039;t the case with yours because of boxing) and added unit tests. here is what I got:&lt;br&gt;&lt;br&gt;using System;&lt;br&gt;using System.Diagnostics;&lt;br&gt;using System.Linq.Expressions;&lt;br&gt;using System.Reflection;&lt;br&gt;&lt;br&gt;&lt;br&gt;public static class ExpressionsExtension&lt;br&gt;{&lt;br&gt;    /// &lt;summary&gt;&lt;br&gt;    /// Derives the name of a property from the given lambda expression and returns it as string.&lt;br&gt;    /// &lt;br&gt;    /// Example:  DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks) returns &quot;Ticks&quot;&lt;br&gt;    /// &lt;/summary&gt;&lt;br&gt;    /// &lt;param name=&quot;expression&quot;&gt;a lambda expression of the type x =&gt; x.PropertyName&lt;/param&gt;&lt;br&gt;    /// &lt;returns&gt;&lt;/returns&gt;&lt;br&gt;    public static string DerivePropertyNameFromExpression&lt;T, R&gt;(this T type, Expression&lt;Func&lt;T, R&gt;&gt; expression)&lt;br&gt;    {&lt;br&gt;        var property_ref_expr = expression.Body;&lt;br&gt;        Debug.Assert(property_ref_expr != null);&lt;br&gt;        var member_expr = property_ref_expr as MemberExpression;&lt;br&gt;        if (member_expr == null &#124;&#124; member_expr.Member.MemberType != MemberTypes.Property)&lt;br&gt;            throw new ArgumentException(&quot;No property reference expression was found.&quot;, &quot;expression&quot;);&lt;br&gt;        return member_expr.Member.Name;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;#if DEBUG&lt;br&gt;&lt;br&gt;namespace Test&lt;br&gt;{&lt;br&gt;    using NUnit.Framework;&lt;br&gt;&lt;br&gt;    [TestFixture]&lt;br&gt;    public class ExpressionExtensionTest&lt;br&gt;    {&lt;br&gt;        [Test]&lt;br&gt;        public void TestNameDerivationFromValueTypeProperty()&lt;br&gt;        {&lt;br&gt;            Assert.AreEqual(&quot;Ticks&quot;, DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks));&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        [Test]&lt;br&gt;        public void TestNameDerivationFromReferenceTypeProperty()&lt;br&gt;        {&lt;br&gt;            Assert.AreEqual(&quot;Parent&quot;, new ExampleClass().DerivePropertyNameFromExpression(s =&gt; s.Parent));&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        public class ExampleClass&lt;br&gt;        {&lt;br&gt;            public ExampleClass Parent { get; set; }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;#endif</description>
		<content:encoded><![CDATA[<p>I improved it to work with valuetype properties too (which isn&#39;t the case with yours because of boxing) and added unit tests. here is what I got:</p>
<p>using System;<br />using System.Diagnostics;<br />using System.Linq.Expressions;<br />using System.Reflection;</p>
<p>public static class ExpressionsExtension<br />{<br />    /// &lt;summary&gt;<br />    /// Derives the name of a property from the given lambda expression and returns it as string.<br />    /// <br />    /// Example:  DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks) returns &#8220;Ticks&#8221;<br />    /// &lt;/summary&gt;<br />    /// &lt;param name=&#8221;expression&#8221;&gt;a lambda expression of the type x =&gt; x.PropertyName&lt;/param&gt;<br />    /// &lt;returns&gt;&lt;/returns&gt;<br />    public static string DerivePropertyNameFromExpression&lt;T, R&gt;(this T type, Expression&lt;Func&lt;T, R&gt;&gt; expression)<br />    {<br />        var property_ref_expr = expression.Body;<br />        Debug.Assert(property_ref_expr != null);<br />        var member_expr = property_ref_expr as MemberExpression;<br />        if (member_expr == null || member_expr.Member.MemberType != MemberTypes.Property)<br />            throw new ArgumentException(&#8220;No property reference expression was found.&#8221;, &#8220;expression&#8221;);<br />        return member_expr.Member.Name;<br />    }<br />}</p>
<p>#if DEBUG</p>
<p>namespace Test<br />{<br />    using NUnit.Framework;</p>
<p>    [TestFixture]<br />    public class ExpressionExtensionTest<br />    {<br />        [Test]<br />        public void TestNameDerivationFromValueTypeProperty()<br />        {<br />            Assert.AreEqual(&#8220;Ticks&#8221;, DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks));<br />        }</p>
<p>        [Test]<br />        public void TestNameDerivationFromReferenceTypeProperty()<br />        {<br />            Assert.AreEqual(&#8220;Parent&#8221;, new ExampleClass().DerivePropertyNameFromExpression(s =&gt; s.Parent));<br />        }</p>
<p>        public class ExampleClass<br />        {<br />            public ExampleClass Parent { get; set; }<br />        }<br />    }<br />}</p>
<p>#endif</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan Zlatev</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29733</link>
		<dc:creator>Ivan Zlatev</dc:creator>
		<pubDate>Sun, 13 Dec 2009 10:22:05 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29733</guid>
		<description>You are right. I have updated the post with support for the Convert expression so that implicit and explicit casting are supported as well.</description>
		<content:encoded><![CDATA[<p>You are right. I have updated the post with support for the Convert expression so that implicit and explicit casting are supported as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: henon</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29732</link>
		<dc:creator>henon</dc:creator>
		<pubDate>Sun, 13 Dec 2009 09:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29732</guid>
		<description>I improved it to work with valuetype properties too (which isn&#039;t the case with yours because of boxing) and added unit tests. here is what I got:&lt;br&gt;&lt;br&gt;using System;&lt;br&gt;using System.Diagnostics;&lt;br&gt;using System.Linq.Expressions;&lt;br&gt;using System.Reflection;&lt;br&gt;&lt;br&gt;&lt;br&gt;public static class ExpressionsExtension&lt;br&gt;{&lt;br&gt;    /// &lt;summary&gt;&lt;br&gt;    /// Derives the name of a property from the given lambda expression and returns it as string.&lt;br&gt;    /// &lt;br&gt;    /// Example:  DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks) returns &quot;Ticks&quot;&lt;br&gt;    /// &lt;/summary&gt;&lt;br&gt;    /// &lt;param name=&quot;expression&quot;&gt;a lambda expression of the type x =&gt; x.PropertyName&lt;/param&gt;&lt;br&gt;    /// &lt;returns&gt;&lt;/returns&gt;&lt;br&gt;    public static string DerivePropertyNameFromExpression&lt;T, R&gt;(this T type, Expression&lt;Func&lt;T, R&gt;&gt; expression)&lt;br&gt;    {&lt;br&gt;        var property_ref_expr = expression.Body;&lt;br&gt;        Debug.Assert(property_ref_expr != null);&lt;br&gt;        var member_expr = property_ref_expr as MemberExpression;&lt;br&gt;        if (member_expr == null &#124;&#124; member_expr.Member.MemberType != MemberTypes.Property)&lt;br&gt;            throw new ArgumentException(&quot;No property reference expression was found.&quot;, &quot;expression&quot;);&lt;br&gt;        return member_expr.Member.Name;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;br&gt;#if DEBUG&lt;br&gt;&lt;br&gt;namespace Test&lt;br&gt;{&lt;br&gt;    using NUnit.Framework;&lt;br&gt;&lt;br&gt;    [TestFixture]&lt;br&gt;    public class ExpressionExtensionTest&lt;br&gt;    {&lt;br&gt;        [Test]&lt;br&gt;        public void TestNameDerivationFromValueTypeProperty()&lt;br&gt;        {&lt;br&gt;            Assert.AreEqual(&quot;Ticks&quot;, DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks));&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        [Test]&lt;br&gt;        public void TestNameDerivationFromReferenceTypeProperty()&lt;br&gt;        {&lt;br&gt;            Assert.AreEqual(&quot;Parent&quot;, new ExampleClass().DerivePropertyNameFromExpression(s =&gt; s.Parent));&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        public class ExampleClass&lt;br&gt;        {&lt;br&gt;            public ExampleClass Parent { get; set; }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;#endif</description>
		<content:encoded><![CDATA[<p>I improved it to work with valuetype properties too (which isn&#39;t the case with yours because of boxing) and added unit tests. here is what I got:</p>
<p>using System;<br />using System.Diagnostics;<br />using System.Linq.Expressions;<br />using System.Reflection;</p>
<p>public static class ExpressionsExtension<br />{<br />    /// &lt;summary&gt;<br />    /// Derives the name of a property from the given lambda expression and returns it as string.<br />    /// <br />    /// Example:  DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks) returns &#8220;Ticks&#8221;<br />    /// &lt;/summary&gt;<br />    /// &lt;param name=&#8221;expression&#8221;&gt;a lambda expression of the type x =&gt; x.PropertyName&lt;/param&gt;<br />    /// &lt;returns&gt;&lt;/returns&gt;<br />    public static string DerivePropertyNameFromExpression&lt;T, R&gt;(this T type, Expression&lt;Func&lt;T, R&gt;&gt; expression)<br />    {<br />        var property_ref_expr = expression.Body;<br />        Debug.Assert(property_ref_expr != null);<br />        var member_expr = property_ref_expr as MemberExpression;<br />        if (member_expr == null || member_expr.Member.MemberType != MemberTypes.Property)<br />            throw new ArgumentException(&#8220;No property reference expression was found.&#8221;, &#8220;expression&#8221;);<br />        return member_expr.Member.Name;<br />    }<br />}</p>
<p>#if DEBUG</p>
<p>namespace Test<br />{<br />    using NUnit.Framework;</p>
<p>    [TestFixture]<br />    public class ExpressionExtensionTest<br />    {<br />        [Test]<br />        public void TestNameDerivationFromValueTypeProperty()<br />        {<br />            Assert.AreEqual(&#8220;Ticks&#8221;, DateTime.Now.DerivePropertyNameFromExpression(s =&gt; s.Ticks));<br />        }</p>
<p>        [Test]<br />        public void TestNameDerivationFromReferenceTypeProperty()<br />        {<br />            Assert.AreEqual(&#8220;Parent&#8221;, new ExampleClass().DerivePropertyNameFromExpression(s =&gt; s.Parent));<br />        }</p>
<p>        public class ExampleClass<br />        {<br />            public ExampleClass Parent { get; set; }<br />        }<br />    }<br />}</p>
<p>#endif</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Asbjørn Ulsberg</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29730</link>
		<dc:creator>Asbjørn Ulsberg</dc:creator>
		<pubDate>Mon, 07 Dec 2009 07:36:46 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29730</guid>
		<description>C# 3.0 Expression Trees are brilliant. Fluent NHibernate is entirely built around them, yielding extremely terse and readable code that&#039;s a walk in the park to refactor. Excellent stuff! :)</description>
		<content:encoded><![CDATA[<p>C# 3.0 Expression Trees are brilliant. Fluent NHibernate is entirely built around them, yielding extremely terse and readable code that&#39;s a walk in the park to refactor. Excellent stuff! <img src='http://ivanz.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: migueldeicaza</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29728</link>
		<dc:creator>migueldeicaza</dc:creator>
		<pubDate>Sat, 05 Dec 2009 14:17:48 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29728</guid>
		<description>This is a brilliant hack.</description>
		<content:encoded><![CDATA[<p>This is a brilliant hack.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alan</title>
		<link>http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/comment-page-1/#comment-29727</link>
		<dc:creator>Alan</dc:creator>
		<pubDate>Fri, 04 Dec 2009 21:22:11 +0000</pubDate>
		<guid isPermaLink="false">http://ivanz.com/?p=699#comment-29727</guid>
		<description>You could probably avoid some of the performance pain (if it actually turns out to be a bottleneck) with some clever caching of the expression tree and/or its result. Lazy &lt;T&gt; sounds like the ideal candidate here - you&#039;ll compute your string name once and then be able to re-use it for free as many times as you want.</description>
		<content:encoded><![CDATA[<p>You could probably avoid some of the performance pain (if it actually turns out to be a bottleneck) with some clever caching of the expression tree and/or its result. Lazy &lt;T&gt; sounds like the ideal candidate here &#8211; you&#39;ll compute your string name once and then be able to re-use it for free as many times as you want.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

