<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Haytham speaks from Microsoft</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/haythamalaa/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/haythamalaa/feed.xml</link>
    <description>Technical posts about Sharepoint and/or Microsoft in general</description>
    <lastBuildDate>Wed, 17 Apr 2019 03:23:13 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Abusing SignalR… doing good</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/abusing-signalr-doing-good</link>
      <pubDate>Mon, 17 Jun 2013 08:19:03 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2013/06/17/abusing-signalr-doing-good/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (i.e. May peace be upon you)  A couple of days ago, a...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (i.e. May peace be upon you)</p>  <p>A couple of days ago, a colleague and myself got together for a few hours to hack something for fun. The nature of the hack is beside the point. What’s interesting to this post is that, for the first time, I got introduced to SignalR [<a href="https://signalr.net/" target="_blank">Official Site</a>]. From their website:</p>  <blockquote>   <p>ASP.NET SignalR is a new library for ASP.NET developers that makes it incredibly simple to add real-time web functionality to your applications. What is &quot;real-time web&quot; functionality? It's the ability to have your server-side code push content to the connected clients as it happens, in real-time.</p> </blockquote>  <p>I’ve heard about the library and the technology behind (Web Sockets) before but this was the first time to experience it and I’ve to say, I love it!</p>  <p>The very basic scenario it addresses is this: traditionally, when you browse to a page, the only time the server can influence what’s render on the client side is during that page browse. Any additional content will have to be served upon request. To design a chat web application for example, your client code (javascript) will have to keep polling every few seconds to check if new data is available. WebSockets came to say this is ridiculous, let’s have a persisted connection between client and server to let the server send data <strong><em>whenever</em></strong> it sees appropriate. This made it more efficient (no polling) and faster to deliver near real-time information.</p>  <p>Now, that’s of itself IS awesome –I thought- but wouldn’t it be cooler if the server can ask the client for information too not just send notifications/data? sure it would!</p>  <p>For the purpose of keeping this post focused, I’ll assume minimum SignalR knowledge. If you think you don’t have that, I would strongly advise you to go through the walkthroughs here (<a href="https://github.com/SignalR/SignalR/wiki">https://github.com/SignalR/SignalR/wiki</a>).</p>  <p>Imagine we want to implement a GetFiles method. The purpose of this method is to get a list of files that exist on the client machine.</p>  <p>I start by creating a static method in the hub class (If you don’t know what Hub is, I strongly advise you to go through its walkthrough on the wiki link above).</p>  <div class="csharpcode">   <pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> FilesHub : Hub</pre>

  <pre><span class="lnum">   2:  </span>{</pre>

  <pre><span class="lnum">   3:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> Task&lt;IEnumerable&lt;string&gt;&gt; GetFiles()</pre>

  <pre><span class="lnum">   4:  </span>    {</pre>

  <pre><span class="lnum">   5:  </span>    }</pre>

  <pre><span class="lnum">   6:  </span>    </pre>

  <pre><span class="lnum">   7:  </span>}</pre>
</div>


<p>A couple of notes:</p>

<ol>
  <li>The method is static, because it’ll be called from anywhere on the server side, not necessarily as part of a response to a SignalR request.</li>

  <li>The return type is Task because we will need some waiting mechanism to receive the response. If you don’t know what Task is, take a look at this (<a href="https://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx">https://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx</a>)</li>
</ol>

<p>Now, the basic idea is, GetFiles will call BeginGetFiles on some client, as part of that call, the client can not return data. The client will then compute the response (list of file paths in this case) and then call EndGetFiles on the server side. This call will trigger the finish action for the async method. Sounds cryptic? let’s look at the code.</p>

<h4>Server side</h4>

<div class="csharpcode">
  <pre><span class="lnum">   1:  </span><span class="kwrd">public</span> <span class="kwrd">class</span> FilesHub : Hub</pre>

  <pre><span class="lnum">   2:  </span>{</pre>

  <pre><span class="lnum">   3:  </span>    <span class="kwrd">private</span> <span class="kwrd">static</span> Dictionary&lt;<span class="kwrd">string</span>, TaskCompletionSource&lt;IEnumerable&lt;PersistenceObject&gt;&gt;&gt; _getFilesTasks </pre>

  <pre><span class="lnum">   4:  </span>        = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">string</span>, TaskCompletionSource&lt;IEnumerable&lt;PersistenceObject&gt;&gt;&gt;();</pre>

  <pre><span class="lnum">   5:  </span>&#160;</pre>

  <pre><span class="lnum">   6:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> Task&lt;IEnumerable&lt;<span class="kwrd">string</span>&gt;&gt; GetFiles()</pre>

  <pre><span class="lnum">   7:  </span>    {</pre>

  <pre><span class="lnum">   8:  </span>        dynamic client = ClientLoadBalancer.Local.GetClient&lt;FilesHub&gt;();</pre>

  <pre><span class="lnum">   9:  </span>        TaskCompletionSource&lt;IEnumerable&lt;<span class="kwrd">string</span>&gt;&gt; tcs = <span class="kwrd">new</span> TaskCompletionSource&lt;IEnumerable&lt;<span class="kwrd">string</span>&gt;&gt;();</pre>

  <pre><span class="lnum">  10:  </span>        <span class="kwrd">string</span> taskId = Guid.NewGuid().ToString();</pre>

  <pre><span class="lnum">  11:  </span>        _getFilesTasks[taskId] = tcs;</pre>

  <pre><span class="lnum">  12:  </span>&#160;</pre>

  <pre><span class="lnum">  13:  </span>        <span class="kwrd">try</span></pre>

  <pre><span class="lnum">  14:  </span>        {</pre>

  <pre><span class="lnum">  15:  </span>            client.BeginGetFiles(taskId);</pre>

  <pre><span class="lnum">  16:  </span>        }</pre>

  <pre><span class="lnum">  17:  </span>        <span class="kwrd">catch</span> (Exception ex)</pre>

  <pre><span class="lnum">  18:  </span>        {</pre>

  <pre><span class="lnum">  19:  </span>            tcs.TrySetException(ex);</pre>

  <pre><span class="lnum">  20:  </span>            _getFilesTasks.Remove(taskId);</pre>

  <pre><span class="lnum">  21:  </span>        }</pre>

  <pre><span class="lnum">  22:  </span>&#160;</pre>

  <pre><span class="lnum">  23:  </span>        <span class="kwrd">if</span> (tcs.Task.Exception != <span class="kwrd">null</span>)</pre>

  <pre><span class="lnum">  24:  </span>        {</pre>

  <pre><span class="lnum">  25:  </span>            <span class="kwrd">throw</span> tcs.Task.Exception;</pre>

  <pre><span class="lnum">  26:  </span>        }</pre>

  <pre><span class="lnum">  27:  </span>&#160;</pre>

  <pre><span class="lnum">  28:  </span>        <span class="kwrd">return</span> tcs.Task;</pre>

  <pre><span class="lnum">  29:  </span>    }</pre>

  <pre><span class="lnum">  30:  </span>&#160;</pre>

  <pre><span class="lnum">  31:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> EndGetFiles(<span class="kwrd">string</span> operationId, IEnumerable&lt;<span class="kwrd">string</span>&gt; result)</pre>

  <pre><span class="lnum">  32:  </span>    {</pre>

  <pre><span class="lnum">  33:  </span>        TaskCompletionSource&lt;IEnumerable&lt;<span class="kwrd">string</span>&gt;&gt; tocall;</pre>

  <pre><span class="lnum">  34:  </span>        <span class="kwrd">if</span> (_getFilesTasks.TryGetValue(operationId, <span class="kwrd">out</span> tocall))</pre>

  <pre><span class="lnum">  35:  </span>        {</pre>

  <pre><span class="lnum">  36:  </span>            tocall.TrySetResult(result);</pre>

  <pre><span class="lnum">  37:  </span>        }</pre>

  <pre><span class="lnum">  38:  </span>    }</pre>

  <pre><span class="lnum">  39:  </span>}</pre>
</div>


<h4>&#160;</h4>

<h4>Client side</h4>

<div class="csharpcode">
  <div class="csharpcode">
    <pre><span class="lnum">   1:  </span>hub.On&lt;<span class="kwrd">string</span>&gt;(<span class="str">&quot;BeginGetFiles&quot;</span>, (opId) =&gt;</pre>

    <pre><span class="lnum">   2:  </span>{</pre>

    <pre><span class="lnum">   3:  </span>    Console.WriteLine(<span class="str">&quot;BeginGetFiles - opId: &quot;</span> + opId);</pre>

    <pre><span class="lnum">   4:  </span>    ThreadPool.QueueUserWorkItem(<span class="kwrd">new</span> WaitCallback((state) =&gt;</pre>

    <pre><span class="lnum">   5:  </span>    {</pre>

    <pre><span class="lnum">   6:  </span>        IEnumerable&lt;<span class="kwrd">string</span>&gt; result = <span class="kwrd">null</span>; <span class="rem">// Calculate result here</span></pre>

    <pre><span class="lnum">   7:  </span>        Console.WriteLine(<span class="str">&quot;Calling EndGetFiles - opId:&quot;</span> + opId);</pre>

    <pre><span class="lnum">   8:  </span>        hub.Invoke&lt;<span class="kwrd">string</span>&gt;(<span class="str">&quot;EndGetFiles&quot;</span>, opId, result).Wait();</pre>

    <pre><span class="lnum">   9:  </span>        Console.WriteLine(<span class="str">&quot;Called EndGetFiles - opId:&quot;</span> + opId);</pre>

    <pre><span class="lnum">  10:  </span>    }));</pre>

    <pre><span class="lnum">  11:  </span>});</pre>
  </div>
  </div>


<p>Now you can just call it like this</p>

<div class="csharpcode">
  <pre><span class="lnum">   1:  </span>IEnumerable&lt;<span class="kwrd">string</span>&gt; files = await FilesHub.GetFiles();</pre>
</div>


<p>Clean? yeah, that’s what I thought. In my opinion, SignalR should provide such functionality without the above hack. However, it sounds this would be abusing the library. The main goal of the library is to send notifications (signals :)) from server to client in a pushing mechanism. Even though I agree this would abuse the library, I think it’s still a good trick and might come handy at times.</p>

