<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Mitsu's blog</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/mitsu/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/mitsu/feed.xml</link>
    <description>Discussing topics related to .Net, WPF, C# and Linq</description>
    <lastBuildDate>Sat, 04 Apr 2015 19:36:21 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Some basic sample to make your code “Linq ready”</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/some-basic-sample-to-make-your-code-linq-ready</link>
      <pubDate>Fri, 18 Jun 2010 01:58:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2010/06/18/some-basic-sample-to-make-your-code-linq-ready/</guid>
      <description><![CDATA[&nbsp;
Linq has been shipped with VS2008/.Net 3.5 and is not really new. If more and more people...]]></description>
      <content:encoded><![CDATA[<p>&nbsp;</p>
<p>Linq has been shipped with VS2008/.Net 3.5 and is not really new. If more and more people are getting used to its syntax, it&rsquo;s sometimes hard to imagine all the scenarii where Linq to object can replace the classical way we program. During some Silverlight coding, I changed my code to make it run in a better Linq spirit and I wanted to share with you this little sample.</p>
<p>Silverlight (and WPF) maintains a tree of visuals. A useful class named VisualTreeHelper provides a very simple api to navigate this tree. One very common scenario is to look for the first visual parent of a T type for a given control. For instance, your control is in a template and you want to retrieve the templated control which you know is a ListBoxItem.</p>
<p>The VisualTreeHelper.GetParent() method (see prototype below) simply gives the graphical parent of any visual (as DependencyObject).</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6555de84-0bbd-49b0-9fba-cf205ea4cdbf" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> VisualTreeHelper
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;">
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Summary:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">     Returns an object's parent object in the visual tree.
    </span><span style="color: #008000;">//</span><span style="color: #008000;">
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Parameters:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">   reference:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">     The object to get the parent object for.
    </span><span style="color: #008000;">//</span><span style="color: #008000;">
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Returns:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">     The parent object of the reference object in the visual tree.
    </span><span style="color: #008000;">//</span><span style="color: #008000;">
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Exceptions:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">   System.InvalidOperationException:
    </span><span style="color: #008000;">//</span><span style="color: #008000;">     reference is null, or is not a valid System.Windows.UIElement.</span><span style="color: #008000;">
</span><span style="color: #000000;">    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> DependencyObject GetParent(DependencyObject reference);
    ...
}
</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<p>Let&rsquo;s try to implement this simple scenario.</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fbc73cbc-ae6a-438e-9c0a-1623a5be92cc" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:White;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> MyDependencyObjectExtensions
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> T GetVisualParent</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #0000FF;">this</span><span style="color: #000000;"> DependencyObject source) <br /></span><span style="color: #0000FF;"><span style="color: #000000;">        </span>where</span><span style="color: #000000;"> T : </span><span style="color: #0000FF;">class</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">do</span><span style="color: #000000;">
        {
            source </span><span style="color: #000000;">=</span><span style="color: #000000;"> VisualTreeHelper.GetParent(source);
        } </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">(source </span><span style="color: #0000FF;">is</span><span style="color: #000000;"> T) </span><span style="color: #000000;">&amp;&amp;</span><span style="color: #000000;"> (source </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">));
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> source </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> T;
    }
}</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />We can now call: <br /><br />
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:68bd0ce7-6918-4d6c-a47d-88cd7a69c8b8" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">var lbi </span><span style="color: #000000;">=</span><span style="color: #000000;"> button1.GetVisualParent</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ListBoxItem</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />Now let&rsquo;s try to think in a Linq way. From a given control in the control tree, we can consider all its graphical parents until the root (Page or Window) to be an iteration. In this first implementation we are classically using a while loop to retrieve each parent one by one. This is exactly the definition of an IEnumerable.</p>
<p>In a first step we will just try to retrieve the list of parents :</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e0af6ac4-1482-4fc0-8841-f90d6ebc00be" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> MyDependencyObjectExtensions
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">DependencyObject</span><span style="color: #000000;">&gt;<br /></span><span style="color: #000000;">        GetVisualParents(</span><span style="color: #0000FF;">this</span><span style="color: #000000;"> DependencyObject source)
    {
        </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
        {
            source </span><span style="color: #000000;">=</span><span style="color: #000000;"> VisualTreeHelper.GetParent(source);
            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (source </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> source;
            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">break</span><span style="color: #000000;">;
        }
    }
}</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />Now we have a IEnumerable (a Linq compatible source), we can look for the first parent of a T type using Linq ! <br />I have also created a version allowing to define the depth of your query in case you don&rsquo;t want to find the first T element but a further one.</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:14f93ae1-d11e-4fe2-ade0-3237f279a54a" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;;font-family:Microsoft Sans Serif;font-size:8,25"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> MyDependencyObjectExtensions
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">DependencyObject</span><span style="color: #000000;">&gt;<br /></span><span style="color: #000000;">        GetVisualParents(</span><span style="color: #0000FF;">this</span><span style="color: #000000;"> DependencyObject source)
    {
        </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
        {
            source </span><span style="color: #000000;">=</span><span style="color: #000000;"> VisualTreeHelper.GetParent(source);
            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (source </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> source;
            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">break</span><span style="color: #000000;">;
        }
    }
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> T GetVisualParent</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #0000FF;">this</span><span style="color: #000000;"> DependencyObject source)<br />        </span><span style="color: #0000FF;">where</span><span style="color: #000000;"> T : </span><span style="color: #0000FF;">class</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> source.GetVisualParent</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800080;">0</span><span style="color: #000000;">);
    }
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> T GetVisualParent</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #0000FF;">this</span><span style="color: #000000;"> DependencyObject source, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> level)<br /></span><span style="color: #000000;">        </span><span style="color: #0000FF;">where</span><span style="color: #000000;"> T : </span><span style="color: #0000FF;">class</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> source.GetVisualParents().OfType</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">().Skip(level).FirstOrDefault();
    }
}
</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />We can see how Linq simplifies the implementation and the composition possibilities.</p>
<p>Last effort, let&rsquo;s try to imagine a generic way to create enumerables from functional parts.</p>
<p>In our case we have :</p>
<ul>
<li>the starting value</li>
<li>the way we get the next value</li>
<li>the exit condition</li>
</ul>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:38675201-4f67-476e-b107-c7f1d9f00993" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> EnumerableHelper
{
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> Create</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(T startElement,<br />        Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> nextElementProvider, Predicate</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> exitCondition)
    {
        </span><span style="color: #0000FF;">while</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">true</span><span style="color: #000000;">)
        {
            startElement </span><span style="color: #000000;">=</span><span style="color: #000000;"> nextElementProvider(startElement);
            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">exitCondition(startElement))
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> startElement;
            </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
                </span><span style="color: #0000FF;">yield</span><span style="color: #000000;"> </span><span style="color: #0000FF;">break</span><span style="color: #000000;">;
        }
    }
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> Create</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(T startElement,<br />        Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> nextElementProvider)
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Create(startElement, nextElementProvider, e </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> e </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">);
    }
}
</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />We can now call :</p>
<p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f7b72861-6578-4d2b-8a6d-a9ba54eff391" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #000000;">var parents </span><span style="color: #000000;">=</span><span style="color: #000000;"> EnumerableHelper.Create(<br />    button1 </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> DependencyObject,<br />    d </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> VisualTreeHelper.GetParent(d));<br />
var lbi </span><span style="color: #000000;">=</span><span style="color: #000000;"> parents.OfType</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ListBoxItem</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">().FirstOrDefault(); 
</span></pre>
<!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></!--></div>
<br />You can notice how generic is the Create method. From the caller view the Enumerable is created &ldquo;on demand&rdquo;.</p>]]></content:encoded>
    </item>
    <item>
      <title>C# 4 expressions: loops, goto, label, if and even for ! [Part III]</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/c-4-expressions-loops-goto-label-if-and-even-for-part-iii</link>
      <pubDate>Tue, 02 Mar 2010 06:53:37 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2010/03/02/c-4-expressions-loops-goto-label-if-and-even-for-part-iii/</guid>
      <description><![CDATA[We now have block and variable support in our very small meta language.    Let’s try to use this...]]></description>
      <content:encoded><![CDATA[<p>We now have block and variable support in our very small meta language.    <br />Let’s try to use this basic engine to add higher functionalities.</p>  <p>.Net 4.0 expression API also brings new instructions such as Loop, Goto, Label, IfThenElse, etc.</p>  <p>We will add them with always the same process: adding a comprehensive method in the Block class and implement the appropriate transformation in the visitor.</p>  <p>First, here is the Block class with all our meta language dictionary. I have never written so many methods without implementing them ! :)</p>  <p></p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1ef8e553-63fa-43c0-886e-520a6c9b47dd" class="wlWriterEditableSmartContent"><pre style="background-color:#C0C0C0;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> Block
{
  </span><span style="color: #008000;">//</span><span style="color: #008000;">prevents the use of the default public
  </span><span style="color: #008000;">//</span><span style="color: #008000;">constructor
  </span><span style="color: #008000;">//</span><span style="color: #008000;">so we hare sure that the only way to get 
  </span><span style="color: #008000;">//</span><span style="color: #008000;">Block instances is to use Block.Start()</span><span style="color: #008000;">
</span><span style="color: #000000;">  </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> Block()
  {
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Block Start</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(T locals,
    Action</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> block)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> R Start</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, R</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(T locals,
    Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, R</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> block)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Block Default 
    { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block _(Action action)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block Assign</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> leftExpression, 
    T rightExpression)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block Loop(Action block)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block IfThen(</span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> testExpression, 
    Action thenBlock)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block IfThenElse(</span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> testExpression, 
    Action thenBlock, Action elseBlock)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block While(</span><span style="color: #0000FF;">bool</span><span style="color: #000000;"> testExpression,
    Action block)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block For(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> from, Predicate</span><span style="color: #000000;">&lt;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> 
    condition, Action</span><span style="color: #000000;">&lt;</span><span style="color: #0000FF;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> block)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block Label(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> name)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
  
  </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> Block Goto(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> name)
  {
    </span><span style="color: #0000FF;">throw</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NotImplementedException();
  }
}</span></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>

<br />Then the code to recognize our methods and transform them to something understandable to the expression API : 

<br />

<br />

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b0135ba0-083b-41ce-87b9-138f9a93d418" class="wlWriterEditableSmartContent"><pre style="background-color:#C0C0C0;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">private</span><span style="color: #000000;"> Expression VisitBlockMethodCall(MethodCallExpression node)
{
  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">Label</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  {
    </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> labelName </span><span style="color: #000000;">=</span><span style="color: #000000;"> 
      (</span><span style="color: #0000FF;">string</span><span style="color: #000000;">)(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;">
        ConstantExpression).Value;
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Expression.Label(
      GetLabelTarget(labelName));
  }
    
  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">IfThen</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> 
      Expression.IfThen(
        Visit(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">]),
        Visit((node.Arguments[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> 
          LambdaExpression).Body));
    
  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">IfThenElse</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> 
      Expression.IfThenElse(
        Visit(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">]), 
        Visit((node.Arguments[</span><span style="color: #800080;">1</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;">
          LambdaExpression).Body),
            Visit((node.Arguments[</span><span style="color: #800080;">2</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> 
        LambdaExpression).Body));

  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">Goto</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    {
      </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> labelName </span><span style="color: #000000;">=</span><span style="color: #000000;"> 
        (</span><span style="color: #0000FF;">string</span><span style="color: #000000;">)(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> 
          ConstantExpression).Value;
      </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Expression.Goto(
        GetLabelTarget(labelName));
    }

  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">Loop</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    {
      var result </span><span style="color: #000000;">=</span><span style="color: #000000;"> (node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> 
        LambdaExpression).Body;
      </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Expression.Loop(Visit(result));
    }

  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">While</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    {
      var exitLabel </span><span style="color: #000000;">=</span><span style="color: #000000;"> Expression.Label();

      var block </span><span style="color: #000000;">=</span><span style="color: #000000;">
        Expression.Block(
          </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Expression[] {
            Expression.Loop(
              Expression.Block(
                </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Expression[] {
                  Expression.IfThen(
                    Expression.Not(
                Visit(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">])),
                    Expression.Goto(exitLabel)),
                    Visit((node.Arguments[</span><span style="color: #800080;">1</span><span style="color: #000000;">] 
                </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> LambdaExpression).Body)
                })),
            Expression.Label(exitLabel)
          });
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> block;
    }
  </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (IsMethodOf</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Block</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(node, </span><span style="color: #800000;">"</span><span style="color: #800000;">For</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    {
      var l </span><span style="color: #000000;">=</span><span style="color: #000000;"> node.Arguments[</span><span style="color: #800080;">2</span><span style="color: #000000;">]
        </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> LambdaExpression;
      var i </span><span style="color: #000000;">=</span><span style="color: #000000;"> l.Parameters[</span><span style="color: #800080;">0</span><span style="color: #000000;">];
      var test </span><span style="color: #000000;">=</span><span style="color: #000000;"> node.Arguments[</span><span style="color: #800080;">1</span><span style="color: #000000;">] 
        </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> LambdaExpression;
      var exitLabel </span><span style="color: #000000;">=</span><span style="color: #000000;"> Expression.Label();
      
      var block </span><span style="color: #000000;">=</span><span style="color: #000000;">
        Expression.Block(
          </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ParameterExpression[] { i },
          </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Expression[] {
            Expression.Assign(i,
              Visit(node.Arguments[</span><span style="color: #800080;">0</span><span style="color: #000000;">])),
            Expression.Loop(
              Expression.Block(
                </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Expression[] {
                  Expression.IfThen(
                    VisitWithReplaceParameter(
                      test.Parameters[</span><span style="color: #800080;">0</span><span style="color: #000000;">], i,
                      Expression.Not(test.Body)), 
                  Expression.Goto(exitLabel)),
                  VisitWithReplaceParameter(
                    l.Parameters[</span><span style="color: #800080;">0</span><span style="color: #000000;">], i, l.Body),
                  Expression.Assign(i,
                    Expression.Add(i,
                      Expression.Constant(</span><span style="color: #800080;">1</span><span style="color: #000000;">)))
                })),
              Expression.Label(exitLabel)
            });
      </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> block;
    }
  </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">; 
}</span></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>

<br />You can notice that most of methods directly have a corresponding one in the expression API. But some of them are higher level ones like ‘While’ or ‘For’ that do not exist in the expression API.

<p></p>

<p>So I had to implement a bigger transformation logic, using many functions of the expression API.</p>

<p>I can now write richer expressions mixing all those features:</p>

<p></p>

<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:dd8e9ebb-6a06-4830-944a-e757a6d7e34e" class="wlWriterEditableSmartContent"><pre style="background-color:#C0C0C0;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Action</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> expLabel </span><span style="color: #000000;">=</span><span style="color: #000000;"> () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
  Block.Default
    ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Start</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    .Goto(</span><span style="color: #800000;">"</span><span style="color: #800000;">exit</span><span style="color: #800000;">"</span><span style="color: #000000;">)
    ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">not printed</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    .Label(</span><span style="color: #800000;">"</span><span style="color: #800000;">exit</span><span style="color: #800000;">"</span><span style="color: #000000;">)
    ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">end</span><span style="color: #800000;">"</span><span style="color: #000000;">));

expLabel </span><span style="color: #000000;">=</span><span style="color: #000000;"> ExpressionHelper.Translate(expLabel);
expLabel.Compile()();

Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nIfThenElse support</span><span style="color: #800000;">"</span><span style="color: #000000;">);

Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Action</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> expIfThenElse </span><span style="color: #000000;">=</span><span style="color: #000000;"> () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
  Block.Start(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> { s </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">""</span><span style="color: #000000;"> }, b </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
    Block.Default
        ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.Write(</span><span style="color: #800000;">"</span><span style="color: #800000;">Enter 'OK'</span><span style="color: #800000;">"</span><span style="color: #000000;">))
        .Assign(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> b.s, Console.ReadLine())
        .IfThenElse(b.s </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">OK</span><span style="color: #800000;">"</span><span style="color: #000000;">,
            () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(
              </span><span style="color: #800000;">"</span><span style="color: #800000;">Good for me</span><span style="color: #800000;">"</span><span style="color: #000000;">),
            () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Sorry</span><span style="color: #800000;">"</span><span style="color: #000000;">))
        );

expIfThenElse </span><span style="color: #000000;">=</span><span style="color: #000000;"> ExpressionHelper.Translate(expIfThenElse);
expIfThenElse.Compile()();

Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nLoop support</span><span style="color: #800000;">"</span><span style="color: #000000;">);

Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Action</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> expLoop </span><span style="color: #000000;">=</span><span style="color: #000000;"> () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
  Block.Start(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> { i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> }, b </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
    Block.Default
      .Loop(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
        Block.Default
          ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> 
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">loop: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> b.i))
          .Assign(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> b.i, b.i </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">)
          .IfThen(b.i </span><span style="color: #000000;">&gt;=</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">,
            () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Block.Default.Goto(</span><span style="color: #800000;">"</span><span style="color: #800000;">break</span><span style="color: #800000;">"</span><span style="color: #000000;">))
          )
      .Label(</span><span style="color: #800000;">"</span><span style="color: #800000;">break</span><span style="color: #800000;">"</span><span style="color: #000000;">)
      ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">end</span><span style="color: #800000;">"</span><span style="color: #000000;">))
  );

expLoop </span><span style="color: #000000;">=</span><span style="color: #000000;"> ExpressionHelper.Translate(expLoop);
expLoop.Compile()();

Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nFor support</span><span style="color: #800000;">"</span><span style="color: #000000;">);

Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Action</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> expFor </span><span style="color: #000000;">=</span><span style="color: #000000;"> () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
  Block.Default
    .For(</span><span style="color: #800080;">0</span><span style="color: #000000;">, i </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">,
      i </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">For:</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> i));

expFor </span><span style="color: #000000;">=</span><span style="color: #000000;"> ExpressionHelper.Translate(expFor);
expFor.Compile()();

Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">\nWhile support</span><span style="color: #800000;">"</span><span style="color: #000000;">);

Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Action</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> expWhile </span><span style="color: #000000;">=</span><span style="color: #000000;"> () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
  Block.Start(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> { i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> }, b </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;">
    Block.Default
      .While(b.i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">, 
        () </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Block.Default
          ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> 
            Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">While:</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> b.i))
          .Assign(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> b.i, b.i </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800080;">1</span><span style="color: #000000;">))
      ._(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">end</span><span style="color: #800000;">"</span><span style="color: #000000;">))
    );

expWhile </span><span style="color: #000000;">=</span><span style="color: #000000;"> ExpressionHelper.Translate(expWhile);
expWhile.Compile()();</span></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>

<br />We now have richer C# expressions possibilities. 

<br />I hope one day we will have natural support for all of this and even more in our favorite language.

<p></p>

<p>Mitsu</p>

<p>The whole project is available here: <a title="http://code.msdn.microsoft.com/CSharp4Expressions/" href="http://code.msdn.microsoft.com/CSharp4Expressions/">http://code.msdn.microsoft.com/CSharp4Expressions/</a></p>]]></content:encoded>
    </item>
    <item>
      <title>C# 4 expressions: variables [Part II]</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/c-4-expressions-variables-part-ii</link>
      <pubDate>Tue, 02 Mar 2010 06:51:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2010/03/02/c-4-expressions-variables-part-ii/</guid>
      <description><![CDATA[Reading the first part is mandatory !
The goal is still the same: enhance the C# 4 expression...]]></description>
      <content:encoded><![CDATA[<P>Reading the <A href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-blocks-part-i.aspx" target=_blank mce_href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-blocks-part-i.aspx">first part</A> is mandatory !</P>
<P>The goal is still the same: enhance the C# 4 expression capabilities.</P>
<P>Now we have block support, let’s find a way to add variables. We can not really ‘code’ something without them…</P>
<P>In our first sample, the main lambda already had a parameter and we were able to use it inside our expression. Technically, adding variables is just creating new local parameters which is easy: </P>
<P>Expression.Parameter(typeof(int), “param”) then we can add this parameter as a variable of a block.</P>
<P>Once again, what is hard is to find a way to express and recognize them easily.</P>
<P>First idea, creating :</P>
<P>Block.DefineVariable(typeof(int), “param”) <BR>Block.ReadVariable(“param”) <BR>Block.AssignVariable(“param”, 1);</P>
<P>Easy to do and easy to transform, except it’s long to write and non typed. We would need a lot of cast instructions to make it run for real, heavy, no test at compile time, unsafe…not good.</P>
<P>Here is what I propose. As lambda can have access to the scope of their hosting method (see this article), we could imagine to create our needed variables outside the expression and then convert them to local parameter during the transformation. <BR>Quite a strange solution with unnecessary declarations inside the hosting method but the idea is here. Let’s create an intermediate scope between the lambda and its hosting method.</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:41b6b19a-c786-4480-bec8-5c1c3f1a6f09 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> Block
{
    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> Block Start</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(T locals, Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> block)
    {
        </SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> NotImplementedException();
    }
    ...</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>Imagine a Start method taking a first instance (that resolves the generic parameter T) and then a second argument defining an action with this a parameter of this same type. 
<P mce_keep="true">Now imagine we use an anonymous type to define this first instance.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bfb85e0a-b357-4c1b-a881-fff46067a66d class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> exp </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Start(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> { j </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000"> }, b </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
        Console.WriteLine(b.j)
    );</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>In this code, type of b is defined by ‘new { j = 1 }’. <BR>b is of course accessible in the body of the lambda and so are all its properties. <BR>b.j is accessible and typed correctly. 
<P mce_keep="true">The only default is b.j is read-only because such are anonymous types properties. But whatever, C# does not supports affectation in expressions. So we will symbolically write something like :</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:23834e2e-6be1-4632-bde1-81fe658629fe class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Block.Assign(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> b.j, </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">);</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>…and we will transform it into something executable. 
<P mce_keep="true">Let’s talk about the transformation. We need to recognize Block.Start, analyze the type of the first argument, create as many local parameters as the first argument exposes properties (same name, same type of course) and then replace all the references to b.X by the corresponding parameter. Last feature, we can initialize all the parameters with the values found in the anonymous type definition. <BR>We also have to make it run correctly recursively.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:203b6d1d-ac90-48bc-b56f-396e898f4de0 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> Expression VisitBlockMethodCall(MethodCallExpression node)
{
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Block</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(node, </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Assign</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
    {
      var left </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> node.Arguments[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">]
        </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000"> LambdaExpression;
      </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> Expression.Assign(Visit(left.Body
        ,Visit(node.Arguments[</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">]));
    }
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Block</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(node, </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Start</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">, </SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">))
    {
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Anonymous type analyzed to generate
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">parameters</SPAN><SPAN style="COLOR: #008000">
</SPAN><SPAN style="COLOR: #000000">      var newExpression </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> node.Arguments[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">]
        </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000"> NewExpression;
      var variablesToAdd </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">
        newExpression.Type.GetProperties()
          .Select((p, i) </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
            </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> {
              Parameter </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> Expression.Parameter(
                p.PropertyType,
                p.Name),
              Value </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> newExpression.Arguments[i]
              }).ToArray();
      
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Prepares properties to be replaced
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">by parameters and adds them to a
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">global collection</SPAN><SPAN style="COLOR: #008000">
</SPAN><SPAN style="COLOR: #000000">      var blockStartLambda </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> node.Arguments[</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">]
        </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000"> LambdaExpression;
      var parameters </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">
        variablesToAdd.ToDictionary(
          v </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> v.Parameter.Name,
          v </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> v.Parameter);
          
      blockStartParameters.Add(
        blockStartLambda.Parameters[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">],
        parameters);
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">VisitMember will make the right
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">replacements during this recursive
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">call</SPAN><SPAN style="COLOR: #008000">
</SPAN><SPAN style="COLOR: #000000">      var body </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> Visit(blockStartLambda.Body);
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Removes from collection</SPAN><SPAN style="COLOR: #008000">
</SPAN><SPAN style="COLOR: #000000">      blockStartParameters.Remove(
        blockStartLambda.Parameters[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">]);
        
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">Creates a block with parameters 
      </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">declaration and initialization</SPAN><SPAN style="COLOR: #008000">
</SPAN><SPAN style="COLOR: #000000">      var blockWithVariablesInitialization </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">
        variablesToAdd.Select(v </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> 
          Expression.Assign(v.Parameter, v.Value))
          .Concat(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Expression[] { body });

      var block </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> Expression.Block(
          variablesToAdd.Select(v </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> v.Parameter),
          blockWithVariablesInitialization);
      </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> block;
    }
    ...</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>The following code is using the prepared references of our anonymous type and replaces them in the VisitMember() method. 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e4ff35b8-fefb-422e-b886-a6ce78750758 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">override</SPAN><SPAN style="COLOR: #000000"> Expression VisitMember(MemberExpression node)
{
  </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> ((node.Member.MemberType 
    </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> MemberTypes.Property)
      </SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000"> (node.Expression </SPAN><SPAN style="COLOR: #0000ff">is</SPAN><SPAN style="COLOR: #000000"> ParameterExpression))
    {
      Dictionary</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">, ParameterExpression</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">
        parameters </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">;
      </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (blockStartParameters.TryGetValue(
        node.Expression </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000"> ParameterExpression,
        </SPAN><SPAN style="COLOR: #0000ff">out</SPAN><SPAN style="COLOR: #000000"> parameters))
      </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> parameters[node.Member.Name];
    }
    </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">base</SPAN><SPAN style="COLOR: #000000">.VisitMember(node);
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>Now let’s see the result of this transformation. 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1d0d5287-75c3-4fae-881f-5be94af1fd8c class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> exp </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Start(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> { j </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000"> }, b </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
        Console.WriteLine(b.j)
    );</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>generates this expression : 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2c1517f5-9f83-4244-8ce3-061222ece034 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">.Lambda #Lambda1</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(System.Int32 $i) {
    .Call CSharp4Expressions.Block.Start(
        .New </SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">f__AnonymousType0`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32](</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">),
        .Lambda #Lambda2</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">f__AnonymousType0`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32]]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">)
}

.Lambda #Lambda2</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[</SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">f__AnonymousType0`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32]]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN style="COLOR: #000000">&lt;&gt;</SPAN><SPAN style="COLOR: #000000">f__AnonymousType0`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32] $b) {
    .Call System.Console.WriteLine($b.j)
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>which is transformed into : 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:77ba550e-3254-465d-b62c-c285768a4376 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">.Lambda #Lambda1</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.Int32]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(System.Int32 $i) {
    .Block() {
        .Block(System.Int32 $j) {
            $j </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">;
            .Call System.Console.WriteLine($j)
        }
    }
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>We finally compile it and it runs correctly 
<P mce_keep="true"><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_2.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_2.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb.png" width=391 height=183 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb.png"></A> <BR></P>
<P>Let’s test if we can nest declarations (variables local to child blocks).</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5aafdf91-0e07-4926-a2a6-6da726c4cf02 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> exp </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Start(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> { j </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000"> }, b </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
        Block.Start(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> { i </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">2</SPAN><SPAN style="COLOR: #000000"> }, b2 </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
            Console.WriteLine(b.j </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> b2.i)));
</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV><BR>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_4.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_4.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_1.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_1.png" width=411 height=163 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_1.png"></A> </P>
<P>Ok we now have block support and a way to declare variables. <BR>If reading is natural, we need to be able to assign values to those variables.</P>
<P>It’s a quite easy transformation. We first create an Assign() method in the Block class.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1b7da4e7-a516-42d8-8b36-156187dd45f2 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> Block 
{
  </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> Block Assign</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(Func</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> leftExpression, T rightExpression)
  {
    </SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> NotImplementedException();
  }
  ...</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>and then we catch it in our transformation engine replacing it with an Expression.Assign() 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:38e73466-275b-4921-807e-2e6fce75a819 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> Expression VisitBlockMethodCall(MethodCallExpression node)
{
  </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Block</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(node, </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Assign</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
  {
    var left </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> node.Arguments[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000">
      LambdaExpression;
    </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> Expression.Assign(
      Visit(left.Body),
      Visit(node.Arguments[</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">]));
  }
  ...</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>Let’s try it 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:63c75b56-1d21-4c0f-a806-8a014a496e8b class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> exp </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> i </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Start(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> { j </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000"> }, b </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
        Block.Default
            ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(b.j))
            .Assign(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> b.j, </SPAN><SPAN style="COLOR: #800080">2</SPAN><SPAN style="COLOR: #000000">)
            ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(b.j))
            );
</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>and we correctly get 
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_6.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_6.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_2.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_2.png" width=411 height=183 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/C4expressionsvariablesPartII_E198/image_thumb_2.png"></A> </P>
<P>Enough for this post !</P>
<P><A href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-loops-goto-label-if-and-even-for-part-iii.aspx" mce_href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-loops-goto-label-if-and-even-for-part-iii.aspx">Next article</A> will add Label, Goto, Loop, IfThenElse, While and For instructions.</P>
<P>The whole project is available here : <A title=http://code.msdn.microsoft.com/CSharp4Expressions/ href="http://code.msdn.microsoft.com/CSharp4Expressions/" mce_href="http://code.msdn.microsoft.com/CSharp4Expressions/">http://code.msdn.microsoft.com/CSharp4Expressions/</A></P>]]></content:encoded>
    </item>
    <item>
      <title>C# 4 expressions: blocks [Part I]</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/c-4-expressions-blocks-part-i</link>
      <pubDate>Tue, 02 Mar 2010 06:46:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2010/03/02/c-4-expressions-blocks-part-i/</guid>
      <description><![CDATA[&nbsp;
Since .Net 3.5 and Linq, the C# compiler is capable of generating expression trees instead...]]></description>
      <content:encoded><![CDATA[<P><IMG src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/97/15/32/LesArcs2010.jpg" original-url="http://blogs.msdn.com/photos/mitsu/images/9971532/original.aspx" mce_src="http://blogs.msdn.com/photos/mitsu/images/9971532/original.aspx">&nbsp;</P>
<P>Since .Net 3.5 and Linq, the C# compiler is capable of generating expression trees instead of standard executable IL. Even if Linq opens the door of meta-programming (using the code to define something else, like a Sql query), we still have a lot of limitations.</P>
<P>A C# expression is limited to a single instruction returning a value, given some parameters. Method calls, properties access, constructors, and operators are allowed but no block, no loops, etc…</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:77fc6bea-1182-4bb5-ab4e-7a0a2926cd7a class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> printExpression </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">
    s </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(s);
var Print </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> printExpression.Compile();
Print(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Hello !!!</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV><BR>
<P mce_keep="true">Of course, compiling the expression is not the only final goal but I will not go further in this article.</P>
<P>With .Net 4.0, Linq expressions implementation has moved to the DLR. The dynamic language runtime is using a richer expression API that allows a lot of thing. We can consider that C# 4.0 compiler is using a subset of the DLR expressions.</P>
<P>So, even if C# 4.0 now allows expressions based on Actions, we still have all the other limitations.</P>
<P>Therefore we can use the expression API programmatically to express more complex expressions like explained in this <A href="http://blogs.msdn.com/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx" target=_blank mce_href="http://blogs.msdn.com/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx">post</A> from Alexandra Rusina. But we won’t be able to express those complex expressions from the C# language.</P>
<P>Let’s try to find a work around for the first big barrier: statements…</P>
<P>The new expression API offers the BlockExpression class to define a block of statements. <BR>It’s quite easy to use since we just have to provide a list of expressions.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9b4763c1-314b-4646-a84f-c63b52bf0624 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> BlockExpression Block(</SPAN><SPAN style="COLOR: #0000ff">params</SPAN><SPAN style="COLOR: #000000"> Expression[] expressions);</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>We will notice that we can also provide a list of variables if needed but I will come back to this point a little bit later in this article. 
<P mce_keep="true">So the first very big restriction is we can only provide one single instruction ! <BR>Now imagine we use a syntax comparable to the one explained in this <A href="http://blogs.msdn.com/mitsu/archive/2008/10/22/treeselector-playing-with-generics-and-type-inference.aspx" mce_href="http://blogs.msdn.com/mitsu/archive/2008/10/22/treeselector-playing-with-generics-and-type-inference.aspx">article</A> where some instance methods always return the instance itself (this) so we can create a sequence of them.</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ba328e37-d858-4a23-b181-91633512e753 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> Block
{
    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> Block _(Action action)
    {
        </SPAN><SPAN style="COLOR: #0000ff">throw</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> NotImplementedException();
    }
    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> Block Default
        { </SPAN><SPAN style="COLOR: #0000ff">get</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">set</SPAN><SPAN style="COLOR: #000000">; }
</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>
<P>I know it’s very strange but I’ve chosen to name my method “_”. It’s authorized by C# and it’s legal :) <BR>I have also defined a Default property to avoid to have to create Block instances every time and because it’s an easy signature to recognize.</P>
<P>Now we can write things like:</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4bf9ae12-437d-488b-9163-48d0efafb09a class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> exp </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> s </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Default
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">I</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">would like</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">to say: </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(s));</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>Now the whole expression is correct because we have a single instruction but it defines a collection of actions and each of them contains a single instruction again. 
<P mce_keep="true">You can notice that there is only one “;” in my code.</P>
<P>Of course, my idea is to use this strange syntax to transform this expression into a BlockExpression. To achieve this I have to analyze my expression, find the Block.Default signature, remove it, and then extract the body from all the actions to finally get the collection of expressions to build my BlockExpression.</P>
<P>To do this, I have implemented an expression visitor. You can notice that the base class is now part of .Net (through the DLR once again): System.Linq.Expressions.ExpressionVisitor.</P>
<P>The visitor is a massively recursive code that helps you analyze all the possible nodes of an expression tree. For this first step, I will override the VisitMethodCall method to catch my sequence of Block methods.</P>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:596a3a6d-e613-4b40-8e99-7ef8005b6208 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">protected</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">override</SPAN><SPAN style="COLOR: #000000"> Expression VisitMethodCall(MethodCallExpression node)
{
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Block</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(node))
    {
        var expressions </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> List</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">();
        </SPAN><SPAN style="COLOR: #0000ff">do</SPAN><SPAN style="COLOR: #000000"> 
        {
            var r </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> VisitBlockMethodCall(
                node </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000"> MethodCallExpression);
            expressions.Insert(</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">, r);
            node </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> node.Object </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000">
                MethodCallExpression;
        } </SPAN><SPAN style="COLOR: #0000ff">while</SPAN><SPAN style="COLOR: #000000"> (node </SPAN><SPAN style="COLOR: #000000">!=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">);

        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> Expression.Block(expressions);
    }
    </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">base</SPAN><SPAN style="COLOR: #000000">.VisitMethodCall(node);
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>As method calls are in sequence, which is an unary operation, it’s possible to unrecursive this specific part of the visitor and that’s what I am doing in the do…while loop. 
<P>There is an important thing to know about method call sequences. The visitor is discovering them in the opposite order of the C# syntax.</P>
<P>For example, if I write :</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c4db9e47-fd03-44a2-bb4b-de5886c7e0ac class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">test.Do().Print();</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>We will be discovering Print() first, then Do(). It’s quite logical because (test.Do()) will be the source from which Print() is called. The loop is going up the sequence while the source (node.Object) is still a MethodCallExpression (node != null). You can notice that the extracted actions bodies are inserted on top of the list to recreate the C# syntax order. 
<P mce_keep="true">Of course we are doing all this work only if the method is declared by the Block type. The IsMethodOf helper method is used here.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e4858f8b-fb4c-473d-a2f4-9a2cec0e936c class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000"> IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(MethodCallExpression node, </SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000"> methodName)
{
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (node </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)
        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;
    </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> ((node.Method.DeclaringType 
        </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">typeof</SPAN><SPAN style="COLOR: #000000">(T))
        </SPAN><SPAN style="COLOR: #000000">&amp;&amp;</SPAN><SPAN style="COLOR: #000000"> (node.Method.Name </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> methodName));
}
</SPAN><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">bool</SPAN><SPAN style="COLOR: #000000"> IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">T</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(MethodCallExpression node)
{
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (node </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">null</SPAN><SPAN style="COLOR: #000000">)
        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">false</SPAN><SPAN style="COLOR: #000000">;
    </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> (node.Method.DeclaringType 
        </SPAN><SPAN style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">typeof</SPAN><SPAN style="COLOR: #000000">(T));
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>For this first step, I am only looking for the “_” method but I will later add more features to the Block class. That’s why I have isolated the methods recognition is a separated method :&nbsp; VisitBlockMethodCall. 
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52ca2fa8-3e28-4ca4-8d70-09e580dd5d99 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #c0c0c0; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">private</SPAN><SPAN style="COLOR: #000000"> Expression VisitBlockMethodCall(MethodCallExpression node)
{
    </SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000"> (IsMethodOf</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Block</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(node, </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">_</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> Visit((node.Arguments[</SPAN><SPAN style="COLOR: #800080">0</SPAN><SPAN style="COLOR: #000000">] </SPAN><SPAN style="COLOR: #0000ff">as</SPAN><SPAN style="COLOR: #000000">
            LambdaExpression).Body);
    ...
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>As I know the only argument is an Action (a lambda expression in our tree), I am just extracting the body and I do not forget to apply the Visitor on it before returning it (so the visitor logic can continue on this branch). 
<P mce_keep="true">Once I have collected all those expressions, I just have to build and return Expression.Block(expressions). '”Block.Default” is naturally skipped at this moment.</P>
<P><STRONG>Important point:</STRONG> all the Block members are not important in the end because there goal are to be removed by this transformation step. After the transformation, they must have all disappeared. I just consider them as markers for my transformation engine (metadata and not code). That’s also why they are never implemented (throw new NotImplementedException()). BUT whatever the transformation we make on the expression, it MUST respect the C# syntax in the first place.</P>
<P>Now we have to apply the visitor on our sample expression.</P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d68e4de-4493-46ab-b149-768077d25a14 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">Action</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&gt;&gt;</SPAN><SPAN style="COLOR: #000000"> expWithBlock </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> s </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000">
    Block.Default
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">I</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Would like</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">To say: </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">))
        ._(() </SPAN><SPAN style="COLOR: #000000">=&gt;</SPAN><SPAN style="COLOR: #000000"> Console.WriteLine(s));


expWithBlock </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> 
    ExpressionHelper.Translate(expWithBlock);
expWithBlock.Compile()(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Hello !!!</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV><BR>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:82f95ae9-726c-4806-960d-87c6e0c66c5c class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: silver; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> ExpressionHelper
{
    </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">TDelegate</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> Translate</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">TDelegate</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(Expression</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">TDelegate</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000"> expression)
    {
        var visitor </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> 
            </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> BlockCompilerVisitor</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">TDelegate</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">();
        </SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000"> visitor.StartVisit(expression);
    }
}
</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>and we get 
<P mce_keep="true"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/Multilineexpressionswith.Net4.0PartI_F411/image_3.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Multilineexpressionswith.Net4.0PartI_F411/image_3.png" width=411 height=203 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Multilineexpressionswith.Net4.0PartI_F411/image_3.png"> </P>
<P>I this very first step we managed to create multiline C# expressions based on actions.</P>
<P>Visual Studio 2010 has a very useful new debug viewer for expressions that you can call at debug time.</P>
<P>Here is our expression before… </P>
<P mce_keep="true">
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b50c1b8e-ecb4-4170-8bae-8f410b2372b9 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #ffff80; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">.Lambda #Lambda1</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.String]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(System.String $s) {
    .Call (.Call (.Call (.Call (CSharp4Expressions.Block.Default)._(.Lambda #Lambda2</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">))._(.Lambda #Lambda3</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">)
    )._(.Lambda #Lambda4</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">))._(.Lambda #Lambda5</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">)
} 

.Lambda #Lambda2</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">() {
    .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">I</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">)
} 

.Lambda #Lambda3</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">() {
    .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Would like</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">)
} 

.Lambda #Lambda4</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">() {
    .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">To say: </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">)
} 

.Lambda #Lambda5</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">() {
    .Call System.Console.WriteLine($s)
}
</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>…and after transformation. <BR>
<DIV style="PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; FLOAT: none; PADDING-TOP: 0px" id=scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:944d92c6-5e84-4754-a3bd-6d3065af77d3 class=wlWriterEditableSmartContent><PRE style="BACKGROUND-COLOR: #ffff80; WORD-WRAP: break-word; WHITE-SPACE: pre-wrap; OVERFLOW: auto"><SPAN style="COLOR: #000000">.Lambda #Lambda1</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">System.Action`</SPAN><SPAN style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000">[System.String]</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">(System.String $s) {
    .Block() {
        .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">I</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);
        .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">Would like</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);
        .Call System.Console.WriteLine(</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">To say: </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">);
        .Call System.Console.WriteLine($s)
    }
}</SPAN></PRE><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></DIV>
<P mce_keep="true">It’s not finished but enough for a single post.</P>
<P><A href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-variables-part-ii.aspx" mce_href="http://blogs.msdn.com/mitsu/archive/2010/03/02/c-4-expressions-variables-part-ii.aspx">Next part</A> will propose a solution for creating variables and then other expression API features like Loop, Goto, Label, Assign and even new features like For.</P>
<P>You can get the whole solution here: <A title=http://code.msdn.microsoft.com/CSharp4Expressions href="http://code.msdn.microsoft.com/CSharp4Expressions" mce_href="http://code.msdn.microsoft.com/CSharp4Expressions">http://code.msdn.microsoft.com/CSharp4Expressions</A></P>]]></content:encoded>
    </item>
    <item>
      <title>Read/Write selector in C# 3 then C# 4</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/readwrite-selector-in-c-3-then-c-4</link>
      <pubDate>Fri, 13 Nov 2009 12:26:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/11/13/readwrite-selector-in-c-3-then-c-4/</guid>
      <description><![CDATA[I needed some quiet time to finish this article so I took some vacations here (Mauritius...]]></description>
      <content:encoded><![CDATA[<P>I needed some quiet time to finish this article so I took some vacations here (Mauritius Island)</P>
<P><A title="Mauritius Island" href="http://www.flickr.com/photos/mitsufu/4100203350/" target=_blank mce_href="http://www.flickr.com/photos/mitsufu/4100203350/"><IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title="Mauritius Island" border=0 alt="Mauritius Island" src="http://farm3.static.flickr.com/2804/4100203350_f53b56f501.jpg" width=500 height=333 mce_src="http://farm3.static.flickr.com/2804/4100203350_f53b56f501.jpg"></A>&nbsp;</P>
<P>Expressions are used to evaluate something. We usually use them in the right side of an affectation or at any place where a value is expected. It can be composed with many other values, with computations or function calls.</P>
<P>When using objects we often retrieve values accessing properties or sub properties, defining property paths. This is what I will work on in this post.</P>
<P>Linq brought the Select method that we all know (one of my favorites).</P>
<P>We often use it to project from an entity to a new expression and many times we just use a property path.</P><PRE class=csharpcode>customers.Select(c =&gt; c.Company.Address.Country);</PRE>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>Those of you who are familiar to Xaml are very used to property paths in binding expressions:</P><PRE class=csharpcode><SPAN class=kwrd>&lt;</SPAN><SPAN class=html>TextBlock</SPAN> <SPAN class=attr>Text</SPAN>=”{<SPAN class=attr>Binding</SPAN> <SPAN class=attr>Path</SPAN>=<SPAN class=attr>Company</SPAN>.<SPAN class=attr>Address</SPAN>.<SPAN class=attr>Country</SPAN>}” <SPAN class=kwrd>/&gt;</SPAN></PRE>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>Even if there are some differences, we could easily compare those two features. <BR>One of the difference is WPF and Silverlight binding expressions can be “TwoWay”. This means that the property path expression can be used to both read or write the property value.</P>
<P>In C#, the selector is just pure code, so of course it compiles the natural access to the property (read only). Let’s think about how to make it possible to have a write access…</P>
<P>It’s not very difficult. If the expression only allows a sequence of chained properties, we just need to get the last one (Country in my example). Evaluating the rest of the expression (Company.Address) gives us the instance where we can get or set the last property value from.</P>
<P>To achieve this we need to be able to analyze the code which is not possible if it’s directly compiled into IL. The lambda syntax allows to use the same code to generate an expression instead of IL. Then we get an expression tree representing our property path, so we can analyze and understand it.</P>
<P>Let’s first try to do this in C# 3. Let’s imagine the final code we would like to have.</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>namespace</SPAN> CSharpRWSelector</PRE><PRE>{</PRE><PRE class=alt>    <SPAN class=kwrd>class</SPAN> Program</PRE><PRE>    {</PRE><PRE class=alt>        <SPAN class=kwrd>static</SPAN> <SPAN class=kwrd>void</SPAN> Main(<SPAN class=kwrd>string</SPAN>[] args)</PRE><PRE>        {</PRE><PRE class=alt>            var ps = PropertySelector&lt;Customer&gt;.Create(c =&gt; c.Address.Country);</PRE><PRE>&nbsp;</PRE><PRE class=alt>            var cust = <SPAN class=kwrd>new</SPAN> Customer();</PRE><PRE>            cust.Address.Country = <SPAN class=str>"FRANCE"</SPAN>;</PRE><PRE class=alt>&nbsp;</PRE><PRE>            Console.WriteLine(ps.GetValue(cust));</PRE><PRE class=alt>&nbsp;</PRE><PRE>            ps.SetValue(cust, <SPAN class=str>"FRANCE !"</SPAN>);</PRE><PRE class=alt>&nbsp;</PRE><PRE>            Console.WriteLine(cust.Address.Country);</PRE><PRE class=alt>        }</PRE><PRE>    }</PRE><PRE class=alt>&nbsp;</PRE><PRE>    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>class</SPAN> Customer</PRE><PRE class=alt>    {</PRE><PRE>        <SPAN class=kwrd>public</SPAN> Customer()</PRE><PRE class=alt>        {</PRE><PRE>            Address = <SPAN class=kwrd>new</SPAN> Address();</PRE><PRE class=alt>        }</PRE><PRE>        <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>string</SPAN> ID { get; set; }</PRE><PRE class=alt>        <SPAN class=kwrd>public</SPAN> Address Address { get; set; }</PRE><PRE>    }</PRE><PRE class=alt>    <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>class</SPAN> Address</PRE><PRE>    {</PRE><PRE class=alt>        <SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>string</SPAN> Country { get; set; }</PRE><PRE>    }</PRE><PRE class=alt>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>
<PRE class=csharpcode>&nbsp;</PRE>
<P>Let’s imagine a PropertySelector class with a Create() method having the same prototype as the Linq Select().</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>static</SPAN> PropertySelector&lt;T, R&gt; Create&lt;R&gt;(Expression&lt;Func&lt;T, R&gt;&gt; source)</PRE><PRE>{</PRE><PRE>&nbsp;</PRE><PRE class=alt>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>You can notice that the Func&lt;T, R&gt; is surrounded with the Expression&lt;T&gt; class. <BR>The use is exactly the same but we will get an Lambda expression instead of just a delegate reference.</P>
<P>First of all, getting a selector for reading is easy. We just need to compile the expression (source.Compile()) and we retrieve the right selector (Func&lt;T,R&gt;).</P>
<P>All the following is to get a way to write to the property.</P>
<P>Now let’s try to analyze this expression to retrieve the instance just before the last property of our property path.</P>
<P>We will have to implement a custom expression visitor. (see MSDN reference <A href="http://msdn.microsoft.com/en-us/library/bb882521.aspx" target=_blank mce_href="http://msdn.microsoft.com/en-us/library/bb882521.aspx">here</A>)</P>
<P>I will quickly comment this part, but all the code is provided at the end of this post.</P>
<P>The expression visitor is massively recursive and is walking along all the expression tree.</P>
<P>We are only looking for properties (ie MemberAccess) and our goal is to catch the last one’s parent ! What I am mainly doing here is finding it and then wait for the next call to get the parent. You can notice that the properties of the path are discovered from right to left (last to first).</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>protected</SPAN> <SPAN class=kwrd>override</SPAN> Expression VisitMemberAccess(MemberExpression m)</PRE><PRE>{</PRE><PRE class=alt><P>    <SPAN class=kwrd>if</SPAN> ((m.Member.MemberType == System.Reflection.MemberTypes.Property) &amp;&amp; <BR>        (!passed))</P></PRE><PRE>    {</PRE><PRE class=alt>        var lambda = Expression.Lambda(m.Expression, source.Parameters.ToArray());</PRE><PRE>        instanceSelector = lambda.Compile();</PRE><PRE class=alt>        propertyName = m.Member.Name;</PRE><PRE>        passed = <SPAN class=kwrd>true</SPAN>;</PRE><PRE class=alt>    }</PRE><PRE>    <SPAN class=kwrd>return</SPAN> <SPAN class=kwrd>base</SPAN>.VisitMemberAccess(m);</PRE><PRE class=alt>}</PRE></DIV>
<P>Once we get the parent (Address) we keep this reduced lamba expression (without the last property), we compile it and then we get a selector on the parent.</P>
<P>We can now implement the SetValue() method, using the selector to retrieve the parent instance given the source. Then, using reflection, we can retrieve the PropertyInfo and call the SetValue().</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> SetValue(T source, R <SPAN class=kwrd>value</SPAN>)</PRE><PRE>{</PRE><PRE class=alt>    var parent = instanceSelector.DynamicInvoke(source);</PRE><PRE>    var prop = parent.GetType().GetProperty(propertyName);</PRE><PRE class=alt>    prop.SetValue(parent, <SPAN class=kwrd>value</SPAN>, <SPAN class=kwrd>null</SPAN>);</PRE><PRE>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>As I said previously, the GetValue() is easy to implement. We just need to call the initial selector.</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>public</SPAN> R GetValue(T source)</PRE><PRE>{</PRE><PRE class=alt>    <SPAN class=kwrd>return</SPAN> selector(source);</PRE><PRE>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>I will also add some security code to be sure that only property paths are allowed.</P>
<P>code like c =&gt; c.SomeMethod().Property; must be rejected.</P>
<P>The expression visitor Visit() method is called for each node whatever its type. So it’s the perfect place to only accept property paths.</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>protected</SPAN> <SPAN class=kwrd>override</SPAN> Expression Visit(Expression exp)</PRE><PRE>{</PRE><PRE class=alt>    <SPAN class=kwrd>if</SPAN> ((exp.NodeType != ExpressionType.Parameter) &amp;&amp;</PRE><PRE>        (exp.NodeType != ExpressionType.Lambda) &amp;&amp;</PRE><PRE class=alt>        ((exp.NodeType != ExpressionType.MemberAccess) </PRE><PRE>          &amp;&amp; ((exp <SPAN class=kwrd>as</SPAN> MemberExpression).Member.MemberType </PRE><PRE class=alt>          == System.Reflection.MemberTypes.Property)))</PRE><PRE>        <SPAN class=kwrd>throw</SPAN> <SPAN class=kwrd>new</SPAN> ArgumentException(<SPAN class=str>"Only Properties are allowed with PropertyPath"</SPAN>);</PRE><PRE class=alt>    <SPAN class=kwrd>return</SPAN> <SPAN class=kwrd>base</SPAN>.Visit(exp);</PRE><PRE>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>Unfortunately, we have no way to check this at compile time.</P>
<P>Last but not least, let’s add an interesting improvement using C# 4.</P>
<P>In this first sample, you can notice that the GetValue is efficient because it’s a real ‘compiled’ selector but the SetValue is using reflection.</P>
<P>As explained in this<A href="http://blogs.msdn.com/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx" mce_href="http://blogs.msdn.com/csharpfaq/archive/2009/09/14/generating-dynamic-methods-with-expression-trees-in-visual-studio-2010.aspx"> very interesting post</A>, C# 4 has new lambda expression features. Actually, not in the C# language itself but in the lambda expression APIs.</P>
<P>Affectation of a value was impossible to be described in a C# 3 lambda expression and it’s still not in C# 4, but we can build this expression tree bye code as the Expression.Assign() method now exists.</P>
<P>So now the goal is still to provide a Write access to my selector.</P>
<P>The visitor is now more complex as we want to make a real transformation of the tree.</P>
<P>From a selector Func&lt;T, R&gt; we want to infer an Action&lt;T, R&gt;.</P>
<P>The following code is very symbolic as it does not compiles but it gives a good idea of what we will try to generate:</P>
<P>From the analysis of</P>
<P>Func&lt;Customer, string&gt; f = c =&gt; c.Address.Country;</P>
<P>We will generate</P>
<P>Action&lt;Customer, string&gt; a = (cust, country) =&gt; cust.Address.Country = country;</P>
<P>Let’s modify the visitor:</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>protected</SPAN> <SPAN class=kwrd>override</SPAN> Expression VisitMemberAccess(MemberExpression m)</PRE><PRE>{</PRE><PRE class=alt>    var result = <SPAN class=kwrd>base</SPAN>.VisitMemberAccess(m);</PRE><PRE>&nbsp;</PRE><PRE class=alt>    currentNode = Expression.MakeMemberAccess(currentNode, m.Member);</PRE><PRE>&nbsp;</PRE><PRE class=alt>    <SPAN class=kwrd>return</SPAN> result;</PRE><PRE>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>In this first step, I am building a parallel expression tree copying the property path definition, excepted the lambda root.</P>
<P>We now need to build our Action&lt;T, R&gt;.</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>public</SPAN> PropertySelector&lt;T, R&gt; Compile(Expression&lt;Func&lt;T, R&gt;&gt; source)</PRE><PRE>{</PRE><PRE class=alt>    <SPAN class=kwrd>this</SPAN>.source = source;</PRE><PRE>&nbsp;</PRE><PRE class=alt>    parameters = <SPAN class=kwrd>new</SPAN> List&lt;ParameterExpression&gt;();</PRE><PRE>&nbsp;</PRE><PRE class=alt>    Visit(source);</PRE><PRE>&nbsp;</PRE><PRE class=alt>    var valueParameter = Expression.Parameter(<SPAN class=kwrd>typeof</SPAN>(R), <SPAN class=str>"value"</SPAN>);</PRE><PRE>    parameters.Add(valueParameter);</PRE><PRE class=alt>    currentNode = Expression.Assign(currentNode, valueParameter);</PRE><PRE>    var setter = Expression.Lambda(<SPAN class=kwrd>typeof</SPAN>(Action&lt;T, R&gt;), currentNode, parameters);</PRE><PRE class=alt>&nbsp;</PRE><PRE>    var result = <SPAN class=kwrd>new</SPAN> PropertySelector&lt;T, R&gt;() { </PRE><PRE class=alt>        selector = source.Compile(), setter = (Action&lt;T, R&gt;)setter.Compile() };</PRE><PRE>&nbsp;</PRE><PRE class=alt>    <SPAN class=kwrd>return</SPAN> result;</PRE><PRE>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>After running the visitor (Visit() method), I am retrieving the property path from currentNode and I am building the affectation with Expression.Assign().</P>
<P>Compared to the Func&lt;T,R&gt;, I also need to add a new R parameter which is the value to affect. (see parameters.Add(valueParameter); ).</P>
<P>Finally, a lambda expression groups all this stuff to create the Action&lt;T,R&gt;.</P>
<P>We can compile it and get the corresponding delegate (setter) which is real code instead of the reflection we’ve used in the C# 3 version.</P>
<P>We now just need to call it from the SetValue() method.</P>
<DIV class=csharpcode><PRE class=alt><SPAN class=kwrd>public</SPAN> <SPAN class=kwrd>void</SPAN> SetValue(T source, R <SPAN class=kwrd>value</SPAN>)</PRE><PRE>{</PRE><PRE class=alt>    setter(source, <SPAN class=kwrd>value</SPAN>);</PRE><PRE>    <SPAN class=rem>//var parent = instanceSelector.DynamicInvoke(source);</SPAN></PRE><PRE class=alt>    <SPAN class=rem>//var prop = parent.GetType().GetProperty(propertyName);</SPAN></PRE><PRE>    <SPAN class=rem>//prop.SetValue(parent, value, null);</SPAN></PRE><PRE class=alt>}</PRE></DIV>
<STYLE type=text/css>.csharpcode {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	BACKGROUND-COLOR: #ffffff; FONT-FAMILY: consolas, "Courier New", courier, monospace; COLOR: black; FONT-SIZE: small
}
.csharpcode PRE {
	MARGIN: 0em
}
.csharpcode .rem {
	COLOR: #008000
}
.csharpcode .kwrd {
	COLOR: #0000ff
}
.csharpcode .str {
	COLOR: #006080
}
.csharpcode .op {
	COLOR: #0000c0
}
.csharpcode .preproc {
	COLOR: #cc6633
}
.csharpcode .asp {
	BACKGROUND-COLOR: #ffff00
}
.csharpcode .html {
	COLOR: #800000
}
.csharpcode .attr {
	COLOR: #ff0000
}
.csharpcode .alt {
	BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; WIDTH: 100%
}
.csharpcode .lnum {
	COLOR: #606060
}
</STYLE>

<P>The main application remains the same and we of course have the same result but we know use a dynamic solution by compiling code thanks to new C#4 expression tree features !</P>
<P>You can find the two relative solutions here for C# 3/VS2008 and C# 4/VS2010.</P>
<P>Mitsu</P><IFRAME style="PADDING-BOTTOM: 0px; BACKGROUND-COLOR: #fcfcfc; PADDING-LEFT: 0px; WIDTH: 98px; PADDING-RIGHT: 0px; HEIGHT: 115px; PADDING-TOP: 0px" title=Preview marginHeight=0 src="http://cid-b1ef19515cad3e56.skydrive.live.com/embedicon.aspx/.Public/CSharpRWSelector.zip" frameBorder=0 marginWidth=0 scrolling=no mce_src="http://cid-b1ef19515cad3e56.skydrive.live.com/embedicon.aspx/.Public/CSharpRWSelector.zip"></IFRAME>]]></content:encoded>
    </item>
    <item>
      <title>Surface Academy Toolkit 2009</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/surface-academy-toolkit-2009</link>
      <pubDate>Tue, 08 Sep 2009 07:40:20 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/09/08/surface-academy-toolkit-2009/</guid>
      <description><![CDATA[Just a quick post to share this project with you.   Microsoft France organized a 2 month long...]]></description>
      <content:encoded><![CDATA[<p><a href="http://surfaceacademy2009.codeplex.com/"><img border="0" src="http://download.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=surfaceacademy2009&amp;DownloadId=82414&amp;Build=15691" /></a></p>  <p>Just a quick post to share this project with you.   <br />Microsoft France organized a 2 month long internship in Paris to develop a Surface Toolkit.</p>  <p>We have provided a bunch of controls, including a Card Game Surface Starter kit that you can see here:</p>  <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:6346ae0a-2e8e-4916-962f-f5a979c56ae8" class="wlWriterEditableSmartContent"><div id="994a4152-9850-456b-a225-2fb1df93c075" style="margin: 0px; padding: 0px; display: inline;"><div><a href="https://www.youtube.com/watch?v=B8m26mJnPKk" target="_new"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/SurfaceAcademyToolkit2009_C038/videod730a3e49db8.jpg" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/SurfaceAcademyToolkit2009_C038/videod730a3e49db8.jpg" style="border-style: none" galleryimg="no" onload="var downlevelDiv = document.getElementById('994a4152-9850-456b-a225-2fb1df93c075'); downlevelDiv.innerHTML = &quot;&lt;div&gt;&lt;object width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;param name=\&quot;movie\&quot; value=\&quot;https://www.youtube.com/v/B8m26mJnPKk&amp;hl=en\&quot;&gt;&lt;\/param&gt;&lt;embed src=\&quot;https://www.youtube.com/v/B8m26mJnPKk&amp;hl=en\&quot; type=\&quot;application/x-shockwave-flash\&quot; width=\&quot;425\&quot; height=\&quot;355\&quot;&gt;&lt;\/embed&gt;&lt;\/object&gt;&lt;\/div&gt;&quot;;" alt=""></a></div></div></div>  <p>The project on Codeplex: <a href="http://surfaceacademy2009.codeplex.com/">http://surfaceacademy2009.codeplex.com/</a></p>]]></content:encoded>
    </item>
    <item>
      <title>Serving Silverlight Apps from Windows Mobile</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/serving-silverlight-apps-from-windows-mobile</link>
      <pubDate>Thu, 02 Jul 2009 05:35:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/07/02/serving-silverlight-apps-from-windows-mobile/</guid>
      <description><![CDATA[For the last Paris mobility briefing, my colleague Pierre Cauchois asked me to co-animate the...]]></description>
      <content:encoded><![CDATA[<P>For the last Paris mobility briefing, my colleague <A href="http://blogs.msdn.com/pierreca/" target=_blank mce_href="http://blogs.msdn.com/pierreca/">Pierre Cauchois</A> asked me to co-animate the Coding4Fun session…hard to refuse.</P>
<P>Even if mobile dev is not my every day work, thanks to the .Net Compact Framework, it’s still .Net programming.</P>
<P>Here is the scenario:</P>
<P>You come back home, you have a windows mobile phone wifi capable and you want to quickly get access to your phone pictures from your home network. <BR>You just activate the Wifi, run my app and then browse to the provided link from any computer on the network.</P>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_4.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_4.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_1.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_1.png" width=318 height=195 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_1.png"></A> </P>
<P>Then a silverlight application is loaded from the phone and you can see any of your pictures.</P>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_6.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_6.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_2.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_2.png" width=639 height=572 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_2.png"></A> </P>
<P>Here is how it works:</P>
<P>I decided to use windows mobile to host a Silverlight application.</P>
<P>I had just needed a Mobile Web Server that I found <A href="http://bansky.net/blog/2007/10/compact-web-server-in-compact-framework/" target=_blank mce_href="http://bansky.net/blog/2007/10/compact-web-server-in-compact-framework/">here</A> (thanks to <A href="http://bansky.net/blog" mce_href="http://bansky.net/blog">Pavel Bánský</A>). The code is full C# and really small and easy to use. I just had to add cookie support but I will talk about it later.</P>
<P>Now I have a basic web server hosted on my phone, I will publish some stuff.</P>
<P>- the main html page containing the silverlight application. I renamed the one provided by Visual Studio to “Default.html” to make it the default page of my web site.</P>
<P>- the xap file which is the Silverlight application itself.</P>
<P>- a web service to expose the list of pictures available on my phone. As I had no web service infrastructure available on my very small web server, I have decided to expose a rss feed which is made with just a few lines of Linq to Xml. Exposing pictures on http gives a lot of advantages like getting the benefits of the browser local cache. In this project it’s very useful because the mobile phone does not have a large and powerful bandwidth, so we appreciate not to reload pictures ever time. Now we just need to link some Silverlight Image controls to the pictures urls and the Silverlight image loader will work for us.</P>
<P>- make the pictures visible. The web server contains a virtual folder feature. I just had to make it point to the local pictures path.</P>
<P><U>Linq to Xml rss content creation on server side (Compact Framework):</U></P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>files</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#2b91af>Directory</FONT><FONT color=#000000>.GetFiles(picturesPath,</FONT><FONT color=#808080> "*.jpg"</FONT><FONT color=#000000>)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR><BR></FONT><FONT color=#000000>XElement</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>response</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"rss"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XAttribute(</FONT><FONT color=#808080>"version"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "2.0"</FONT><FONT color=#000000>),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"channel"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"title"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "Mobile pictures"</FONT><FONT color=#000000>),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"description"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "Mobile pictures from my phone"</FONT><FONT color=#000000>),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"link"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> ""</FONT><FONT color=#000000>),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>(</FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>file</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>files.Select(f</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#2b91af>Path</FONT><FONT color=#000000>.GetFileName(f))</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"item"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"title"</FONT><FONT color=#000000>,</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>file),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"description"</FONT><FONT color=#000000>,</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>file),</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement(</FONT><FONT color=#808080>"link"</FONT><FONT color=#000000>,</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>.Format(</FONT><FONT color=#808080>"http://{0}/photos/{1}"</FONT><FONT color=#000000>,ipAddress,file))</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>)</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>).ToArray()</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>)</FONT><FONT color=#808080> <BR></FONT><FONT color=#000000>)</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>Regarding the client development, I have created a simple Silverlight 3 application (yes I also wanted to play with those new features like 3D and non linear animations).</P>
<P>The Silverlight application calls the web service, reads the rss content (using Linq to Xml again !) and binds the pictures paths to a listbox. The selected picture is displayed in the biggest part of the window. When you change the selected picture, an animation wipes out the existing one and another is bringing the new one with a 3D effect.</P>
<P><U>Analysing rss content with Linq to Xml on client side (Silverlight):</U></P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>root</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>XElement.Parse(e.Result)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR><BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>items</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>root.Element(</FONT><FONT color=#808080>"channel"</FONT><FONT color=#000000>).Elements(</FONT><FONT color=#808080>"item"</FONT><FONT color=#000000>).Select(i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i.Element(</FONT><FONT color=#808080>"link"</FONT><FONT color=#000000>).Value)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#000000>lb.ItemsSource</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>items</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>And that’s it !</P>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_8.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_8.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_8.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_3.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_3.png" width=520 height=365 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_3.png"></A> </P>
<P>Now let’s talk about security. Once again, it’s just a little demo but we can not talk about opening a web server on your phone without talking about security.</P>
<P>The web server is made to respond to the user for a short time. https is complex and even basic authentication needs credentials on the server side. I have chosen another solution that I found to be more appropriate. In my scenario, the web server is requested by a single user who’s got the phone in his hands. So when a first request comes (no cookie), I am asking for a human validation (MessageBox). Of course during this time the server is stopped. The user just has to validate on the phone and then the server answers and provides an authentication cookie. Next requests will carry the cookie and the user will not have to validate again.</P>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_10.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_10.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_10.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_4.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_4.png" width=292 height=256 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WindowsMobileSilverlighthosting_112BD/image_thumb_4.png"></A> </P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P mce_keep="true">&nbsp;</P>
<P>This remains a really really small demo. We could of course imagine having a more robust web server and some optimizations like thumbnails creation on server side but this was just a test and I hope it gave some interesting ideas to some of you.</P>
<P>In addition to hosting a Silverlight application on my mobile phone, the funniest was to be able to use .Net both on server and client side (Compact Framework and Silverlight) without using the Windows .Net Framework !</P>
<P>The source code is attached to this post.</P><IFRAME style="BORDER-BOTTOM: #dde5e9 1px solid; BORDER-LEFT: #dde5e9 1px solid; PADDING-BOTTOM: 0px; BACKGROUND-COLOR: #ffffff; MARGIN: 3px; PADDING-LEFT: 0px; WIDTH: 240px; PADDING-RIGHT: 0px; HEIGHT: 66px; BORDER-TOP: #dde5e9 1px solid; BORDER-RIGHT: #dde5e9 1px solid; PADDING-TOP: 0px" marginHeight=0 src="http://cid-7bd91ae7f5096b79.skydrive.live.com/embedrowdetail.aspx/Public/BlogMitsu/WindowsMobileSilverlightHosting.zip" frameBorder=0 marginWidth=0 scrolling=no mce_src="http://cid-7bd91ae7f5096b79.skydrive.live.com/embedrowdetail.aspx/Public/BlogMitsu/WindowsMobileSilverlightHosting.zip"></IFRAME>]]></content:encoded>
    </item>
    <item>
      <title>Linq: how to share parameters between lambda expressions ?</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/linq-how-to-share-parameters-between-lambda-expressions</link>
      <pubDate>Mon, 18 May 2009 15:38:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/05/18/linq-how-to-share-parameters-between-lambda-expressions/</guid>
      <description><![CDATA[Before going into Linq, here is again one of my pictures: Le Louvre by night, Paris
 
&nbsp;
When...]]></description>
      <content:encoded><![CDATA[<P>Before going into Linq, here is again one of my pictures: Le Louvre by night, Paris</P>
<P><A href="http://farm4.static.flickr.com/3489/3814713962_04493936b9_o.jpg" mce_href="http://farm4.static.flickr.com/3489/3814713962_04493936b9_o.jpg"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_1.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_1.png" width=404 height=215 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_1.png"></A> </P>
<P mce_keep="true">&nbsp;</P>
<P>When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it’s not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another. <BR>The compiler is using two technical different ways to let parameters go out of a method.</P>
<P>As an example, let’s first see how the .SelectMany() method is working.</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "1"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "2" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "A"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "B"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "C" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values1</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values2</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>+</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s2</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_6.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_6.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_6.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" title=image border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_2.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_2.png" width=369 height=159 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/Linqparameters_D8E7/image_thumb_2.png"></A> </P>
<P>This very little example shows that s1 and s2 are both accessible in the select. It’s nice, but how does this work ? <BR>You must know that the ‘from’ statement of the Linq sugar syntax does not match any existing Linq method. Let’s see how we would have written this using the classical C# syntax.</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "1"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "2" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "A"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "B"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "C" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>values1.SelectMany(s1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values2.Select(s2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>+</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s2))</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>Let’s focus on the SelectMany parameter:</P>
<P>SelectMany(Func&lt;TSource, IEnumerable&lt;TResult&gt;&gt; selector).</P>
<P>The method must return an IEnumerable&lt;TResult&gt;. In our example, we get it using values2.Select(). We are now touching the interesting point. The parameter of the .Select() is also a lambda “s2 =&gt; s1 + s2”. In Linq to object a lamda generates an anonymous method and anonymous methods can access their host scope. Here, s1 is visible inside values2.Select().</P>
<P>So the first solution to share parameters between methods is to nest them</P>
<P>like “.SelectMany(s =&gt; s.Select(s2 =&gt;’s in accesible here’ ))”</P>
<P>instead of creating a sequence “source.Skip(‘scope1’).Take(‘scope2’)”</P>
<P>Now what if we would like to share a parameter between two following methods like Skip() and Take() ?</P>
<P>Let’s see how the “let” keyword works.</P>
<P>In the next example, I will start from a list of numbers stored as strings. <BR>I would like to get only numbers greater than 10 and order them by their value but keeping the result as an enumeration of string.</P>
<P>We can write it quickly this way:</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "12"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "4"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "7"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "18"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "32" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>where</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>orderby</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>This works fine but we all notice the ugly “Convert.ToInt32(s)” used twice on the same value. Thanks to the “let” keyword we can create a new parameter that will be accessible in all the query.</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>string</FONT><FONT color=#000000>[]</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080> "12"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "4"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "7"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "18"</FONT><FONT color=#000000>,</FONT><FONT color=#808080> "32" </FONT><FONT color=#000000>}</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>let</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>where</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>orderby</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>We can see that the “let” keyword solves this problem extremely easily. But once again the sugar syntax of Linq is really magic. As we always want to know the secrets of magic tricks, let’s try to get the same result without using the “let” keyword and we will find out what the C# compiler is really doing.</P>
<P>Let’s start by decomposing all the steps using the regular C# syntax.</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values.Where(s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#000000>)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080> <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step1.OrderBy(s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s))</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>I have chosen to decompose this way to show that the only place to share something between the Where() and the OrderBy() is the result.</P>
<P>Now we have to make a new parameter travel across the sequence in addition to the current result. The idea is to create a new type to group the current result and the new value. To achieve this easily we can just use a anonymous type.</P>
<DIV class=code><FONT color=#006400>//step0: Create an anonymous type grouping the original string and the new value in a single element&nbsp; <BR>//step1, step2: 'i' is now accessible as a property of each element&nbsp; <BR>//step3: Get back to an enumeration of string by removing the temporary anonymous element&nbsp; <BR></FONT><FONT color=#808080><BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step0</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values.Select(s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s,</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>})</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080>&nbsp; <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step1</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step0.Where(tmp</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#000000>)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080>&nbsp; <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step2</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step1.OrderBy(tmp</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.i)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080>&nbsp; <BR></FONT><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step3</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>step2.Select(tmp</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.s)</FONT><FONT color=#0000ff>;</FONT><FONT color=#808080>&nbsp;</FONT> </DIV>
<P>We can of course write this in a single Linq query</P>
<DIV class=code><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>(</FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>new</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>{</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>S</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s,</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>I</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>})</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>where</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.I</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#808080>&nbsp; <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>orderby</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.I</FONT><FONT color=#808080>&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>tmp.S</FONT><FONT color=#0000ff>;</FONT> </DIV>
<P>which is the exact equivalent of what is compiled when using the ‘let’ keyword</P><FONT color=#0000ff>var</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>q</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>from</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>in</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>values</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#000000>let</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#0000ff>=</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>Convert.ToInt32(s)</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>where</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>&gt;</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#800000>10</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>orderby</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>i</FONT><FONT color=#808080> <BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=#0000ff>select</FONT><FONT color=#808080>&nbsp;</FONT><FONT color=#000000>s</FONT><FONT color=#0000ff>;</FONT> 
<P>Of course the Linq syntax is extremely short but it’s always nice to know what’s happening behind the scene.</P>
<P>I hope this sample can also help you to solve some other problems when playing with Linq.</P>]]></content:encoded>
    </item>
    <item>
      <title>WPF/Surface demo: driving an extending desktop with a popfly duck !</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/wpfsurface-demo-driving-an-extending-desktop-with-a-popfly-duck</link>
      <pubDate>Mon, 09 Mar 2009 09:53:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/03/09/wpfsurface-demo-driving-an-extending-desktop-with-a-popfly-duck/</guid>
      <description><![CDATA[Contrarily to the title, this is a serious article !
From 10th to 12th of February Microsoft...]]></description>
      <content:encoded><![CDATA[<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_4.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_4.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_4.png"><IMG style="BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; BORDER-TOP: 0px; BORDER-RIGHT: 0px" border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb_1.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb_1.png" width=298 height=246 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb_1.png"></A> </P>
<P>Contrarily to the title, this is a serious article !</P>
<P>From 10th to 12th of February Microsoft France have organized the Paris Techdays event. <BR>During the developer keynote, I have presented some Surface development features with a funny demo.</P>
<P>Here is a video of this demo but in french.</P>
<P><IFRAME style="WIDTH: 500px; HEIGHT: 400px" src="http://silverlight.services.live.com/invoke/62703/SurfaceTechdays/iframe.html?autoplay=0" frameBorder=0 scrolling=no mce_src="http://silverlight.services.live.com/invoke/62703/SurfaceTechdays/iframe.html?autoplay=0"></IFRAME></P>
<P>Now let me explain what happened.</P>
<P><STRONG>Here is the idea:</STRONG></P>
<P>Get a Surface table, setup a windows extended desktop using the VGA output of the table and an external display. The Surface application is very simple and similar to the very classical application where many people can move, rotate and resize pictures. In our case, pictures were slides exported from Powerpoint. When the applications runs, it creates a classical WPF window (not Surface specific) and put it on the secondary display, in a maximized state. <BR>The goal is to output a defined part of what we have on the table on the external display. I have choose to use Tag to define this part (a rectangle) on the table. Placing the tag will define the center of this output rectangle. Then you can change the position of the output rectangle by moving the tag. As the Surface also recognizes the orientation, I have used the delta orientation value to increase or decrease the output rectangle size depending on the direction you are turning the tag (clockwise/counter clockwise).</P>
<P>As many people (including me :-) ) can't test this application at home, I have simulated feature in a WPF application using mouse move and mouse wheel events.</P>
<P><STRONG>How to implement it:</STRONG></P>
<P>It's quite fast to implement but let's see the interesting points.</P>
<P><A href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_2.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_2.png" mce_href="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_2.png"><IMG style="BORDER-RIGHT-WIDTH: 0px; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px" border=0 alt=image src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb.png" original-url="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb.png" width=505 height=309 mce_src="http://blogs.msdn.com/blogfiles/mitsu/WindowsLiveWriter/WPFSurfacedemodrivinganextendingdesktopw_83AC/image_thumb.png"></A> </P>
<P>First, how to capture the targeted visual part ? We will use a WPF VisualBrush. <BR>This component is a Brush, comparable to a image brush, a plain color or a gradient brush but will use another WPF Visual as for painting. What you can notice is that the VisualBrush is not based on a kind of bitmap capture but is redrawing the visual using the wpf vector information. This means that if the VisualBrush is growing the original size, you will still keep a sharp rendered image keeping the benefits of the vector based drawing.</P>
<P>So the output window is simply defined this way:</P>
<DIV>
<DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&lt;Window x:Class=<SPAN style="COLOR: #006080">"SurfaceMedia.OutputWindow"</SPAN></PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    xmlns=<SPAN style="COLOR: #006080">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</SPAN></PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    xmlns:x=<SPAN style="COLOR: #006080">"http://schemas.microsoft.com/winfx/2006/xaml"</SPAN></PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    Title=<SPAN style="COLOR: #006080">"OutputWindow"</SPAN> Height=<SPAN style="COLOR: #006080">"300"</SPAN> Width=<SPAN style="COLOR: #006080">"300"</SPAN>&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;Window.Background&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        &lt;VisualBrush x:Name=<SPAN style="COLOR: #006080">"vb"</SPAN> /&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;/Window.Background&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&lt;/Window&gt;</PRE></DIV></DIV>
<P>Now we need to define which part of the main window we want to output. <BR>We will surround our main content with a grid and then put a Canvas over it. Then we can define a Rectangle in our canvas to visually define our output region. <BR>No the problem is we have our Rectangle over the main content but we want our main content to still receive inputs (mouse, keyboard) and we want the Rectangle to respond to some mouse events.</P>
<P>To achieve this, we will define the Canvas IsHitTestVisible attribute to "False". Easy to do but now the Rectangle will no longer receive mouse events. <BR>In our surrounding grid, we will catch the needed mouse events. As the main content can potentially stop the bubble events from going up to their parents, we will prefer using PreviewMouseMove and PreviewMouseWheel.</P>
<DIV>
<DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&lt;Grid PreviewMouseMove=<SPAN style="COLOR: #006080">"Canvas_MouseMove"</SPAN> PreviewMouseWheel=<SPAN style="COLOR: #006080">"Grid_MouseWheel"</SPAN>&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;Grid&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        ...main content</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;/Grid&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;Canvas x:Name=<SPAN style="COLOR: #006080">"canvas"</SPAN> IsHitTestVisible=<SPAN style="COLOR: #006080">"False"</SPAN>&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        &lt;Rectangle x:Name=<SPAN style="COLOR: #006080">"outputRect"</SPAN> Width=<SPAN style="COLOR: #006080">"100"</SPAN> Height=<SPAN style="COLOR: #006080">"100"</SPAN> Stroke=<SPAN style="COLOR: #006080">"Black"</SPAN></PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">            StrokeThickness=<SPAN style="COLOR: #006080">"1"</SPAN> Opacity=<SPAN style="COLOR: #006080">"0.5"</SPAN> Canvas.Left=<SPAN style="COLOR: #006080">"6"</SPAN> Canvas.Top=<SPAN style="COLOR: #006080">"72"</SPAN> /&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    &lt;/Canvas&gt;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&lt;/Grid&gt;</PRE></DIV></DIV>
<P><BR>Now we get what we wanted. We just need to adjust the VisualBrush playing on its Viewbox property to define exactly which part of the source we want to display.</P>
<DIV>
<DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> Canvas_MouseMove(<SPAN style="COLOR: #0000ff">object</SPAN> sender, MouseEventArgs e)</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">{</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    RefreshOutputWindow(e);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">}</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> Grid_MouseWheel(<SPAN style="COLOR: #0000ff">object</SPAN> sender, MouseWheelEventArgs e)</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">{</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    <SPAN style="COLOR: #0000ff">if</SPAN> ((outputRect.Width &lt; 40) &amp;&amp; (e.Delta &lt; 0))</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        <SPAN style="COLOR: #0000ff">return</SPAN>;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputRect.Width += e.Delta / 10;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputRect.Height += e.Delta / 10;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    RefreshOutputWindow(e);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">}</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> RefreshOutputWindow(MouseEventArgs e)</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">{</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    var pos = e.GetPosition(canvas);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    pos.Offset(-outputRect.Width / 2, -outputRect.Height / 2);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    Canvas.SetTop(outputRect, pos.Y);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    Canvas.SetLeft(outputRect, pos.X);</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputWindow.vb.Viewbox = <SPAN style="COLOR: #0000ff">new</SPAN> Rect(pos, <SPAN style="COLOR: #0000ff">new</SPAN> Size(outputRect.Width, outputRect.Height));</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputWindow.vb.ViewboxUnits = BrushMappingMode.Absolute;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">}</PRE></DIV></DIV>
<P><STRONG>How to programmatically place a WPF window on a secondary display ?</STRONG></P>
<P>This one is not really related to WPF but is a little bit tricky. <BR>We will need to make some interop with Window Forms because Screens information is not accessible from WPF APIs. <BR>For this demo, if an extended desktop is available, we will place our output window on it in full screen. If not, we will just put it as a regular window on the main screen.</P>
<DIV>
<DIV style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px"><SPAN style="COLOR: #0000ff">private</SPAN> <SPAN style="COLOR: #0000ff">void</SPAN> ShowOutputWindow()</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">{</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputWindow = <SPAN style="COLOR: #0000ff">new</SPAN> OutputWindow();</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    <SPAN style="COLOR: #0000ff">if</SPAN> (System.Windows.Forms.Screen.AllScreens.Length &gt; 1)</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    {</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.WindowStartupLocation = WindowStartupLocation.Manual;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">&nbsp;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        System.Drawing.Rectangle workingArea</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">            = System.Windows.Forms.Screen.AllScreens[1].WorkingArea;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.Left = workingArea.Left;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.Top = workingArea.Top;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.Width = workingArea.Width;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.Height = workingArea.Height;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.WindowStyle = WindowStyle.None;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">        outputWindow.Topmost = <SPAN style="COLOR: #0000ff">true</SPAN>;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    }</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputWindow.Show();</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: white; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">    outputWindow.vb.Visual = mainGrid;</PRE><PRE style="BORDER-BOTTOM-STYLE: none; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; FONT-FAMILY: consolas, 'Courier New', courier, monospace; BORDER-TOP-STYLE: none; COLOR: black; FONT-SIZE: 8pt; BORDER-LEFT-STYLE: none; OVERFLOW: visible; PADDING-TOP: 0px">}</PRE></DIV></DIV>
<P><STRONG>Why using the popfly duck ?!</STRONG></P>
<P>I was just looking for something to stick the tag on and when looking around me I have found this funny duck that we had received when organizing the Paris ReMix a few years ago. It's hard to stick the tag under the duck but once it's done the duck shape is quite handy !</P>
<P>The source code is attached to this post.</P>
<P><IFRAME style="BORDER-BOTTOM: #dde5e9 1px solid; BORDER-LEFT: #dde5e9 1px solid; PADDING-BOTTOM: 0px; BACKGROUND-COLOR: #ffffff; MARGIN: 3px; PADDING-LEFT: 0px; WIDTH: 240px; PADDING-RIGHT: 0px; HEIGHT: 66px; BORDER-TOP: #dde5e9 1px solid; BORDER-RIGHT: #dde5e9 1px solid; PADDING-TOP: 0px" marginHeight=0 src="http://cid-7bd91ae7f5096b79.skydrive.live.com/embedrowdetail.aspx/Public/BlogMitsu/SurfaceMedia.zip" frameBorder=0 marginWidth=0 scrolling=no mce_src="http://cid-7bd91ae7f5096b79.skydrive.live.com/embedrowdetail.aspx/Public/BlogMitsu/SurfaceMedia.zip"></IFRAME></P>]]></content:encoded>
    </item>
    <item>
      <title>[Coding4Fun]: understanding WinForms designmode</title>
      <link>https://docs.microsoft.com/archive/blogs/mitsu/coding4fun-understanding-winforms-designmode</link>
      <pubDate>Fri, 13 Feb 2009 15:00:00 GMT</pubDate>
      <dc:creator><![CDATA[Mitsu Furuta]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mitsu/2009/02/13/coding4fun-understanding-winforms-designmode/</guid>
      <description><![CDATA[We have just finished the french Paris Techdays today. I had organized a Coding4Fun session and here...]]></description>
      <content:encoded><![CDATA[<P>We have just finished the french Paris Techdays today. I had organized a Coding4Fun session and here is on of the demo which is...let's say special :p.</P>
<P><IFRAME style="WIDTH: 500px; HEIGHT: 400px" src="http://silverlight.services.live.com/invoke/62703/WinFormsDesignMode/iframe.html?autoplay=0" frameBorder=0 scrolling=no mce_src="http://silverlight.services.live.com/invoke/62703/WinFormsDesignMode/iframe.html?autoplay=0" IFRAME></P>


<P>You can find the code attached to this post.</P>

</IFRAME></P>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/41/98/14/DesignTimeForm.zip" original-url="http://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-09-41-98-14/DesignTimeForm.zip">DesignTimeForm.zip</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>