<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Development</title>
        <link>http://geekswithblogs.net/Madman/category/6726.aspx</link>
        <description>Posts related to programming and development</description>
        <language>en-US</language>
        <copyright>Darren Kopp</copyright>
        <managingEditor>darrenkopp@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>Fast Reflection project on CodePlex</title>
            <link>http://geekswithblogs.net/Madman/archive/2008/07/09/fast-reflection-project-on-codeplex.aspx</link>
            <description>&lt;p&gt;As a followup to my previous entry on faster reflection with expression trees, I have &lt;a href="http://www.codeplex.com/FastReflection"&gt;released the source on code plex&lt;/a&gt; and that is where i will actively be distributing updates through. &lt;/p&gt;
&lt;p&gt;Some noteable changes are that now you retrieve an FastProperty instance through a factory method which will cache the property for subsequent reference. Also, I have merged everything into one class because I realized I was looking at what information I needed in the wrong places. Now you can just say FastProperty&amp;lt;object,object&amp;gt;.Make() and it won't throw a fit on binding.&lt;/p&gt;
&lt;p&gt;Also added a couple of extension methods so that you can create a FastProperty from a PropertyInfo object, ie. var fastProperty = typeof(MyType).GetProperty("MyProperty").ToFastProperty();&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123676"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123676" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Madman/aggbug/123676.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Darren Kopp</dc:creator>
            <guid>http://geekswithblogs.net/Madman/archive/2008/07/09/fast-reflection-project-on-codeplex.aspx</guid>
            <pubDate>Wed, 09 Jul 2008 07:19:41 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Madman/comments/123676.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Madman/archive/2008/07/09/fast-reflection-project-on-codeplex.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Madman/comments/commentRss/123676.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Madman/services/trackbacks/123676.aspx</trackback:ping>
        </item>
        <item>
            <title>Faster Reflection using Expression Trees</title>
            <link>http://geekswithblogs.net/Madman/archive/2008/06/27/faster-reflection-using-expression-trees.aspx</link>
            <description>&lt;p&gt;Currently I'm working on a data synchronization tool in which I have finally had the need to use &lt;a href="http://en.wikipedia.org/wiki/Reflection_%28computer_science%29" target="_blank"&gt;reflection&lt;/a&gt;. I'll admit that I'm not an expert when it comes to reflection, but it isn't that hard to work with either. And I have heard before about the performance hit you take with reflection, so up until now I hadn't ever needed / wanted to use it, but I see now the power that reflection offers.&lt;/p&gt;  &lt;p&gt;One day while perusing my RSS feeds for the day I was reading one of &lt;a href="http://hanselman.com/blog/" target="_blank"&gt;Scott Hanselman's&lt;/a&gt; &lt;a href="http://www.hanselman.com/blog/TheWeeklySourceCode27SuckLessLibraries.aspx" target="_blank"&gt;weekly source code posts&lt;/a&gt; in which the &lt;a href="http://www.codeplex.com/UkadcDiagnostics" target="_blank"&gt;Ukadc.Diagnostics&lt;/a&gt; project uses Lightweight Code Generation (LCG) to &lt;a href="http://www.codeproject.com/KB/cs/DynamicCodeVsReflection.aspx" target="_blank"&gt;speed up the retrieval of the value of a property using reflection&lt;/a&gt;. Pretty slick, but what I need is a faster way to SET the value of a property without knowing it's type. Now, like I said before, I'm no reflection guru, nor am I an IL guru, but I thought I would give it a shot. Epic Fail.&lt;/p&gt;  &lt;p&gt;I'm kind of thick headed though when it comes to a problem, I usually don't give up unless I'm sure I can't win, and I'm not giving up. So I decided go try doing this the C# 3.0 way, where I don't have to IL generate anything. Ultimately, there is IL being generated, as reported by Reflector, but I'm not generating it, so all is well.&lt;/p&gt;  &lt;p&gt;So first off, lets list our requirements.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Improve performance of getting / setting properties via &lt;a href="http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo(VS.71).aspx" target="_blank"&gt;PropertyInfo&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Getter / Setter      &lt;ol&gt;       &lt;li&gt;Support for known type and known return type &lt;/li&gt;        &lt;li&gt;Support for known type and unknown return type &lt;/li&gt;        &lt;li&gt;Support unknown type and unknown return type &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;For this post, I'm only going to cover FastProperty, but in the attachment I'll include the files to support item #1 and item #2 when parts are unknown (FastProperty&amp;lt;T,P&amp;gt; and FastProperty&amp;lt;T&amp;gt;). The only real differences between each one is gradually replacing object for it's generic counterpart.&lt;/p&gt;  &lt;p&gt;So first, lets take a step back and talk about what a property &lt;em&gt;actually&lt;/em&gt; is. If we want to set a property, we would just do something like &lt;/p&gt;  &lt;p&gt;Employee e = new Employee();&lt;/p&gt;  &lt;p&gt;e.ID = 4;&lt;/p&gt;  &lt;p&gt;All is fine and well. But a property is just syntactic sugar over a method. In this instance we would have a set method called void set_ID(Integer) and a get method called Integer get_ID(). So what we really want to do is make a delegate for these methods. So to begin with, lets tackle the easiest method, the get method.&lt;/p&gt;  &lt;h2&gt;The Code&lt;/h2&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FastProperty&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;{&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt; Property { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; GetDelegate;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; SetDelegate;&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; FastProperty(&lt;span style="color: #2b91af"&gt;PropertyInfo&lt;/span&gt; property)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property = property;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        InitializeGet();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        InitializeSet();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    }&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InitializeSet()&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;var&lt;/span&gt; instance = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;), &lt;span style="color: #a31515"&gt;"instance"&lt;/span&gt;);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;var&lt;/span&gt; value = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;), &lt;span style="color: #a31515"&gt;"value"&lt;/span&gt;);&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: green"&gt;// value as T is slightly faster than (T)value, so if it's not a value type, use that&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: #2b91af"&gt;UnaryExpression&lt;/span&gt; instanceCast = (!&lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType.IsValueType) ? &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.TypeAs(instance, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType) : &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Convert(instance, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: #2b91af"&gt;UnaryExpression&lt;/span&gt; valueCast = (!&lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.PropertyType.IsValueType) ? &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.TypeAs(value, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.PropertyType) : &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Convert(value, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.PropertyType);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetDelegate = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Call(instanceCast, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.GetSetMethod(), valueCast), &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ParameterExpression&lt;/span&gt;[] { instance, value }).Compile();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    }&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InitializeGet()&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;var&lt;/span&gt; instance = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Parameter(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;), &lt;span style="color: #a31515"&gt;"instance"&lt;/span&gt;);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: #2b91af"&gt;UnaryExpression&lt;/span&gt; instanceCast = (!&lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType.IsValueType) ? &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.TypeAs(instance, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType) : &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Convert(instance, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.DeclaringType);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetDelegate = &lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Lambda&amp;lt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt;&amp;gt;(&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.TypeAs(&lt;span style="color: #2b91af"&gt;Expression&lt;/span&gt;.Call(instanceCast, &lt;span style="color: blue"&gt;this&lt;/span&gt;.Property.GetGetMethod()), &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;object&lt;/span&gt;)), instance).Compile();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    }&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt; Get(&lt;span style="color: blue"&gt;object&lt;/span&gt; instance)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetDelegate(instance);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    }&lt;/p&gt;      &lt;p style="margin: 0px"&gt; &lt;/p&gt;      &lt;p style="margin: 0px"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Set(&lt;span style="color: blue"&gt;object&lt;/span&gt; instance, &lt;span style="color: blue"&gt;object&lt;/span&gt; value)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;        &lt;span style="color: blue"&gt;this&lt;/span&gt;.SetDelegate(instance, value);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;    }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;}&lt;/p&gt;   &lt;/div&gt;    &lt;p style="margin: 0px"&gt;&lt;/p&gt; &lt;/div&gt;  &lt;h3&gt;Getter&lt;/h3&gt;  &lt;p&gt;The get method returns a value and takes no inputs, so we are going to use the &lt;a href="http://msdn.microsoft.com/en-us/library/bb549151.aspx" target="_blank"&gt;Func&amp;lt;T,TResult&amp;gt;&lt;/a&gt; delegate because we want to return a value. Now, don't freak out because that delegate takes in a parameter, but the method signature doesn't, the input is the instance you are calling the property on. This is what we are basically going to do:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;   &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;,&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; getter = instance =&amp;gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; instance.Property; &lt;/p&gt; &lt;/div&gt;  &lt;p&gt;or more correctly&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;   &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;,&lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; getter = instance =&amp;gt; &lt;span style="color: blue"&gt;return&lt;/span&gt; instance.get_Property();&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;We are just going to represent this as an Expression tree. So lets get down to the nitty gritty. All of our wonderful expression stuff lives in the &lt;font face="consol"&gt;System.Linq.Expresssions&lt;/font&gt; namespace. To recreate our lambda expression we would do the following. If we look at the InitializeGet method, we start off by declaring a parameter called instance. This basically just saying that we'll be passing in a parameter of type object into our lambda called instance. The next thing that we do is create an &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.unaryexpression.aspx" target="_blank"&gt;UnaryExpression&lt;/a&gt; that is casting our parameter from the type of object to the type of the property.&lt;/p&gt;  &lt;p&gt;Why are we casting from object to the type you ask? Well, because we don't know the type before compile time, we have to use a delegate of type Func&amp;lt;object,object&amp;gt;, so in order to create that delegate, we have to match that signature. But we also are going to be calling the get method on our instance variable, which isn't of type object, so we are casting it.&lt;/p&gt;  &lt;p&gt;So now, we are going to call the get_&amp;lt;Property&amp;gt; method. So we create an &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.methodcallexpression.aspx" target="_blank"&gt;MethodCallExpression&lt;/a&gt; via the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.call.aspx" target="_blank"&gt;Expression.Call&lt;/a&gt; method and pass in the instance of our object, which is properly cast to the correct type, and the get method for the property, via &lt;a href="http://msdn.microsoft.com/en-us/library/e17dw503.aspx" target="_blank"&gt;PropertyInfo.GetGetMethod()&lt;/a&gt;. But guess what, we have a delegate that is expecting a return value of type object back, not what the get_&amp;lt;Property&amp;gt; is actually returning (unless it happens to be object), so we do another &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.expression.typeas.aspx" target="_blank"&gt;Expression.TypeAs&lt;/a&gt; to convert the value back to object.&lt;/p&gt;  &lt;p&gt;Now finally we get the delegate for all of our hard work, so we call the &lt;a href="http://msdn.microsoft.com/en-us/library/bb336566.aspx" target="_blank"&gt;Expression.Lambda&amp;lt;T&amp;gt;&lt;/a&gt; method and pass our expression, and any parameters needed for it, which we only have 1 of, which is our instance, and then finally call the &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression.compile.aspx" target="_blank"&gt;Compile&lt;/a&gt; method on our &lt;a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression.aspx" target="_blank"&gt;LambdaExpression&lt;/a&gt; which will gives us our Func&amp;lt;object,object&amp;gt; delegate.&lt;/p&gt;  &lt;p&gt;Simple as pie right? Well, if you kept up with all that, we should be able to figure out the setter.&lt;/p&gt;  &lt;h3&gt;Setter&lt;/h3&gt;  &lt;p&gt;The set method is a void and takes exactly 1 parameter, so we are going to use the &lt;a href="http://msdn.microsoft.com/en-us/library/bb549311.aspx" target="_blank"&gt;Action&amp;lt;T1,T2&amp;gt;&lt;/a&gt; delegate. So what we are basically wanting to do is this:&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;   &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; setter = (instance, value) =&amp;gt; instance.Property = value;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;or more correctly&lt;/p&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: consolas"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;   &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Action&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;object&lt;/span&gt;, &lt;span style="color: blue"&gt;object&lt;/span&gt;&amp;gt; setter = (instance, value) =&amp;gt; instance.set_Property(value);&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;So to begin with, we create parameters for instance, and for value, both of type object. Then what do we do next? Well, we cast them back to their proper type. Now we are going to call the method, just like we did with the get method, but we are going to pass in a parameter - value. Now all we need to do is create our Lambda and compile. And we are done.&lt;/p&gt;  &lt;h1&gt;Performance&lt;/h1&gt;  &lt;p&gt;For this performance test, I'm using the &lt;a href="http://thecodeslinger.wordpress.com/2007/11/27/dynamic-object-instantiation-performance/" target="_blank"&gt;Get/Set implementation&lt;/a&gt; put forth by &lt;a href="http://thecodeslinger.wordpress.com/" target="_blank"&gt;Pete the CodeSlinger&lt;/a&gt; which uses IL Generation and DynamicMethod, the Expression Tree method which I have put forth, the "classic" reflection approach, and, of course, the native approach.&lt;/p&gt;  &lt;p&gt;For 10,000,000 iterations, here is the time in milliseconds for each approach. The results are kind of creepy in the way the it's almost a mirror on each side of the decimal point.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Native      &lt;ol&gt;       &lt;li&gt;Get - 169.0169 &lt;/li&gt;        &lt;li&gt;Set - 1874.1874 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;PropertyCaller&amp;lt;T,K&amp;gt;      &lt;ol&gt;       &lt;li&gt;Get - 389.0389 &lt;/li&gt;        &lt;li&gt;Set - 1966.1966 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;FastProperty&amp;lt;T,P&amp;gt;      &lt;ol&gt;       &lt;li&gt;Get - 301.0301 &lt;/li&gt;        &lt;li&gt;Set - 1956.1956 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;FastProperty&amp;lt;T&amp;gt;      &lt;ol&gt;       &lt;li&gt;Get - 470.047 &lt;/li&gt;        &lt;li&gt;Set - 2247.2247 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;FastProperty      &lt;ol&gt;       &lt;li&gt;Get - 516.0516 &lt;/li&gt;        &lt;li&gt;Set - 2277.2277 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;PropertyInfo      &lt;ol&gt;       &lt;li&gt;Get - 7313.7313 &lt;/li&gt;        &lt;li&gt;Set - 14163.4162 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt;    &lt;li&gt;MethodInfo - via PropertyInfo.GetGetMethod / PropertyInfo.GetSetMethod      &lt;ol&gt;       &lt;li&gt;Get - 6960.696 &lt;/li&gt;        &lt;li&gt;Set - 13702.3701 &lt;/li&gt;     &lt;/ol&gt;   &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;As we can see, using Expression Trees we can get MUCH better performance than using the standard reflection methods even when we don't know type or return type. And the only performance penalty we pay is for boxing. Also, each FastProperty call up there assumes calling the Set / Get method on the FastProperty instance. If we call the actual delegate (GetDelegate and SetDelegate) we actually achieve better performance than the PropertyCaller delegates in the FastProperty&amp;lt;T,P&amp;gt; instance, and the difference between then FastProperty&amp;lt;T&amp;gt; and FastProperty instances is 3 milliseconds on the get, and 19 milliseconds on the set.&lt;/p&gt;  &lt;h3&gt;The more you know before hand, the better.&lt;/h3&gt;  &lt;p&gt;So, although I don't think it needs to be stated, I will, just to be thorough. The more you know before hand, the better this will perform. So, if you don't know the type you will be using, or the return type, use FastProperty. If you know the type, but not the return type of the property, use FastProperty&amp;lt;T&amp;gt; because then you can avoid the casting. It's not a terrible performance hit, but developers are perfectionists, lets not kid ourselves. And if you are just going to want to get a property of a type, for whatever reason (you should be going native, but I don't know your situation) then use FastProperty&amp;lt;T,P&amp;gt;.&lt;/p&gt;  &lt;h3&gt;Where to go next?&lt;/h3&gt;  &lt;p&gt;Well, I'm just laying out the framework that I have built, I'll leave it up to you to do whatever you want. It's faster to invoke the delegates than to call the Get / Set method on the FastProperty class, but I leave that there to allow you to add error handling (i.e. you can see if the value is assignable from the property.PropertyType, and throw an exception there instead of it coming up from the delegate code, which will be hard to track down).&lt;/p&gt;  &lt;h1&gt;Source&lt;/h1&gt;  &lt;p&gt;You can pick up all the source for this over at the &lt;a href="https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=FastReflection" target="_blank"&gt;FastReflection&lt;/a&gt; project at code.msdn.com. I'll probably be moving this over to codeplex eventually and supported more things via reflection (fields, methods?), we'll see. For now, i'm just going to put everything up at code.msdn.com.&lt;/p&gt;  &lt;h3&gt;License&lt;/h3&gt;  &lt;p&gt;This code is released under the "&lt;em&gt;OMG LOL go pwn the developer next to you with this new knowledge"&lt;/em&gt; license. Ok not really, I'm just putting Ms-PL for code.msdn.com, which I believe lets you do anything you want. If not, let me know and I'll change the license. Hopefully this has been helpful for all who stumble upon this.&lt;/p&gt;  &lt;p&gt;Happy .NETing,    &lt;br /&gt;Darren&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123434"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=123434" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Madman/aggbug/123434.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Darren Kopp</dc:creator>
            <guid>http://geekswithblogs.net/Madman/archive/2008/06/27/faster-reflection-using-expression-trees.aspx</guid>
            <pubDate>Fri, 27 Jun 2008 22:18:25 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Madman/comments/123434.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Madman/archive/2008/06/27/faster-reflection-using-expression-trees.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Madman/comments/commentRss/123434.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Madman/services/trackbacks/123434.aspx</trackback:ping>
        </item>
        <item>
            <title>Recurse the Control Tree in ASP.NET</title>
            <link>http://geekswithblogs.net/Madman/archive/2008/02/20/recurse-the-control-tree-in-asp.net.aspx</link>
            <description>&lt;p&gt;I came across a need the other day to recurse the page control tree (or any control tree really) to find all controls of a certain type, so this is the extension method I wrote to help me do that. Hopefully it will help others as well.&lt;/p&gt;  &lt;p&gt;/// &amp;lt;summary&amp;gt;    &lt;br /&gt;/// Recurses the control tree and finds all controls in the control collection that are of the specified type.     &lt;br /&gt;/// &amp;lt;/summary&amp;gt;     &lt;br /&gt;/// &amp;lt;typeparam name="T"&amp;gt;The type of control you want to find&amp;lt;/typeparam&amp;gt;     &lt;br /&gt;/// &amp;lt;param name="controls"&amp;gt;The controls.&amp;lt;/param&amp;gt;     &lt;br /&gt;/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;     &lt;br /&gt;public static IEnumerable&amp;lt;T&amp;gt; FindRecursive&amp;lt;T&amp;gt;(this ControlCollection controls)     &lt;br /&gt;{     &lt;br /&gt;    // create list of results     &lt;br /&gt;    List&amp;lt;T&amp;gt; results = new List&amp;lt;T&amp;gt;(); &lt;/p&gt;  &lt;p&gt;    // add controls in current control collection that match type    &lt;br /&gt;    results.AddRange(controls.OfType&amp;lt;T&amp;gt;()); &lt;/p&gt;  &lt;p&gt;    // recurse control tree    &lt;br /&gt;    foreach (Control c in controls)     &lt;br /&gt;    {     &lt;br /&gt;        // add controls to results     &lt;br /&gt;        results.AddRange(FindRecursive&amp;lt;T&amp;gt;(c.Controls));     &lt;br /&gt;    } &lt;/p&gt;  &lt;p&gt;    return results;    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;p&gt;This method could easily be extended to accept another parameter to specify the ID of the control. The method above does depend on System.Linq and .NET 3.5 (or at least visual studio 2008, because I believe that most of this is just a compiler trick).&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=119798"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=119798" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Madman/aggbug/119798.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Darren Kopp</dc:creator>
            <guid>http://geekswithblogs.net/Madman/archive/2008/02/20/recurse-the-control-tree-in-asp.net.aspx</guid>
            <pubDate>Wed, 20 Feb 2008 15:16:57 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Madman/comments/119798.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Madman/archive/2008/02/20/recurse-the-control-tree-in-asp.net.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Madman/comments/commentRss/119798.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Madman/services/trackbacks/119798.aspx</trackback:ping>
        </item>
        <item>
            <title>Having fun with the new guy</title>
            <link>http://geekswithblogs.net/Madman/archive/2007/08/17/Having-fun-with-the-new-guy.aspx</link>
            <description>&lt;p&gt;While being a developer short for a while isn't the most fun thing for a small company... losing your new developer wouldn't be much fun either, but i think this was worth taking the chance.&lt;/p&gt;
&lt;p&gt;&lt;img alt="" src="http://geekswithblogs.net/images/geekswithblogs_net/Madman/6991/r_hazing_developers.PNG" /&gt;&lt;/p&gt;
&lt;p&gt;What would you do if the first time you loaded up visual studio you saw this? From the look on the new guys face... i would categorize the 30 minutes i spent finding the most rediculous font for programming and setting up visual studio to be well spent.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=114752"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=114752" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Madman/aggbug/114752.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Darren Kopp</dc:creator>
            <guid>http://geekswithblogs.net/Madman/archive/2007/08/17/Having-fun-with-the-new-guy.aspx</guid>
            <pubDate>Fri, 17 Aug 2007 22:38:56 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Madman/comments/114752.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Madman/archive/2007/08/17/Having-fun-with-the-new-guy.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Madman/comments/commentRss/114752.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Madman/services/trackbacks/114752.aspx</trackback:ping>
        </item>
        <item>
            <title>Living the verbose lifestyle</title>
            <link>http://geekswithblogs.net/Madman/archive/2007/06/22/Living-the-verbose-lifestyle.aspx</link>
            <description>&lt;p&gt;I've decided to live the verbose lifestyle in programming. I've learned that usually doing something verbosely ends up working better in the long run (even though it might take a bit longer). I'm definately glad that the visual studio and/or c# compiler team do this as well.&lt;/p&gt;
&lt;p&gt;Right now our company is in the middle of converting from asp to .NET and have decided that having the project run from the root of the website. So we changed our visual studio web development server virtual path to "/". After doing this (and some other things) i tried to publish and got a message in the status bar "Build Failed". No errors. No other messages. Nothing.&lt;/p&gt;
&lt;p&gt;After digging around and googling for a solution, i randomly ended up in the visual studio settings under the "Projects and Solutions" item. In the "General" section under this, i checked "Show Output window when build starts" and under the "Build and Run" item i changed "MSBuild project build output verbosity" to Normal (from minimal).&lt;/p&gt;
&lt;p&gt;After doing this i saw this&lt;/p&gt;
&lt;font size="1"&gt;
&lt;p&gt;------ Build started: Project: C:\...\hh2WebServices\, Configuration: Debug .NET ------&lt;br /&gt;
Index was outside the bounds of the array.&lt;br /&gt;
------ Skipped Publish: Project C:\...\hh2WebServices\, Configuration: Debug .NET ------&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;Interesting. Now googling this i ended up on the illustrious Scott Gunthrie's blog and noticed that he had &lt;a href="http://weblogs.asp.net/scottgu/archive/2007/04/23/public-hotfix-patch-for-vs-index-was-outside-the-bounds-of-the-array-publish-website-issue.aspx"&gt;written a post about this issue&lt;/a&gt; and posted some workarounds and a link to the hot fix.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font size="2"&gt;It's not a new issue, but it's the first time i had run into it, and it took a while to figure it out. So here is hoping that other people might be able to figure out the issue just a little bit faster. &lt;/font&gt;&lt;/p&gt;
&lt;/font&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113394"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=113394" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/Madman/aggbug/113394.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Darren Kopp</dc:creator>
            <guid>http://geekswithblogs.net/Madman/archive/2007/06/22/Living-the-verbose-lifestyle.aspx</guid>
            <pubDate>Fri, 22 Jun 2007 15:51:26 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Madman/comments/113394.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Madman/archive/2007/06/22/Living-the-verbose-lifestyle.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Madman/comments/commentRss/113394.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Madman/services/trackbacks/113394.aspx</trackback:ping>
        </item>
    </channel>
</rss>