<p>Till next time,</p>]]></content:encoded>
    </item>
    <item>
      <title>Writing proper async code</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/writing-proper-async-code</link>
      <pubDate>Sun, 12 Jun 2011 16:03:13 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2011/06/12/writing-proper-async-code/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (aka peace upon you)  Asynchronous calls have always...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (aka peace upon you)</p>  <p>Asynchronous calls have always been there for more efficient use of CPU time. You run something asynchronously when you know it’ll take some time to execute and you don’t want to block your current thread while waiting for that code (In some cases you actually do want to block the thread but then spanning another thread is, in this case, more or less a waste of resources In My Humble Opinion).</p>  <p>For the purpose of this post, let’s take as an example downloading a remote file, and printing the contents of the file to screen.</p>  <p>I would imagine this as one way to implement it:</p>  <pre class="code"><span style="color: blue"><pre class="code"><span style="color: blue">private void </span>TestSync()
{
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Starting Test() &quot; </span>+ GetCurrentContext());
    WriteLinePageTitle(DownloadStringSlowNetworkSync(<span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">&quot;https://sample.com&quot;</span>)));
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Test() is done with big delay &quot; </span>+ GetCurrentContext());
}

<span style="color: blue">private string </span>DownloadStringSlowNetworkSync(<span style="color: #2b91af">Uri </span>uri)
{
    <span style="color: #2b91af">Thread</span>.Sleep(500);
    <span style="color: blue">return new </span><span style="color: #2b91af">WebClient</span>().DownloadString(uri);
}</pre></span>
This is an example for a synchronous operation. TestSync() will not return till the string is downloaded. Here is a run:</pre>

<pre class="code"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/5658.image_64F66A02.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/5658.image_5F00_64F66A02.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/6758.image_thumb_51690A61.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/6758.image_5F00_thumb_5F00_51690A61.png" width="459" height="129" /></a></pre>

<pre class="code">As you can see, it all ran on the same thread.</pre>

<pre class="code">If a UI component is calling TestSync() this means execution will block UI. And that’s the typical use of of Asynchronous method.</pre>

<pre class="code">Here is the typical way to implement async:</pre>

<pre class="code"><span style="color: blue">private void </span>TestAsync1()
{
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Starting Test() &quot; </span>+ GetCurrentContext());
    BeginDownloadStringSlowNetworkAsync(
        <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">&quot;https://sample.com&quot;</span>),
        (sender, e) =&gt;
        {
            WriteLinePageTitle(e.Result);
        },
        (ex) =&gt;
        {
            WriteLinePageTitle(<span style="color: #a31515">&quot;Exception is thrown: &quot; </span>+ ex.Message);
        });
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Test() is done with no delay &quot; </span>+ GetCurrentContext());
}

<span style="color: blue">private void </span>BeginDownloadStringSlowNetworkAsync(
    <span style="color: #2b91af">Uri </span>uri, 
    <span style="color: #2b91af">DownloadStringCompletedEventHandler </span>completedCallback, 
    <span style="color: #2b91af">Action</span>&lt;<span style="color: #2b91af">Exception</span>&gt; errorCallback)
{
    <span style="color: #2b91af">Thread</span>.Sleep(500);
    <span style="color: blue">try
    </span>{
        <span style="color: blue">var </span>client = <span style="color: blue">new </span><span style="color: #2b91af">WebClient</span>();
        client.DownloadStringCompleted += completedCallback;
        client.DownloadStringAsync(uri);
    }
    <span style="color: blue">catch </span>(<span style="color: #2b91af">Exception </span>ex)
    {
        errorCallback(ex);
    }
}</pre>

<pre class="code"><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/2047.image_38010727.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2047.image_5F00_38010727.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/0878.image_thumb_10C6BDF2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/0878.image_5F00_thumb_5F00_10C6BDF2.png" width="455" height="92" /></a></pre>

<pre class="code">This is a little bit better, now UI is not blocked (notice UI running in thread 1 continued working fine)… <br />There are 2 down sides of this implementation,<br />1- Code isn’t intuitive, it’s way different than the counter sync code.<br />2- Exception handling is not propagating well enough, stack trace isn’t preserved for instance.</pre>

<pre class="code">Now with the better version, using <a href="https://msdn.microsoft.com/en-us/vstudio/async.aspx" target="_blank"><strong><font size="3">Async Library</font></strong></a>.</pre>

<pre class="code"><span style="color: blue">public async </span><span style="color: #2b91af">Task </span>TestAsync2()
{
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Starting Test() &quot; </span>+ GetCurrentContext());
    RunAsync();
    <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">&quot;Test() is done with no delay &quot; </span>+ GetCurrentContext());
}

<span style="color: blue">private async void </span>RunAsync()
{
    WriteLinePageTitle(<span style="color: blue">await </span>DownloadStringTaskSlowNetworkAsync(<span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">&quot;https://sample.com&quot;</span>)));
}</pre>

<pre class="code"><span style="color: blue">public async </span><span style="color: #2b91af">Task</span>&lt;<span style="color: blue">string</span>&gt; DownloadStringTaskSlowNetworkAsync(<span style="color: #2b91af">Uri </span>address)
{
    <span style="color: blue">await </span><span style="color: #2b91af">TaskEx</span>.Delay(500);    <span style="color: green">// Simulate 500ms of network delay
    </span><span style="color: blue">return await new </span><span style="color: #2b91af">WebClient</span>().DownloadStringTaskAsync(address);
}</pre>

<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/6747.image_5E62EA72.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/6747.image_5F00_5E62EA72.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/2626.image_thumb_170D9480.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2626.image_5F00_thumb_5F00_170D9480.png" width="399" height="85" /></a></p>

<p>You will notice a couple of things here: 
  <br />1- The result is identical to the async code we wrote above 

  <br />2- Code looks intuitive, very similar to sync version 

  <br />3- There are two new keywords async and await. And that’s what I <em>actually </em>want to talk about..</p>

<p>async: you decorate your methods with async to be able to call await inside them.
  <br />await: you prefix method calls with await when you want the library to create callbacks for you to handle async operation.</p>

<p>In DownloadStringTaskSlowNetworkAsync() method, we call DownloadStringTaskAsync, which is an extension method included in Async library. What await does is that it creates a callback method that includes the rest of your code (return statement in this case), then calls the method DownloadStringTaskAsync() and registers that callback, when the callback is invoked (ie when the string is actually downloaded), it invokes the rest of your code (return… etc).</p>

<p>Going one level up, RunAsync(), it awaits on DownloadStringTaskSlowNetworkAsync, which means WriteLinePageTitle will not be called till DownloadStringTaskSlowNetworkAsync returns… till then, RunAsync will just return a void task.</p>

<p>One more level up, here we called RunAsync without prefixing the call with await, this means, the function will behave exactly like our previous Begin method, it’ll start, and control flow will directly continue to print “Test() is done…”.</p>

<p><strong>A few things to note:</strong>

  <br />1- The leaves of the calls (DownloadStringTaskAsync… etc) are implemented by Async Library, if you want to implement your own leaf method, it’s a little bit more interesting, I’ll save that for a coming post.

  <br />2- await doesn’t block current thread, it rather wraps up the rest of code in your method and creates a callback to execute the code after async operation is done.

  <br />3- If you surrounded an await statement with try,catch, catch statements will be included in that callback, which means you will be able to catch exceptions for async methods the same way you catch exceptions for sync methods</p>

<p>Last to note, Async Library is available for WP7 as well as WPF/Silverlight… I’ve to tell you, it makes async code WAY easier to write!</p>

<p>Stay Tuned <img style="border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/2548.wlEmoticon-smile_08CF1B90.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-06-30-metablogapi/2548.wlEmoticon_2D00_smile_5F00_08CF1B90.png" /></p>]]></content:encoded>
    </item>
    <item>
      <title>WebParts fail to import for readers? here is a possible fix</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/webparts-fail-to-import-for-readers-here-is-a-possible-fix</link>
      <pubDate>Wed, 09 Jun 2010 16:42:06 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2010/06/09/webparts-fail-to-import-for-readers-here-is-a-possible-fix/</guid>
      <description><![CDATA[One of our partners faced a problem that I think we will eventually see occurring frequently with...]]></description>
      <content:encoded><![CDATA[<p>One of our partners faced a problem that I think we will eventually see occurring frequently with external customers, so I thought to document the problem’s symptoms and a possible fix..</p>  <h3>Problem Context:</h3>  <ol>   <li>You have a v2 WebPart (didn’t try it with v3 but it’s likely to repro as well), that you deployed to a server. </li>    <li>You (or some developer who uses your WebPart), creates a feature, with an elements file that tries to deploy that WebPart into a page, something like this:      <br />      <pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">Elements</span> <span style="color: #ff0000">xmlns</span>=<span style="color: #0000ff">&quot;&lt;a href=&quot;</span><span style="color: #ff0000">http</span>://<span style="color: #ff0000">schemas</span>.<span style="color: #ff0000">microsoft</span>.<span style="color: #ff0000">com</span>/<span style="color: #ff0000">sharepoint</span>/&quot;&quot;<span style="color: #0000ff">&gt;</span>https://schemas.microsoft.com/sharepoint/&quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;</span>&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Module</span> <span style="color: #ff0000">Name</span>=<span style="color: #0000ff">&quot;MyModule&quot;</span> <span style="color: #ff0000">Url</span>=<span style="color: #0000ff">&quot;Shared Documents&quot;</span> <span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">&lt;</span><span style="color: #800000">File</span> <span style="color: #ff0000">Url</span>=<span style="color: #0000ff">&quot;Test.aspx&quot;</span> <span style="color: #ff0000">Type</span>=<span style="color: #0000ff">&quot;GhostableInLibrary&quot;</span> <span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">   <span style="color: #0000ff">&lt;</span><span style="color: #800000">AllUsersWebPart</span> <span style="color: #ff0000">WebPartZoneID</span>=<span style="color: #0000ff">&quot;Header&quot;</span> <span style="color: #ff0000">WebPartOrder</span>=<span style="color: #0000ff">&quot;1&quot;</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    <span style="color: #0000ff">&lt;</span>![CDATA[
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">     <span style="color: #0000ff">&lt;?</span>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;<span style="color: #0000ff">?&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">WebPart</span> <span style="color: #ff0000">xmlns</span>:<span style="color: #ff0000">xsd</span>=<span style="color: #0000ff">&quot;&lt;a href=&quot;</span><span style="color: #ff0000">http</span>://<span style="color: #ff0000">www</span>.<span style="color: #ff0000">w3</span>.<span style="color: #ff0000">org</span>/<span style="color: #ff0000">2001</span>/<span style="color: #ff0000">XMLSchema</span>&quot;&quot;<span style="color: #0000ff">&gt;</span>https://www.w3.org/2001/XMLSchema&quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;</span> xmlns:xsi=&quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">a</span> <span style="color: #ff0000">href</span>=<span style="color: #0000ff">&quot;https://www.w3.org/2001/XMLSchema-instance&quot;</span>&quot;&gt;<span style="color: #ff0000">http</span>://<span style="color: #ff0000">www</span>.<span style="color: #ff0000">w3</span>.<span style="color: #ff0000">org</span>/<span style="color: #ff0000">2001</span>/<span style="color: #ff0000">XMLSchema</span>-<span style="color: #ff0000">instance</span>&quot;&lt;/<span style="color: #ff0000">a</span><span style="color: #0000ff">&gt;</span> xmlns=&quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">a</span> <span style="color: #ff0000">href</span>=<span style="color: #0000ff">&quot;https://schemas.microsoft.com/WebPart/v2&quot;</span>&quot;&gt;<span style="color: #ff0000">http</span>://<span style="color: #ff0000">schemas</span>.<span style="color: #ff0000">microsoft</span>.<span style="color: #ff0000">com</span>/<span style="color: #ff0000">WebPart</span>/<span style="color: #ff0000">v2</span>&quot;&lt;/<span style="color: #ff0000">a</span><span style="color: #0000ff">&gt;</span>&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Title</span><span style="color: #0000ff">&gt;</span>A Test<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Title</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">FrameType</span><span style="color: #0000ff">&gt;</span>Default<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FrameType</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Description</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">IsIncluded</span><span style="color: #0000ff">&gt;</span>true<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IsIncluded</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">ZoneID</span><span style="color: #0000ff">&gt;</span>Header<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ZoneID</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">PartOrder</span><span style="color: #0000ff">&gt;</span>0<span style="color: #0000ff">&lt;/</span><span style="color: #800000">PartOrder</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">FrameState</span><span style="color: #0000ff">&gt;</span>Normal<span style="color: #0000ff">&lt;/</span><span style="color: #800000">FrameState</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Height</span><span style="color: #0000ff">&gt;</span>222px<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Height</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Width</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Width</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">AllowRemove</span><span style="color: #0000ff">&gt;</span>false<span style="color: #0000ff">&lt;/</span><span style="color: #800000">AllowRemove</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">AllowZoneChange</span><span style="color: #0000ff">&gt;</span>false<span style="color: #0000ff">&lt;/</span><span style="color: #800000">AllowZoneChange</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">AllowMinimize</span><span style="color: #0000ff">&gt;</span>false<span style="color: #0000ff">&lt;/</span><span style="color: #800000">AllowMinimize</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">IsVisible</span><span style="color: #0000ff">&gt;</span>true<span style="color: #0000ff">&lt;/</span><span style="color: #800000">IsVisible</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">DetailLink</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">HelpLink</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Dir</span><span style="color: #0000ff">&gt;</span>Default<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Dir</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">PartImageSmall</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">MissingAssembly</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">PartImageLarge</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">IsIncludedFilter</span> <span style="color: #0000ff">/&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">ID</span><span style="color: #0000ff">&gt;</span>g_A4D135E1_0834_4b88_AA94_E7EC5BA80E93<span style="color: #0000ff">&lt;/</span><span style="color: #800000">ID</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Assembly</span><span style="color: #0000ff">&gt;</span>MyWebPartAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35<span style="color: #0000ff">&lt;/</span><span style="color: #800000">Assembly</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;</span><span style="color: #800000">TypeName</span><span style="color: #0000ff">&gt;</span>MyWebParts.WebPart1<span style="color: #0000ff">&lt;/</span><span style="color: #800000">TypeName</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">WebPart</span><span style="color: #0000ff">&gt;</span>            ]]&gt;
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">   <span style="color: #0000ff">&lt;/</span><span style="color: #800000">AllUsersWebPart</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  <span style="color: #0000ff">&lt;/</span><span style="color: #800000">File</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Module</span><span style="color: #0000ff">&gt;</span>
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Elements</span><span style="color: #0000ff">&gt;</span></pre></pre>
  </li>

  <li>Login as admin, enable the feature.. do NOT browse to Shared Documents\test.aspx. </li>

  <li>Login as reader/visitor and try to browse to that page, you will be greeted with an ErrorWebPart saying: 
    <br /><strong>Error 
      <br />Web Part Error: A Web Part or Web Form Control on this Page cannot be displayed or imported. You don't have Add and Customize Pages permissions required to perform this action. </strong></li>

  <li>If, at this point, you logged in as Admin, the WebPart will render just fine and any reader logs in from now on will render just fine as well </li>
</ol>

<h3>The problem: </h3>

<p>Well, it’s hard to call it a problem, it’s a design decision we took. Sharepoint at that first page visit, will do the WebPart import into the page, and as it’s a reader who tries to do the import, it fails. </p>

<h3>The solution: </h3>

<p>The failure (as the text tells) is because the logged in user doesn’t have AddAndCustomizePages permission, this error pops up in different places but the reason for that specific one is because MyWebPart1 is not marked as SafeAgainstScript=True (I’ve a post about it <a href="https://blogs.msdn.com/b/haythamalaa/archive/2009/12/03/contributors-contributors-contributors-sharepoint.aspx" target="_blank">here</a>). You don’t want to mark just any WebPart as SafeAgainstScript=True because that can expose you, the site and your users to danger. At this point you have 3 options: </p>

<ol>
  <li>If you know for sure that non of your WebPart properties accepts strings from users (or that you do proper encoding/stripping to any script you might get), then go ahead and mark the WebPart as SafeAgainstScript=True in the &lt;SafeControls&gt; entry. </li>

  <li>If you are unsure, then your best bet is to mark it as SafeAgainstScript=True and put [RequiresDesignerPermissions] attribute on all properties of your WebPart. This will allow readers to import the WebPart but not to change any. </li>

  <li>Just let your users be aware of this scenario <img style="border-bottom-style: none; border-right-style: none; border-top-style: none; border-left-style: none" class="wlEmoticon wlEmoticon-smile" alt="Smile" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/06/30/metablogapi/1321.wlEmoticon-smile_2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-06-30-metablogapi/1321.wlEmoticon_2D00_smile_5F00_2.png" />.</li>
</ol>

<p>That’s it for now… stay tuned!</p>]]></content:encoded>
    </item>
    <item>
      <title>What should I talk about?</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/what-should-i-talk-about</link>
      <pubDate>Thu, 17 Dec 2009 10:25:51 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/12/17/what-should-i-talk-about/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)  One of the problems every blogger...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)</p>  <p>One of the problems every blogger faces is how to answer the above question… We often ask our fellow friends for suggestions, or even post a whole post and let people suggest topics in the comments… well, no more! skribit (<a title="https://skribit.com/" href="https://skribit.com/">https://skribit.com/</a>) tries to find a better solution to that, by subscribing, it gives you a widget code that you can put as a side bar in your blog to let people suggest topics to talk about…</p>  <p>And I’m starting this right away, I’ve included the suggestions box in this post, please use it to suggest topics for me to talk about… after a while I’ll move it into the sidebar and keep it there for the foreseeable future :)</p>  <p>Note: if you are using RSS, you might not be able to get the box, please visit the original post link.</p>  <div id="writeSkribitHere"></div><script src="https://assets.skribit.com/javascripts/SkribitWidget.js?renderTo=writeSkribitHere&amp;blog=57e97897412109ac650e09e944081db3&amp;cnt=5"></script><noscript>Sorry, but the <a href="https://skribit.com" title="Skribit - Cure Writer's Block">Skribit</a> widget only works on browsers with JavaScript support.  <a href="https://skribit.com/blogs/haytham-talks-from-microsoft" title="Skribit Suggestions for Haytham talks from Microsoft">View suggestions for this blog here.</a></noscript>  <p>&#160;</p>  <p>Thanks, </p>  <p>Stay tuned ;)</p>]]></content:encoded>
    </item>
    <item>
      <title>Contributors, Contributors, Contributors [Sharepoint]</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/contributors-contributors-contributors-sharepoint</link>
      <pubDate>Thu, 03 Dec 2009 11:14:03 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/12/03/contributors-contributors-contributors-sharepoint/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)  I’m planning to start a series of...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (aka Peace Upon You)</p>  <p>I’m planning to start a series of posts, targeting SharePoint developers, introducing some parts of features I got exposed to. I’ll start this by talking about new features we are releasing as part of our security solution in Windows Sharepoint Foundation 2010.</p>  <p>One of the famous threats in the internet is XSS (Cross Site Scripting [<a href="https://en.wikipedia.org/wiki/Cross-site_scripting">Wikipedia link</a>]). Let me describe it in a short scenario,     <br />BadUser creates a page with a form and some javascript code that posts the form data to BadSite.com, then BadUser uploads that page into GoodSite.com/userPages, now he also got access to whatever information that site provides about its users.     <br />GoodUser browses to the page in GoodSite.com, fills the form and violla, BadUser gets the information + whatever his script can gather about GoodUser..     <br />The problem here is that GoodUser didn’t really know that this page will go post data somewhere else… and has no way to know except by using some advanced techniques…</p>  <p>This can be applied on WebParts, some WebParts allow users to directly inject nonencoded text that gets rendered into WebPart Pages (WikiPages).. eg. ContentEditorWebPart. For this, we have introduced new web.config flag called SafeAgainstScript. which basically if set to false, will prevent non-privileged users from changing properties that might not be encoded.</p>  <p>And into the details…</p>  <p></p>  <p>We, generally, prevent contributors from modifying unsafe properties in WebParts. For that to be flexible, we introduce 2 new flags </p>  <ul>   <li><strong>SafeAgainstScript</strong> SafeControls [<a href="https://msdn.microsoft.com/en-us/library/ms413697.aspx">MSDN Link</a>] flag, setting this flag to false (it’s <strong><font color="#ff0000">false</font></strong> by default) on any SafeControl entry, means contributors are not allowed to insert these WebParts into WebPart Pages / Wiki Pages, and if a Designer (a user with Add and Customize Pages permission [<a href="https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spsecuritytrimmedcontrol.permissionsstring.aspx">MSND Link</a>]) has already added that, then contributors won’t be allowed to modify any of its properties except for the Title. </li>    <ul>     <li>e.g </li>      <pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 15px; font-size: 12px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">SafeControls</span><span style="color: #0000ff">&gt;</span> 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 27px; font-size: 12px">         <span style="color: #0000ff">&lt;</span><span style="color: #800000">SafeControl</span> <span style="color: #ff0000">Assembly</span>=<span style="color: #0000ff">&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=XXXXXXXXXXXXXX&quot;</span> <br /><span style="color: #ff0000">Namespace</span>=<span style="color: #0000ff">&quot;Microsoft.SharePoint.WebPartPages&quot;</span> <span style="color: #ff0000">TypeName</span>=<span style="color: #0000ff">&quot;TitleBarWebPart&quot;</span> <span style="color: #ff0000">Safe</span>=<span style="color: #0000ff">&quot;True&quot;</span> <span style="color: #ff0000">AllowRemoteDesigner</span>=<span style="color: #0000ff">&quot;True&quot;</span> <strong><span style="color: #ff0000">SafeAgainstScript</span>=<span style="color: #0000ff">&quot;True&quot;</span></strong> <span style="color: #0000ff">/&gt;</span> 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">&lt;/</span><span style="color: #800000">SafeControls</span><span style="color: #0000ff">&gt;</span></pre></pre>
  </ul>

  <li><strong>RequiresDesignerPermission</strong> property-level attribute. It comes in effect only when a WebPart is marked as <strong>SafeAgainstScript=True</strong> in Web.Config SafeControls entries, WebPart developers can use it to say “even though this WebPart is safe, these specific properties are not” </li>

  <ul>
    <li>e.g </li>

    <pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 14px; font-size: 12px">[RequiresDesignerPermission] 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 17px; font-size: 12px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Content 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 16px; font-size: 12px">{ 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 15px; font-size: 12px">    <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> _content; } 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; height: 14px; font-size: 12px">    <span style="color: #0000ff">set</span> { _content = <span style="color: #0000ff">value</span>; } 
</pre><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">} </pre></pre>
  </ul>

  <li>Here is a matrix that explains which flag will have the upper hand in different situations: 
    <ul>
      <li>SafeAgainstScript=<strong>False</strong> 

        <table border="1" cellspacing="0" cellpadding="2" width="477"><tbody>
            <tr>
              <td valign="top" width="135">&#160;</td>

              <td valign="top" width="131">Contributors</td>

              <td valign="top" width="209">Designers &amp; higher</td>
            </tr>

            <tr>
              <td valign="top" width="135">RequiresDesignerPermission=True</td>

              <td valign="top" width="131"><strong>No Access</strong></td>

              <td valign="top" width="209">Can Access</td>
            </tr>

            <tr>
              <td valign="top" width="135">RequiresDesignerPermission=False</td>

              <td valign="top" width="131"><strong>No Access</strong></td>

              <td valign="top" width="209">Can Access</td>
            </tr>
          </tbody></table>

        <br /></li>
    </ul>

    <ul>
      <li>SafeAgainstScript<strong>=True</strong> 

        <table border="1" cellspacing="0" cellpadding="2" width="471"><tbody>
            <tr>
              <td valign="top" width="133">&#160;</td>

              <td valign="top" width="133">Contributors</td>

              <td valign="top" width="203">Designers &amp; higher</td>
            </tr>

            <tr>
              <td valign="top" width="133">RequiresDesignerPermision=True</td>

              <td valign="top" width="133">Can Access</td>

              <td valign="top" width="203">Can Access</td>
            </tr>

            <tr>
              <td valign="top" width="133">RequiresDesignerPermission=False</td>

              <td valign="top" width="133"><strong>No Access</strong></td>

              <td valign="top" width="203">Can Access</td>
            </tr>
          </tbody></table>
      </li>
    </ul>
  </li>

  <li>For the built-in WebParts, we have taken the effort to hide the properties from the toolpane in situations where users have No Access to modify them. For custom WebParts, developers have two choices 
    <ol>
      <li>Use our built in ToolPane provider, and we will take care of hiding and showing these properties. </li>

      <li>If you need to provide a custom ToolPane, you will need to take care of that. (I understand we need to improve at this point to provide you with the public APIs to make your life easier) </li>
    </ol>
  </li>
</ul>

<p></p>

<p>I’ll put together a sample custom WebPart that demonstrate these new flags, and will update this post once I get it uploaded.</p>

<p>Stay Tuned!</p>]]></content:encoded>
    </item>
    <item>
      <title>Invent your own language… using Oslo Part II</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/invent-your-own-language-using-oslo-part-ii</link>
      <pubDate>Sun, 24 May 2009 14:54:26 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/05/24/invent-your-own-language-using-oslo-part-ii/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You)  Part II: Consume the Abstract Syntax...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You)</p>  <p align="center"><strong><font size="5">Part II: Consume the Abstract Syntax Tree        <br /></font></strong><em><font size="2">… Do some action! …</font></em></p>  <p>If you have not read <a href="https://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank">Part I</a>, we have created our first grammar to recognize this language:</p>  <div id="codeSnippetWrapper">   <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">Send <span style="color: #006080">&quot;D:\Reports\Templates\Regular.tmpl&quot;</span> <span style="color: #0000ff">to</span> sm1@hotmail.com<br />Send <span style="color: #006080">&quot;D:\Reports\Templates\Special.tmpl&quot;</span> <span style="color: #0000ff">to</span> sm2@hotmail.com,sm3@hotmail.com</pre>

  <br /></div>

<p></p>

<p>Visit <a href="https://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank">here</a> [Part I link] to find the reporter.mg file listing at the end.</p>

<p>In this post, we will explore how to compile/parse the generated tree programmatically. And we will dig a little bit one cool way to do .NET code generation works.</p>

<ol>
  <li>Create a new project (You can pick a console one for simplicity), Add Reference to: 
    <br />C:\Program Files\Microsoft Oslo SDK 1.0\Bin\Microsoft.M.Grammar.dll and 

    <br />C:\Program Files\Microsoft Oslo SDK 1.0\Bin\System.Dataflow.dll 

    <br />

    <br />Microsoft.M.Grammar.dll contains MGrammarCompiler which we will use to compile our .mg file into .mgx (just as we used mg.exe tool) 

    <br />

    <br />System.Dataflow.dll contains DynamicParser class which will be our starting point to parse the generated AST. 

    <br /></li>

  <li>This is the compile function: 
    <div>
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Compile()<br />{<br />    MGrammarCompiler compiler = <span style="color: #0000ff">new</span> MGrammarCompiler();<br />    compiler.FileNames = <span style="color: #0000ff">new</span> <span style="color: #0000ff">string</span>[] { <br />        <span style="color: #006080">@&quot;C:\Users\HaythamAlaa\Documents\reporter.mg&quot;</span> };<br />    compiler.OutFile = <span style="color: #006080">&quot;reporter.mgx&quot;</span>;<br />    compiler.Execute(ErrorReporter.Standard);<br />}</pre>
    </div>

    <div>
      <br />Basically, we pass the fileName(s) and output file then just call Execute and you are done. 

      <br /></div>
  </li>

  <li>
    <div>Then, we add a Parse method: 
      <br /></div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Parse()<br />{<br />    FileStream mgxFile = File.OpenRead(<span style="color: #006080">&quot;reporter.mgx&quot;</span>);<br />    DynamicParser parser = DynamicParser.LoadFromMgx(mgxFile, <br />                                <span style="color: #006080">&quot;Basic.Languages.Reporter&quot;</span>);<br />    <span style="color: #0000ff">object</span> root = parser.Parse&lt;<span style="color: #0000ff">object</span>&gt;(<br />            <span style="color: #006080">@&quot;C:\Users\HaythamAlaa\Documents\reportsData.bundle&quot;</span>, <br />            <span style="color: #0000ff">null</span>, ErrorReporter.Standard);<br />    IGraphBuilder graphBuilder = parser.GraphBuilder;<br /><br />    Traverse(root, graphBuilder);<br /></pre>
DynamicParser loads the mgx file we compiled earlier, and then we ask it to parse the bundle file… note that parser.Parse&lt;object&gt; object is obligatory (I believe this will be refactored in the release) 

      <br />

      <br />As all nodes returned of type object, parser provides an IGraphBuilder object that helps us traverse and query the AST. 

      <br />

      <br />Traverse method should just loop over all nodes (and sub nodes) and output their values. 

      <br /></div>
  </li>

  <li>
    <div>Traverse is very straight forward. However, there is a couple of tricks we need to be aware of while traversing the tree… 
      <br />First, here is the basic Traverse Method:</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Traverse(<span style="color: #0000ff">object</span> root, IGraphBuilder graphBuilder)<br />{<br />    <span style="color: #0000ff">if</span> (root <span style="color: #0000ff">is</span> <span style="color: #0000ff">string</span>)<br />    {<br />        Console.WriteLine(root.ToString());<br />        <span style="color: #0000ff">return</span>;<br />    }<br /><br />    Console.WriteLine(<span style="color: #006080">&quot;-&gt;&quot;</span> + graphBuilder.GetLabel(root));<br /><br />    <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> childNode <span style="color: #0000ff">in</span> graphBuilder.GetSuccessors(root))<br />    {<br />        Traverse(childNode, graphBuilder);<br />    }<br />}</pre>

      <br />The output should be like this:</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">-&gt;Commands<br />-&gt;Send<br />D:\Reports\Templates\Regular.tmpl<br />-&gt;Emails<br />sm1@hotmail.com<br />-&gt;Send<br />D:\Reports\Templates\Special.tmpl<br />-&gt;Emails<br />sm2@hotmail.com<br /><a href="mailto:sm3@hotmail.com">sm3@hotmail.com</a></pre>

      <br />First thing to notice, this is a simple Depth First Search (or Visitor Pattern as architects like to call it). 

      <br />

      <br />Second, The leaf nodes of the tree are all strings, the non-leaf ones can be of type SimpleNode or SequenceNode (or something else?) depending on how they were projected in MGrammar. 

      <br />e.g:</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">=&gt; Send{. . .};</pre>
Would produce: 

      <br /></div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">Send{<br />...<br /><br /><br />}</pre>
That will be parsed as <strong>SimpleNode</strong> 

      <br />

      <br />While,</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">=&gt; Send[. . .];</pre>
Would produce:</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">Send[<br />.....<br />]</pre>
And will be parsed as <strong>SequenceNode</strong> 

      <br />

      <br />I didn’t read any recommendation on when to use what… but I personally prefer to use {} when you are thinking of a Class and use [] when you are thinking of an Array… more of this will come later when we start generating code. 

      <br />

      <br />Third, GetSuccessor: This method can visit any node and iterate over its children. There is also GetSequenceElements and GetEntityMembers… probably what we want to do is something like this: 

      <br />if (graphBuilder.IsEntity(root)) 

      <br />&#160;&#160;&#160; foreach (… graphBuilder.GetEntityMembers(root)) 

      <br />else if (graphBuilder.IsSequence(root)) 

      <br />&#160;&#160;&#160; foreach (… graphBuilder.GetSequenceElements(root)) 

      <br />… same thing for IsNode 

      <br />

      <br />Note the order of checking is important, (e.g IsSequence before IsNode) because a sequence node is also a “node”… 

      <br /></div>
  </li>

  <li>
    <div>Let the fun begin, first, let’s declare a method that accepts a path and a list of emails as parameters,</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Send(<span style="color: #0000ff">string</span> reportPath, <span style="color: #0000ff">params</span> <span style="color: #0000ff">string</span>[] contacts)<br />{<br />    Console.WriteLine(reportPath);<br />    <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">string</span> str <span style="color: #0000ff">in</span> contacts)<br />        Console.WriteLine(<span style="color: #006080">&quot;-&gt;&quot;</span> + str);<br />}</pre>
Now, we want to map every Send node into a method call with the right parameters… 

      <br />We will use <a href="https://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx" target="_blank">System.Linq.Expressions</a> namespace [MSDN Link]. 

      <br />

      <br />This namespace contains virtually all types of operations you can perform in .NET, Assignment, MethodCall, Lambda Expression, ArrayIndexing…. etc 

      <br />

      <br />2 things we are interested in for now, are MethodCallExpression (to be able to represent our Send method) and Lambda Expression to be able to compile into MSIL and actually run these instructions 

      <br />

      <br />Here is an example for this in action</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">MethodCallExpression expression = <br />&#160;&#160;&#160; Expression.Call(<span style="color: #0000ff">typeof</span>(Program).GetMethod(<span style="color: #006080">&quot;Send&quot;</span>),<br />    <span style="color: #0000ff">new</span> Expression[] { Expression.Constant(path, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">string</span>)),<br />                       Expression.Constant(emails, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">string</span>[])) });<br />LambdaExpression lambda = Expression.Lambda(expression);<br />lambda.DynamicInvoke();<br /></pre>
Expression is an abstract base class that contains static methods to instantiate all types of expressions.. here we call Expression.Call to create a MethodCallExpression. 

      <br />It accepts a MethodInfo for the method you want to call… 

      <br />and a list of parameters. 

      <br />You can then call lambda.DynamicInvoke() to compile into MSIL and execute whatever expressions are inside… 

      <br /></div>
  </li>

  <li>
    <div>Here is the full listing for creating the lambda expressions out of a passed node: </div>

    <div id="codeSnippetWrapper">
      <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> LambdaExpression[] TraverseAndBuildTree(<span style="color: #0000ff">object</span> root, </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>                                         IGraphBuilder graphBuilder)</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> {</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>   <span style="color: #0000ff">if</span> (graphBuilder.GetLabel(root).ToString().ToLower() == <span style="color: #006080">&quot;send&quot;</span>)</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>   {</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     IEnumerable&lt;<span style="color: #0000ff">object</span>&gt; childs = graphBuilder.GetSuccessors(root);</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>&#160; </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     <span style="color: #0000ff">string</span> path = </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>         graphBuilder.GetSuccessors(childs.First()).First().ToString(</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     List&lt;<span style="color: #0000ff">string</span>&gt; emails = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">string</span>&gt;();</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> objContact <span style="color: #0000ff">in</span></pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         graphBuilder.GetSequenceElements(childs.Last()))</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     {</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>       emails.Add(objContact.ToString());</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     }</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>&#160; </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>     MethodCallExpression expression = </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>       Expression.Call(<span style="color: #0000ff">typeof</span>(Program).GetMethod(<span style="color: #006080">&quot;Send&quot;</span>),</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>       <span style="color: #0000ff">new</span> Expression[] { Expression.Constant(path, <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">string</span>)),</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>           Expression.Constant(emails.ToArray(), <span style="color: #0000ff">typeof</span>(<span style="color: #0000ff">string</span>[])) })</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     LambdaExpression lambda = Expression.Lambda(expression);</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> LambdaExpression[] { lambda };</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span>   }</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24">  24:</span>&#160; </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25">  25:</span>   List&lt;LambdaExpression&gt; expressions = <span style="color: #0000ff">new</span> List&lt;LambdaExpression&gt;();</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26">  26:</span>   <span style="color: #0000ff">if</span> (graphBuilder.IsSequence(root))</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27">  27:</span>     <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> childNode <span style="color: #0000ff">in</span> </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28">  28:</span>         graphBuilder.GetSequenceElements(root))</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29">  29:</span>     {</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30">  30:</span>       expressions.AddRange(</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31">  31:</span>           TraverseAndBuildTree(childNode, graphBuilder));</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32">  32:</span>     }</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33">  33:</span>   <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (graphBuilder.IsNode(root))</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34">  34:</span>     <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> childNode <span style="color: #0000ff">in</span> </pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35">  35:</span>         graphBuilder.GetSuccessors(root))</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36">  36:</span>     {</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37">  37:</span>       expressions.AddRange(</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38">  38:</span>           TraverseAndBuildTree(childNode, graphBuilder));</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum39">  39:</span>     }</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum40">  40:</span>   <span style="color: #0000ff">return</span> expressions.ToArray();</pre>
<!--CRLF-->

        <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum41">  41:</span> }</pre>
<!--CRLF--></div>
    </div>

    <div id="codeSnippetWrapper">25 – 40: We just traverse all nodes available and concatenate the lambda expressions into one big list. 
      <br />6 – 15: We get the values of path and email parameters from AST. 

      <br />17 – 22: We create the Call Expression. 

      <br />

      <br />You can then use this method like this:</div>

    <div id="codeSnippetWrapper">
      <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">LambdaExpression[] lambdas = TraverseAndBuildTree(root, graphBuilder);<br /><span style="color: #0000ff">foreach</span> (LambdaExpression lambda <span style="color: #0000ff">in</span> lambdas)<br />    lambda.Compile().DynamicInvoke();<br /></pre>
That’s it ! Spend sometime and play around with expressions ;)</div>
  </li>
</ol>

<p><em>Congrats</em>, you’ve reached your 2nd Checkpoint! 

  <br />

  <br />The technique described here works well with simple languages, but once your language gets more complicated, you will need a more structured design (and hopefully an automated process) which we will explore in the next part.</p>

<p></p>

<div></div>
<a href="https://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" target="_blank">Part I: Create the Grammar (What your users write)</a>. 

<br />Part II: Consume the abstract syntax tree (Do some action). [This post] 

<br />Path III: Compile your language into MSIL. 

<div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;"><br /><br /><script type="text/javascript">
</script>
<script type="text/javascript" src="https://tweetmeme.com/i/scripts/button.js"></script></div>]]></content:encoded>
    </item>
    <item>
      <title>Static Constructor… saved my day</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/static-constructor-saved-my-day</link>
      <pubDate>Thu, 21 May 2009 11:47:47 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/05/21/static-constructor-saved-my-day/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (Peace upon you)  I was designing some classes today and...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh (Peace upon you)</p>  <p>I was designing some classes today and I got stuck with an old problem I used to fix in an ugly way… </p>  <div id="codeSnippetWrapper">   <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">class</span> A<br />{<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> InitializeMe()<br />    {<br />        SomeClassManager.Listeners.Add(<span style="color: #0000ff">typeof</span>(A));<br />    }<br />}</pre>

  <br /></div>

<p>It would have been easy if:</p>

<ul>
  <li>You know when exactly to call InitializeMe() so that you get hooked up at the right time (before Listeners start iterating over its elements) </li>

  <li>You know what classes contain an InitializeMe function.. you can use reflection to get a list of all classes and then check whether they have the method or not.. –you can’t use interfaces as they don’t allow static members- </li>
</ul>

<p>What if you are designing a plugin model for an application, and want to call some static initialization code… again you will have to do reflection (which is slow).</p>

<p>And here comes static constructors!</p>

<div id="codeSnippetWrapper">
  <pre style="border-bottom-style: none; text-align: left; 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: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">class</span> A<br />{<br />    <span style="color: #0000ff">static</span> A()<br />    {<br />        InitializeMe();<br />    }<br /><br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> InitializeMe()<br />    {<br />        SomeClassManager.Listeners.Add(<span style="color: #0000ff">typeof</span>(A));<br />    }<br />}</pre>

  <br /></div>

<p></p>

<p>That simple.. <a href="https://msdn.microsoft.com/en-us/library/k9x6w0hc(VS.80).aspx" target="_blank">Static Constructor</a> [MSDN Link] will be called once this type is “loaded” which happens in .NET when your assembly first gets loaded into memory.. which is exactly the time I wanted… and it saved my day!</p>

<p>P.S, Check MEF (Managed Extensibility Framework) if you are interested in supporting plugins in ur apps, <a href="https://www.codeplex.com/MEF">https://www.codeplex.com/MEF</a>

  <br />And here is a getting started guide, <a href="https://code-inside.de/blog-in/2008/11/19/howto-first-steps-with-mef-hello-mef/">https://code-inside.de/blog-in/2008/11/19/howto-first-steps-with-mef-hello-mef/</a></p><div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;"><br /><br /><script type="text/javascript">
</script>
<script type="text/javascript" src="https://tweetmeme.com/i/scripts/button.js"></script></div>]]></content:encoded>
    </item>
    <item>
      <title>Invent your own language… using Oslo. Part I</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/invent-your-own-language-using-oslo-part-i</link>
      <pubDate>Thu, 14 May 2009 21:47:00 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/05/14/invent-your-own-language-using-oslo-part-i/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You)
  
Part I: Create the Grammar...]]></description>
      <content:encoded><![CDATA[
<p align="justify">Alsalam alikom wa ra7mat Allah wa barakatoh (Peace Upon You)</p>
  
<p align="center"><font size="5"><b>Part I: Create the Grammar        <br></b></font><font size="2"><i>…What your users will write…</i></font></p>
  
<p align="justify">If you are writing enterprise software, you probably came across this problem many times when you want to give IT Admins the ability to customize your application through some sort of scripts. The typical solution used to be VBS on windows and now PowerShell.. they both offer great ability to expose your public APIs and let others consume them. However, there is no way to customize the syntax to your application needs.</p>
  
<p align="justify">If you are writing a software that does some reporting and want to let users decide what template should be used when sending to every customer… wouldn’t it be nice if you let them write something like this:</p>
  <div class="csharpcode" align="justify">   
<pre class="alt"><span class="kwrd">Send</span> <span class="str">"D:\Reports\Templates\Regular.tmpl"</span> <span class="kwrd">to</span> sm1@hotmail.com, sm2@hotmail.com</pre>
</div>
<div class="csharpcode" align="justify">
  
<pre><span class="kwrd">Send</span> <span class="str">"D:\Reports\Templates\Special.tmpl"</span> <span class="kwrd">to</span> sm3@hotmail.com</pre>
</div>
<style type="text/css">






.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.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;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>

<p align="justify">Or how about a language that describes mathematical equations such as this: 
  <br>[[x, y] ; [1, 2] ; [2, x + 5]] * Func(2, 3, 5) – x2 + y * j ^ 3 + d - mul(3) 
  <br>This may look similar to Matlab syntax… in fact it’s. The first part <i>“[[x, y] ; [1, 2] ; [2, x + 5]]</i>” declares a 2x2 matrix. You can think of all sorts of symbols and operations that can be put into an equation… <a href="https://en.wikipedia.org/wiki/LaTeX" mce_href="https://en.wikipedia.org/wiki/LaTeX" target="_blank">Latex</a> and <a href="https://en.wikipedia.org/wiki/MathML" mce_href="https://en.wikipedia.org/wiki/MathML" target="_blank">MathML</a> [Wikipedia links] come into mind. Latex is not only for representing equations but it’s one feature of it. My point is, even if you want to reuse one of the existing languages out there in your application, you don’t have to rewrite a parser, buy a commercial package or get an OSS that might limit how you will be able to distribute your software. MGrammer (included in Oslo) is one elegant way to achieve this…</p>

<p align="justify">Or maybe you just don’t like the brackets () in C# and want to define a bracketless C# (VB maybe 8-) )…</p>

<p align="justify">There might be a debate around this and whether you will want each app to have it’s own Domain Specific Language (DSL) or is it better to have a common known scripting language (like VBS or PowerShell).. I won’t go into this debate actually here… but I would just say, “it depends”.</p>

<p align="justify">Let’s get to work, 
  <br>First thing you need to do is to get <a href="https://www.microsoft.com/downloads/details.aspx?FamilyID=f2f4544c-626c-44a3-8866-b2a9fe078956&amp;displaylang=en" mce_href="https://www.microsoft.com/downloads/details.aspx?FamilyID=f2f4544c-626c-44a3-8866-b2a9fe078956&amp;displaylang=en" target="_blank">Oslo installed</a> [Microsoft Downloads Link]. It needs SQL server (<a href="https://go.microsoft.com/?linkid=9314315" mce_href="https://go.microsoft.com/?linkid=9314315" target="_blank">Free Express Edition</a> works fine) –I installed it without SQL Server, it gave me an error but it’s ok as long as you don’t do any stuff that requires this-…</p>

<p align="justify">Oslo comes with a toolchain. We will actually make use of only 2 tools out of these; IntelliPad.exe (GUI tool) and Mg.exe (CMD tool)</p>

<p align="justify">We will be using IntelliPad to write MGrammar (.mg file), test it on our examplr data and make sure the syntax tree is in the right format.</p>

<p align="justify">We will then use Mg tool to compile the MGrammar file into .mgx file which represents you compiled language parser.. you can then use it from any .NET app.</p>

<p align="justify">We will make a grammar that recognizes that first language above, let’s call it <b>Reporter</b>. We will get to know MGrammer as we move on…</p>

<ol>
  
<li>
    <div align="justify">Open notepad (or notepad++ you geeky guys), type in this example and save as reportsData.bundle. </div>
    <div id="codeSnippetWrapper">
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">Send <span style="color: rgb(0, 96, 128);">"D:\Reports\Templates\Regular.tmpl"</span> <span style="color: rgb(0, 0, 255);">to</span> sm1@hotmail.com <br>Send <br><span style="color: rgb(0, 96, 128);">"D:\Reports\Templates\Special.tmpl"</span> <span style="color: rgb(0, 0, 255);">to</span> sm2@hotmail.com, sm3@hotmail.com <br></pre>
    </div>
    <div align="justify">This will work as our test data to make sure our grammar works fine. 
      <br></div>
  </li>

  
<li>
    <div align="justify">Open IntelliPad:<a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_8.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_8.png" mce_href="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_8.png"> 
        <br><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_3.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_3.png" style="border-width: 0px; display: inline;" title="image" alt="image" mce_src="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_3.png" border="0" width="470" height="112"></a> 
      <br>and save that empty file into Reporter.mg (you have to put the extension yourself or else IntelliPad won’t recognize the file as an MGrammar one) 
      <br>Now close IntelliPad, and double click on your file to open IntelliPad again in MGrammar Mode (it’s a bug if you ask me) 
      <br></div>
  </li>

  
<li>
    <div align="justify">The container in MG is the module element (much like namespaces or packages) 
      <br>inside a module you can have different elements (language, type, … etc) in MG we are interested in language element. 
      <br>Now go ahead and type your first lines 
      <br>
      <br>module Basic.Languages 
      <br>{ 
      <br>&nbsp;&nbsp;&nbsp; language Reporter 
      <br>&nbsp;&nbsp;&nbsp; { 
      <br>&nbsp;&nbsp;&nbsp; } 
      <br>} 
      <br></div>
  </li>

  
<li>
    <div align="justify">From MGrammar Mode menu, choose Tree Preview, this allows you to see how the data match as you start writing more code. It will ask you for an input file, go ahead and choose reportsData.bundle we created in step 1. 
      <br>Here is how it should look like now: 
      <br><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_10.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_10.png" mce_href="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_10.png"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_4.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_4.png" style="border-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="image" alt="image" mce_src="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_4.png" border="0" width="493" height="153"></a> 
      <br></div>
  </li>

  
<li>
    <div align="justify">Add this statement (syntax Main = any*; ) into language section, you will notice that the right tree got populated with a long list of characters. 
      <br>This statement is the entry point (Main), here it tells Mg that the syntax you are expecting is “any*” which basically matches with any number of any type of characters. 
      <br></div>
  </li>

  
<li>
    <div align="justify">We know that every statement we have starts with “Send” and has a “to” in the middle. So we can refine that any* matching with something more useful, like this: 
      <br>syntax Main = "Send" any* "to" any*; 
      <br>This instructs MG that you are expecting a word “Send” then some text then “to” then some text. 
      <br>You will notice that the preview tree now has a little structure… “Send” node and some characters in side then a “to” node with the rest of the statement.. 
      <br>But there is an error saying that the second statement didn’t match anything. 
      <br>This is because our main statement matches only one instance. 
      <br>We can define it like this: 
      <br>syntax Main = ("Send" any* "to" any*)+; 
      <br>or we can be more descent and define another syntax, say SendCommand.. like this 
      <br></div>
    <div id="codeSnippetWrapper">
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">syntax SendCommand = <span style="color: rgb(0, 96, 128);">"Send"</span> any* <span style="color: rgb(0, 96, 128);">"to"</span> any*;<br>syntax Main = SendCommand+;</pre>
      <br></div>
    <div align="justify">As you might have guessed, + means 1 or more, * means 0 or more (same as in Regular Expressions) and ? means 0 or 1 occurrence. 
      <br><a href="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_12.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_12.png" mce_href="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_12.png"><img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_5.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_5.png" style="border-width: 0px; display: inline;" title="image" alt="image" mce_src="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_5.png" border="0" width="483" height="126"></a> 
      <br></div>
  </li>

  
<li>
    <div align="justify">To make things a little bit more descent, we can define tokens for “Send” and “to”… Tokens can pretty much looked at as the basic building element for a statement or a matching pattern.. 
      <br>
      <br>Here is our code after defining the tokens:</div>
    <div id="codeSnippetWrapper">
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">token SendToken = <span style="color: rgb(0, 96, 128);">"Send"</span>;<br>token ToToken = <span style="color: rgb(0, 96, 128);">"to"</span>;<br>syntax SendCommand = SendToken any* ToToken any*;<br>syntax Main = SendCommand+; </pre>
      <br></div>
  </li>

  
<li>
    <div align="center">We need to match the quoted string (file path) but we don’t really bother for now to validate whether it’s a valid file path or not (Left as an exercise to the reader)&nbsp; <br>So we will go ahead and define a more complex token for that, 
      <br></div>
    <div align="center">
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">token AlphaNumeric = <span style="color: rgb(0, 128, 0);">'a'..'z' | 'A'..'Z'  | '0'..'9' | '_' | '-';</span><br>token Path = (AlphaNumeric | <span style="color: rgb(0, 128, 0);">':' | '\\' | '.')+;</span><br>token Email = (AlphaNumeric | <span style="color: rgb(0, 128, 0);">'@' | '.')+;</span><br>token QuotedPath = <span style="color: rgb(0, 128, 0);">'"' Path '"';</span><br><br>syntax SendCommand = SendToken QuotedPath ToToken Email;<br></pre>
    </div>
    <div align="center">
      <br></div>
    <div align="center">‘a’..’z’ defines range (as you might have guessed) other things are straight forward. 
      <br></div>
    <img src="https://msdnshared.blob.core.windows.net/media/TNBlogsFS/BlogFileStorage/blogs_msdn/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_6.png" original-url="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_6.png" style="border-width: 0px; display: block; float: none; margin-left: auto; margin-right: auto;" title="Preview Tree" alt="Preview Tree" mce_src="https://blogs.msdn.com/blogfiles/haythamalaa/WindowsLiveWriter/CreateyourownlanguagepartI_11387/image_thumb_6.png" border="0" width="230" height="278"> 
    
<p align="center"><i>This is how the right preview tree should look like by now.</i> 
      <br>
      <br></p>

    <div>
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" dir="ltr" id="codeSnippet">Things are getting to look better, right? <br>maybe you started to get the feeling it’s just like regular expressions,<br>in fact, it’s pretty much the same concept (String matching after all if <br>you want the truth) but writing in MGrammar gives you a lot of other <br>options when writing your rules than you have when matching with regEx<br>Also you will almost write 0 Lines of code to get your abstract syntax <br>tree (AST) built ;)</pre>
    </div>
    <div>
      <br></div>
  </li>

  
<li>
    <div align="justify">You will notice IntelliPad is complaining about the ‘,’ in the second example line. He’s right, we didn’t define how a list of emails may look like. 
      <br></div>
    <div id="codeSnippetWrapper">
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">syntax ListOfEmails = Email | ListOfEmails <span style="color: rgb(0, 96, 128);">","</span> Email;<br>syntax SendCommand = SendToken QuotedPath ToToken ListOfEmails;<br></pre>
      <br>This is a recursive rule. It recognizes <a href="mailto:%E2%80%9Ca@b.com" mce_href="mailto:“a@b.com">“a@b.com</a>” and <a href="mailto:%E2%80%9Ca@b.com,c@d.com" mce_href="mailto:“a@b.com,c@d.com">“a@b.com,c@d.com</a>” but it doesn’t recognize <a href="mailto:%E2%80%9Ca@b.com" mce_href="mailto:“a@b.com">“a@b.com</a>,” in other words, the “,” must be between 2 emails… which is exactly what we want. 
      <br></div>
  </li>

  
<li>
    <div>We are almost done, we now need to modify the resultant tree to make it look, well, better. </div>
    
<ul>
      
<li>
        <div>We need to fix the Send nodes so that we don’t actually put “Send” and “to” into the resultant tree. We will use projection, that’s we will define how do we want our rules to be projected (output) into the syntax tree… 
          <br></div>
        <div id="codeSnippetWrapper">
          
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">syntax SendCommand = SendToken p:QuotedPath ToToken list:ListOfEmails<br>                        =&gt; Send[p, list];<br></pre>
          <br>We named the match result of QuotedPath as p and ListOfEmails as list.. then the projection is Send[p, list]; would create a node called Send, with 2 children only. 
          <br>Here is the output: 
          <br></div>
        <div id="codeSnippetWrapper">
          
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">Main[<br>  [<br>    Send[<br>      <span style="color: rgb(0, 96, 128);">"\"</span>D:\\Reports\\Templates\\Regular.tmpl\<span style="color: rgb(0, 96, 128);">""</span>,<br>      ListOfEmails[<br>        <span style="color: rgb(0, 96, 128);">"sm1@hotmail.com"</span><br>      ]<br>    ],<br>    Send[<br>      <span style="color: rgb(0, 96, 128);">"\"</span>D:\\Reports\\Templates\\Special.tmpl\<span style="color: rgb(0, 96, 128);">""</span>,<br>      ListOfEmails[<br>        ListOfEmails[<br>          <span style="color: rgb(0, 96, 128);">"sm2@hotmail.com"</span><br>        ],<br>        <span style="color: rgb(0, 96, 128);">","</span>,<br>        <span style="color: rgb(0, 96, 128);">"sm3@hotmail.com"</span><br>      ]<br>    ]<br>  ]<br>]</pre>
          <br></div>
        <div id="codeSnippetWrapper">
          <br></div>
      </li>

      
<li>
        <div>Now we need to remove that “\” before and after the path… they don’t look neat. 
          <br></div>
        <div id="codeSnippetWrapper">
          
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">token QuotedPath = <span style="color: rgb(0, 128, 0);">'"' p:Path '"' =&gt; Path[p];</span><br>syntax SendCommand = SendToken p:QuotedPath ToToken list:ListOfEmails<br>                        =&gt; Send[valuesof(p), list];<br></pre>
          <br>First modification would make a node for every path and would remove the double quotes like this 
          <br>Path[ 
          <br>&nbsp;&nbsp;&nbsp; “D:\…..” 
          <br>] 
          <br>
          <br>valuesof(p) will extract the node contents and project it to the tree, in this case the contents is the path with singles double quotations (if you know what I mean). 
          <br></div>
      </li>

      
<li>
        <div>You can go ahead and try to modify the Email rule so that it doesn’t project the “,” and projects all emails in one list rather than nested lists. 
          <br></div>
      </li>
    </ul>
Here is how the tree looks like after all my modifications (with those left to you as exercise :)) 
    <br>
    <div>
      
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet">Commands[<br>  Send[<br>    <span style="color: rgb(0, 96, 128);">"D:\\Reports\\Templates\\Regular.tmpl"</span>,<br>    Emails[<br>      <span style="color: rgb(0, 96, 128);">"sm1@hotmail.com"</span><br>    ]<br>  ],<br>  Send[<br>    <span style="color: rgb(0, 96, 128);">"D:\\Reports\\Templates\\Special.tmpl"</span>,<br>    Emails[<br>      <span style="color: rgb(0, 96, 128);">"sm2@hotmail.com"</span>,<br>      <span style="color: rgb(0, 96, 128);">"sm3@hotmail.com"</span><br>    ]<br>  ]<br>]</pre>
    </div>
    <div>&nbsp;</div>
  </li>

  
<li>
    <div>Now, to compile use .mg file, open cmd, browse to the folder where reporter.mg file is and type this: 
      <br>"C:\Program Files\Microsoft Oslo SDK 1.0\Bin\mg.exe" reporter.mg 
      <br>This will generate a reporter.mgx file in the current directory. 
      <br>Now run: 
      <br>"C:\Program Files\Microsoft Oslo SDK 1.0\Bin\mgx.exe” reportsData.bundle -r:reporter.mgx 
      <br>This will generate the M structure (similar to AST with some key differences that we will explore in the next post) into a separate file. <b>Viola</b>!</div>
    <div>
      <br></div>
  </li>
</ol>

<p>Conclusion:</p>

<ol>
  
<li>We used IntelliPad to write and test our MGrammer. </li>

  
<li>MGrammar is a descriptive language to define the grammar of any language. </li>

  
<li>We need to define <b>module </b>and <b>language</b> sections in .mg file. </li>

  
<li><b>syntax </b>Main is the starting point, <b>syntax</b>es can be recursive. </li>

  
<li><b>tokens </b>are the basic elements. </li>

  
<li>We use projection rules on <b>syntax</b> and <b>token </b>to reshape the resultant tree. </li>

  
<li>MGrammar is awesome! </li>
</ol>

<p><i>Congrats</i>, you reached your first <b>Checkpoint</b>!</p>

<p>Here is the full listing of Reporter.mg file.</p>

<div id="codeSnippetWrapper">
  
<pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; font-family: 'Courier New',courier,monospace; direction: ltr; color: black; font-size: 8pt;" id="codeSnippet"><span style="color: rgb(0, 0, 255);">module</span> Basic.Languages<br>{<br>    language Reporter<br>    {<br>        token SendToken = <span style="color: rgb(0, 96, 128);">"Send"</span>;<br>        token ToToken = <span style="color: rgb(0, 96, 128);">"to"</span>;<br>        <br>        token AlphaNumeric = <span style="color: rgb(0, 128, 0);">'a'..'z' | 'A'..'Z'  | '0'..'9';</span><br>        token Path = (AlphaNumeric | <span style="color: rgb(0, 128, 0);">':' | '\\' | '.')+;</span><br>        token Email = (AlphaNumeric | <span style="color: rgb(0, 128, 0);">'@' | '.')+;</span><br>        token QuotedPath = <span style="color: rgb(0, 128, 0);">'"' p:Path '"' =&gt; Path{p};</span><br>        <br>        syntax ListOfEmails = e:Email =&gt; Emails[e]<br>                            | list:ListOfEmails <span style="color: rgb(0, 96, 128);">","</span> e:Email <br>                                =&gt; Emails[valuesof(list), e];<br>        syntax SendCommand = SendToken p:QuotedPath ToToken <br>                              list:ListOfEmails<br>                                =&gt; Send{valuesof(p), list};<br>        syntax Main = s:SendCommand+ =&gt; Main{Commands[valuesof(s)]};<br>        interleave whitespace = (<span style="color: rgb(0, 96, 128);">" "</span> | <span style="color: rgb(0, 96, 128);">"\r"</span> | <span style="color: rgb(0, 96, 128);">"\n"</span> | <span style="color: rgb(0, 96, 128);">"\t"</span>)+;<br>    }<br>}</pre>
</div>

<p>Path I: Create the Grammar (What your users write). [This post] 
  <br>Part II: Consume the abstract syntax tree (Do some action!). 
  <br>Path III: Compile your language into MSIL.</p>

<p><b>References:
    <br></b>A good tutorial for MGrammar: <a href="https://msdn.microsoft.com/en-us/library/dd441702.aspx" mce_href="https://msdn.microsoft.com/en-us/library/dd441702.aspx">https://msdn.microsoft.com/en-us/library/dd441702.aspx</a></p>

<p>You will also find some good documents in C:\Program Files\Microsoft Oslo SDK 1.0\Documents
  <br>2 in particular are interesting:
  <br>&nbsp;&nbsp;&nbsp; MGrammar Language Specification.docx
  <br>&nbsp;&nbsp;&nbsp; MGrammar in a Nutshell.docx</p>

<p>Have a nice time!</p>
<a href="https://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license"><img src="https://i.creativecommons.org/l/by-nc-sa/3.0/us/80x15.png" alt="Creative Commons License" style="border-width: 0pt;"></a><br><span xmlns:dc="https://purl.org/dc/elements/1.1/" mce_thref="https://purl.org/dc/dcmitype/Text" property="dc:title" rel="dc:type">Creating a .NET language</span> by <a href="https://blogs.msdn.com/haythamalaa" xmlns:cc="https://creativecommons.org/ns#" property="cc:attributionName" rel="cc:attributionURL">Haytham Abuel-Futuh</a> is licensed under a <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/us/" rel="license">Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License</a>.<br>Based on a work at <a href="https://blogs.msdn.com/haythamalaa/archive/2009/05/14/invent-your-own-language-using-oslo-part-i.aspx" xmlns:dc="https://purl.org/dc/elements/1.1/" rel="dc:source">blogs.msdn.com</a>.
<div class="wlWriterHeaderFooter" style="margin: 0px; padding: 0px;"><br><br><script type="text/javascript">
</script>
<script src="https://tweetmeme.com/i/scripts/button.js" type="text/javascript" mce_src="https://tweetmeme.com/i/scripts/button.js"></script></div>]]></content:encoded>
    </item>
    <item>
      <title>Friend Assemblies in .NET</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/friend-assemblies-in-net</link>
      <pubDate>Thu, 30 Apr 2009 19:03:00 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2009/04/30/friend-assemblies-in-net/</guid>
      <description><![CDATA[Alsalam alikom wa r7amat Allah wa barakatoh (Peace upon you..)  Long story short, you have AssemblyA...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa r7amat Allah wa barakatoh (Peace upon you..)</p>  <p>Long story short, you have AssemblyA (with source code) and AssemblyB… you want AssemblyB to be able to access internal members (types, data, methods.. etc) of AssemblyA…   <br>You can of course do that through Reflection but come on, it’s not a descent way to do it.. right??</p>  <p>.Net framework offers an attribute called InternalsVisibleToAttribute [<a mce_href="https://msdn.microsoft.com/en-us/library/0tke9fxk(VS.80).aspx" target="_blank" href="https://msdn.microsoft.com/en-us/library/0tke9fxk(VS.80).aspx">MSDN Link</a>]. It works on .NET 2.0 and above. An example grabbed from MSDN:</p>  <div id="codeSnippetWrapper">   <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">     <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #008000">// AssemblyA.dll</span></pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">using</span> System.Runtime.CompilerServices;</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">using</span> System;</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>&nbsp; </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span> [assembly:InternalsVisibleTo(<span style="color: #006080">"AssemblyB"</span>)]</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&nbsp; </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span> <span style="color: #008000">// internal by default</span></pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span> <span style="color: #0000ff">class</span> Class1 </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> {</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Test() </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     {</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         Console.WriteLine(<span style="color: #006080">"Class1.Test"</span>);</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     }</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> }</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>&nbsp; </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span> <span style="color: #008000">// public type with internal member</span></pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Class2 </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> {</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>     <span style="color: #0000ff">internal</span> <span style="color: #0000ff">void</span> Test() </pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     {</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>         Console.WriteLine(<span style="color: #006080">"Class2.Test"</span>);</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span>     }</pre><!--CRLF-->    <pre style="border-bottom-style: none; text-align: left; 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: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23">  23:</span> }</pre><!--CRLF--></div></div><p>This way, you can easily use Class1 and Class2.Test from AssemblyB code (obviously after adding reference to AssemblyA).  <br>I had 2 questions in mind after reading this,</p><ol>  <li>This can be some sort of a hack, I simply create another asesmbly that replaces your AssemblyB and violla, I’ve access to your internal members…     <br><i>A: You can include the public token key for AssemblyB in InternalsVisibleTo constructor</i></li>  <li>Can I selectively determine what classes (types) are visible to AssemblyB?</li></ol><p>.NET offers a class called StrongNameIdentityPermission [<a mce_href="https://msdn.microsoft.com/en-us/library/system.security.permissions.strongnameidentitypermission(VS.80).aspx" target="_blank" href="https://msdn.microsoft.com/en-us/library/system.security.permissions.strongnameidentitypermission(VS.80).aspx">MSDN Link</a>] Which can solve both of these problems actually.</p><p>I’ll not go into details for how to do it, but let me state what can you do using this class..  <br>You can require that calling assemblies (e.g AssemblyB) must be strongly signed using a private key you provide.. (Imagine you are building a multi-tier application and you don’t want anybody else to just call your public classes…)</p><p>StrongNameIdentityPermission works only on public classes, while InternalsVisibleToAttribute works on assembly level.</p><p>One last thing, it’s actually kind of funny.. InternalsVisibleTo constructor has 2 overloads (only in .NET3.5), the second accepted named parameter named AllIntenalsVisible (bool) which gave me hope that I can somehow selectively choose what to make visible… but… here is what I found in MSDN Documentation:</p><blockquote>  <p>This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.</p></blockquote><p>Catch you later…</p>]]></content:encoded>
    </item>
    <item>
      <title>Javascript Execution Context</title>
      <link>https://docs.microsoft.com/archive/blogs/haythamalaa/javascript-execution-context</link>
      <pubDate>Mon, 21 Jul 2008 08:24:00 GMT</pubDate>
      <dc:creator><![CDATA[Haytham Abuel-Futuh]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/haythamalaa/2008/07/21/javascript-execution-context/</guid>
      <description><![CDATA[Alsalam alikom wa ra7mat Allah wa barakatoh  I've faced a strange javascript exception when running...]]></description>
      <content:encoded><![CDATA[<p>Alsalam alikom wa ra7mat Allah wa barakatoh</p>  <p>I've faced a strange javascript exception when running a piece of code on Firefox/Safari, while it works fine on IE.</p>  <p>Here is what Firefox 3.0 says:</p>  <pre class="csharpcode">uncaught <span class="kwrd">exception</span>: [<span class="kwrd">Exception</span>... "Illegal <span class="kwrd">operation</span> <span class="kwrd">on</span> WrappedNative <br>prototype <span class="kwrd">object</span>" nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" <br>location: "JS frame :</pre><style type="text/css">.csharpcode, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.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;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style><p>After some searching, I found out that the problem is related to how Firefox/Safari handles the execution context for any called function.</p><p>Have a look on this code:</p><div class="csharpcode">  <pre><span class="lnum">   1:  </span><span class="kwrd">&lt;</span><span class="html">html</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">   2:  </span><span class="kwrd">&lt;</span><span class="html">head</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">   3:  </span><span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">language</span><span class="kwrd">="javascript"</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">   4:  </span><span class="kwrd">function</span> ExecuteRef(str, arg) { </pre>  <pre><span class="lnum">   5:  </span>    str(arg); </pre>  <pre><span class="lnum">   6:  </span>} </pre>  <pre><span class="lnum">   7:  </span><span class="kwrd">function</span> TestRun(objID){</pre>  <pre><span class="lnum">   8:  </span>    <span class="kwrd">var</span> obj = document.getElementById(objID); </pre>  <pre><span class="lnum">   9:  </span>    <span class="kwrd">var</span> funcRef = obj.parentNode.removeChild; </pre>  <pre><span class="lnum">  10:  </span>    ExecuteRef(funcRef, obj); </pre>  <pre><span class="lnum">  11:  </span>} </pre>  <pre><span class="lnum">  12:  </span><span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  13:  </span><span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  14:  </span>&nbsp;</pre>  <pre><span class="lnum">  15:  </span><span class="kwrd">&lt;</span><span class="html">body</span> <span class="attr">onload</span><span class="kwrd">="javascript:TestRun('delete1');"</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  16:  </span>&nbsp;</pre>  <pre><span class="lnum">  17:  </span><span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">='delete1'</span><span class="kwrd">&gt;</span>This should not appear<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  18:  </span><span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span>This should alwasy appear<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  19:  </span>&nbsp;</pre>  <pre><span class="lnum">  20:  </span><span class="kwrd">&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span> </pre>  <pre><span class="lnum">  21:  </span><span class="kwrd">&lt;/</span><span class="html">html</span><span class="kwrd">&gt;</span></pre></div><style type="text/css">.csharpcode, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.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;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style><p>What this code is supposed to do is to remove the first div -'delete1'- just when the page load. What actually happens on FF/Safari, is that nothing is deleted, while on IE the first div no longer appears.</p><p>We have 2 functions here, TestRun which given an ID for an element, it creates a reference for that object <i><font size="1">-ln 8-</font></i> and a reference for the removeChild method <i><font size="1">-ln 9-</font></i>, then passes them both to ExecuteRef, which will simply execute that <i><font size="1">-ln 5-</font></i>.</p><p>Why is that happening?   <br>Is it a problem with FF Javascript Engine AND Safari's one? or a problem with IE's ?</p><p>Actually it's not a mistake at all. It's all about how each engine determines the context of function execution. Let's speak in code language:</p><pre class="csharpcode"><span class="kwrd">var</span> funcRef = obj.parentNode.removeChild;</pre><p>funcRef is now a function pointer for removeChild.   <br>When you execute funcRef(args), it's translated as funcRef(this, args) in all javascript engine. this way it's easy for your function body to reference member variables of the object it's defined in.</p><p>The difference between IE &amp; FF/Safari engines, is that IE preserves the original caller object, in this case the "obj.parentNode".. so that when it translates funcRef(args) to funcRef(<font color="#0000ff">this</font>, args) it makes sure that "<font color="#0000ff">this</font>" is "obj.parentNode" and your code works fine. On the other hand, FF/Safari don't preserve the original caller, instead they just try to execute the function in the new context. Which in turn means that in funcRef(<font color="#0000ff">this</font>, args), <font color="#0000ff">this </font>refers to the ExecuteRef method itself, and as there is no function called removeChild defined inside ExecuteRef, the calling just fails giving that weird exception above.</p><p>I've explored different solutions for that problem. I'll try to summarize them here:</p><p>1. Explicitly declare the parameters of the function pointer:</p><ol>  <pre class="csharpcode"><span class="kwrd">var</span> funcRef = <span class="kwrd">function</span>(x) { obj.parentNode.removeChild (x); }</pre>  <style type="text/css">.csharpcode, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.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;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style></ol><p>This way, jscript engine will <b>have to</b> preserve the calling object -obj.parentNode-</p><p>2. If you already know the full parameters that will passed, you can get use of the <a mce_href="https://en.wikipedia.org/wiki/Closure_(computer_science)" target="_blank" href="https://en.wikipedia.org/wiki/Closure_(computer_science)">closure</a> feature JS supports.</p><blockquote>  <pre class="csharpcode"><span class="kwrd">var</span> funcRef = <span class="kwrd">function</span>(){obj.parentNode.removeChild(obj);};ExecuteRef(funcRef);</pre></blockquote><style type="text/css">.csharpcode, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.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;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style><p>3. Use Function.call / Function.apply:   <br>this requires that you pass the context to ExecuteRef..</p><blockquote>  <pre class="csharpcode"><span class="kwrd">function</span> ExecuteRef(context, str, arg) {    str.call(context, arg);}<span class="kwrd">function</span> TestRun(objID){    ....<br>    ExecuteRef(obj.parentNode, funcRef, obj);}</pre></blockquote><pre class="csharpcode"><font face="Tahoma">That's it so far.</font></pre><pre class="csharpcode"><font face="Tahoma">Thanks for reading!</font></pre><style type="text/css">.csharpcode, .csharpcode pre{	font-size: small;	color: black;	font-family: consolas, "Courier New", courier, monospace;	background-color: #ffffff;	/*white-space: pre;*/}.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;	width: 100%;	margin: 0em;}.csharpcode .lnum { color: #606060; }</style>]]></content:encoded>
    </item>
  </channel>
</rss>