<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/styles/xslt/rss.xslt"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:c9="http://channel9.msdn.com">
<channel>
	<title>Channel 9 - Discussions by bdesmet</title>
	<atom:link rel="self" type="application/rss+xml" href="http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS"></atom:link>
	<image>
		<url>http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.png</url>
		<title>Channel 9 - Discussions by bdesmet</title>
		<link>http://channel9.msdn.com/Niners/bdesmet/Discussions</link>
	</image>
	<description>Channel 9 keeps you up to date with the latest news and behind the scenes info from Microsoft that developers love to keep up with. From LINQ to SilverLight – Watch videos and hear about all the cool technologies coming and the people behind them.</description>
	<link>http://channel9.msdn.com/Niners/bdesmet/Discussions</link>
	<language>en</language>
	<pubDate>Wed, 22 May 2013 02:21:08 GMT</pubDate>
	<lastBuildDate>Wed, 22 May 2013 02:21:08 GMT</lastBuildDate>
	<generator>Rev9</generator>
	<c9:totalResults>0</c9:totalResults>
	<c9:pageCount>0</c9:pageCount>
	<c9:pageSize>0</c9:pageSize>
	<item>
		<title>Coffeehouse - Has anyone considered writing an MSIL interpreter in C#?</title>
		<description><![CDATA[<p>@<a href="/Forums/Coffeehouse/Has-anyone-considered-writing-an-MSIL-interpreter-in-C#cb6b6fc182f084798998a9f14015c3880">evildictaitor</a>: Don't underestimate the C# compiler wrt the switch statement. A few examples:</p><ul><li>Switches with a low number of cases can become if statements. </li><li>Switches with reasonably contigious ranges can&nbsp;be turned into the switch instruction. Holes can be patched with a jump target to the default case. </li><li>Switches with a non-zero case can get rebased using an add or sub instruction in order to use the switch instruction again. </li><li>Switches with sparse case values can be turned into nested/hierarchical switch instructions, using any of the above techniques applied. </li><li>Switches on string cases can become if statements, but if there are more than a treshold number (currently 6 IIRC), a Dictionary&lt;string, int&gt; is lazily constructed, using the int values for switch instruction use. </li></ul><p>Just a little example:</p><p><pre class="brush: csharp">[MethodImpl(MethodImplOptions.NoInlining)]
static void Bar(int i, out int j)
{
    switch (i)
    {
        case 17:
            j = 1;
            break;
        case 19:
            j = 2;
            break;
        case 20:
            j = 3;
            break;
        case 25:
            j = 4;
            break;
        default:
            j = 0;
            break;
    }
}</pre></p><p>Code above got compiled without /o&#43;, so you'll see redundant nop instruction and excessive st/ld pairs in the IL. Using SOS, we dump the IL and the generated assembler:</p><p><pre class="brush: text">0:004&gt; !name2ee switch.exe Program.Bar
Module:      000007ff000330d8
Assembly:    switch.exe
Token:       0000000006000002
MethodDesc:  000007ff000340e0
Name:        Program.Bar(Int32, Int32 ByRef)
JITTED Code Address: 000007ff001501d0
0:004&gt; !dumpil 000007ff000340e0
ilAddr = 000000000137207c
IL_0000: nop 
IL_0001: ldarg.0 
IL_0002: stloc.0 
IL_0003: ldloc.0 
IL_0004: ldc.i4.s 17
IL_0006: sub 
IL_0007: switch (IL_0023, IL_0037, IL_0028, IL_002d)
IL_001c: ldloc.0 
IL_001d: ldc.i4.s 25
IL_001f: beq.s IL_0032
IL_0021: br.s IL_0037
IL_0023: ldarg.1 
IL_0024: ldc.i4.1 
IL_0025: stind.i4 
IL_0026: br.s IL_003c
IL_0028: ldarg.1 
IL_0029: ldc.i4.2 
IL_002a: stind.i4 
IL_002b: br.s IL_003c
IL_002d: ldarg.1 
IL_002e: ldc.i4.3 
IL_002f: stind.i4 
IL_0030: br.s IL_003c
IL_0032: ldarg.1 
IL_0033: ldc.i4.4 
IL_0034: stind.i4 
IL_0035: br.s IL_003c
IL_0037: ldarg.1 
IL_0038: ldc.i4.0 
IL_0039: stind.i4 
IL_003a: br.s IL_003c
IL_003c: ret </pre></p><p>Notice the sub instruction to rebase the switch statement and the insertion of case 18 equals default case by a jump to IL_0037 in the switch instruction's table. Case 25 which is further away from the switch range encountered here is handled by fall-through after the switch instruction and a jump to IL_0032. If you'd have a cluster of cases close to 25 again, you'd see another switch instruction being born.</p><p>I'll leave it as a good SOS exercise to the reader to perform a !U on the JITTED code address for different platform architectures and unravvle the assember generated...</p><p><pre class="brush: text">0:004&gt; !u 000007ff001501d0
Normal JIT generated code
Program.Bar(Int32, Int32 ByRef)
Begin 000007ff001501d0, size 62
...</pre></p><p>Also don't forget order of cases in a switch statement doesn't matter, so the techniques described above can be applied even if cases don't appear in a sorted lexical order.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/Coffeehouse/Has-anyone-considered-writing-an-MSIL-interpreter-in-C/1568c51394f044ba91829f150031048b#1568c51394f044ba91829f150031048b</link>
		<pubDate>Sun, 03 Jul 2011 02:58:28 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/Coffeehouse/Has-anyone-considered-writing-an-MSIL-interpreter-in-C/1568c51394f044ba91829f150031048b#1568c51394f044ba91829f150031048b</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>22</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - LINQ Question</title>
		<description><![CDATA[<p><blockquote>
<div class="quoteAuthor">Ion Todirel wrote:</div>
<div class="quoteBody">&#65279;
<blockquote>
<table class="quoteTable">
<tbody>
<tr>
<td valign="top" width="10"><img src="/Themes/AlmostGlass/images/icon-quote.gif"></td>
<td class="txt3"><strong>littleguru wrote:</strong>
<hr size="1">
<i>&#65279;does somebody know how to detect if a type is an anonymous type?</i></td>
</tr>
</tbody>
</table>
</blockquote>
why would you want that for?</div>
</blockquote>
<br /><br />Well,&nbsp;a possible use is in the creation of <strong>custom LINQ query provider</strong>s that are aiming to achieve some
<strong>intelligence </strong>level. Say you have a query like this:<br /><br /><font face="Courier New">var res1 = from p in products select new {&nbsp;p.Name, p.Price = p.UnitPrice&nbsp;}</font><br /><br />Now assume you're using this query as the basis for another query (which results in calling IQueryProvider's CreateQuery method again, causing to 'extend' the current query's expression tree), like this:<br /><br /><font face="Courier New">var&nbsp;res2 = from p in res1 orderby p.Price descending select new { p.Name, Discount&nbsp;= p.Price * 0.05 }</font><br /><br />In this second query, you're referring to p.Price, which in reality is the original datasource's (i.e. variable product) p.UnitPrice 'column'. With anonymous types, your query provider could figure this out since it has the guarantee that what comes in to the
 anonymous type from the first query will be the same as what comes out when using it in a second query. Therefore, projections based on anonymous types are the only real gateways towards smart providers that allow some level of composability. So, when iterating
 over res2, the composed query above could be translated into one single query statement rather than one that goes to the server and another one that's executing in a LINQ to Objects fashion (sample assuming we're targeting SQL):<br /><br /><font face="Courier New">SELECT [Name], 0.05 * [UnitPrice] AS&nbsp;[Discount] FROM Products ORDER BY [UnitPrice] DESCENDING</font><br /><br />On the other hand, assume you have something like this:<br /><br /><font face="Courier New">var res1 = from p in products select new MyProduct(p.Name, p.UnitPrice)</font><br /><br />Now, if you start to write a query against res1's results, there's no way to get to know where p.Name and p.UnitPrice have gone through. Maybe MyProduct has properties with
<em>similar</em> names, but that's not enough. Even if you'd use this:<br /><br /><font face="Courier New">var res1 = from p in products select new MyProduct { p.Name, Price = p.UnitPrice }</font><br /><br />and refer to MyProduct's Name or Price property subsequently, there's no guarantee whatsoever that values assigned to those properties are retrieved immutably.<br /><br />So, to wrap up: an intelligent query parser could benefit from the knowledge that anonymous types expose certain semantics wrt their properties, while types provided by the developer herself put an end to query intelligence (and therefore composability).<br /><br />P.S. I've been working on some of this stuff lately on a query parser that I want to be quite intelligent; I've the same problem as littleguru mentioned but am using the heuristic I posted here at the moment to detect anonymous types.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/257443-LINQ-Question/e5afd6a39acb4f60ad999dfa008a2641#e5afd6a39acb4f60ad999dfa008a2641</link>
		<pubDate>Sat, 08 Sep 2007 21:53:58 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/257443-LINQ-Question/e5afd6a39acb4f60ad999dfa008a2641#e5afd6a39acb4f60ad999dfa008a2641</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>15</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - LINQ Question</title>
		<description><![CDATA[<p><p dir="ltr">A few answers that might be useful. In C# 3.0 anonymous types are immutable indeed; there are only getters while the properties can only be set through the constructor (which is created by the compiler). In VB 9.0 anonymous types are mutable except
 for fields marked with the Key keyword. This is the addition Paul Vick was referring to.<br /><br />Concerning detection of anonymous types, you're right there's no metadata available that ensures a type is a dynamically cooked up type, neither is there an API to help you out (which wouldn't give any guarantuees anyhow if there's no dedicated piece of metadata).
 A good heuristic to determine anonymous types is:<br /><br />- Check for the CompilerGeneratedAttribute using reflection<br />- Type name contains &quot;AnonymousType&quot;<br />- Type name starts with either &quot;&lt;&gt;&quot; or &quot;VB$&quot; (these prefixes cannot be used by code written by a developer)<br />- Type is generic<br />- Type definition is private (and sealed in C# 3.0)<br /><br />The code below illustrates this:<br /><br /><font face="Courier New">static TypeAttributes s_ta = TypeAttributes.NotPublic;</font></p>
<p dir="ltr"><font face="Courier New">static bool IsAnonymousType(Type t)<br />{<br />&nbsp;&nbsp;&nbsp; return&nbsp;&nbsp;&nbsp; t.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Length == 1<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; t.IsGenericType<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; t.Name.Contains(&quot;AnonymousType&quot;)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; (t.Name.StartsWith(&quot;&lt;&gt;&quot;) || t.Name.StartsWith(&quot;VB$&quot;))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;&amp; (t.Attributes &amp; s_ta) == s_ta;<br />}<br /><br /></font><font face="Arial">An alternative is to construct a regex to check the name to be valid against C# 3.0 or VB 9.0 anonymous type naming conventions - which unfortunately aren't official by any means.<br /><br />Enjoy!</font></p></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/257443-LINQ-Question/a74e7dd650984fc58fd49dfa008a24c9#a74e7dd650984fc58fd49dfa008a24c9</link>
		<pubDate>Fri, 07 Sep 2007 00:34:15 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/257443-LINQ-Question/a74e7dd650984fc58fd49dfa008a24c9#a74e7dd650984fc58fd49dfa008a24c9</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>15</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - The opposite of ??</title>
		<description><![CDATA[<p>Actually, you can let the compiler infer the generic type info for you, as illustrated in the following piece of code (C# 3.0):</p>
<p><font face="Courier New">using System;</font></p>
<p><font face="Courier New">class Program<br />{<br />&nbsp;&nbsp; static void Main()<br />&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bar bar = new Bar();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /*<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar.Foo = new Foo();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar.Foo.Bar = new Bar();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar.Foo.Bar.Foo = new Foo();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */<br /><br /><font color="#ff0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Foo foo = bar.IsNull(b =&gt; b.Foo).IsNull(f =&gt; f.Bar).IsNull(b =&gt; b.Foo);</font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(foo == null);<br />&nbsp;&nbsp; }<br />}</font></p>
<p><font face="Courier New">class Bar<br />{<br />&nbsp;&nbsp; public Foo Foo {get;set;}<br />}</font></p>
<p><font face="Courier New">class Foo<br />{<br />&nbsp;&nbsp; public Bar Bar {get;set;}<br />}</font></p>
<p><font color="#008000"><font face="Courier New">public static class ObjectExtension<br />{<br />&nbsp;&nbsp; public static R IsNull&lt;T,R&gt;(this T value, Func&lt;T,R&gt; func)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where T : class<br />&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return value ==&nbsp;null ? default(R) : func(value);<br />&nbsp;&nbsp; }<br />}</font><br /></font><br />It's not as easy as some kind of proposed&nbsp;?. operator but it could serve as a&nbsp;workaround, especially when writing query expressions. All it's doing is shortcutting a clumsy nesting of ? :&nbsp;operator uses:<br /><br /><font face="Courier New" color="#ff0000">Foo foo = bar != null ? (bar.Foo != null ? (bar.Foo.Bar != null ? (bar.Foo.Bar.Foo != null ? bar.Foo.Bar.Foo : null) : null) : null) : null;</font><br /><br />Concerning the ?. operator I do recognize the value as well but I feel it's a bit too late in the Orcas release cycle to design and implement this properly, especially to make it consistent when used in combination with properties and fields, indexers, method
 invocations, etc, as well as tools functionality such as IntelliSense.<br /><br />Cheers,<br />-Bart</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/257394-The-opposite-of-/64efad881cd2497bb7b39dfa0089e2d3#64efad881cd2497bb7b39dfa0089e2d3</link>
		<pubDate>Wed, 05 Sep 2007 00:34:54 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/257394-The-opposite-of-/64efad881cd2497bb7b39dfa0089e2d3#64efad881cd2497bb7b39dfa0089e2d3</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>23</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - basic C# question</title>
		<description><![CDATA[<p><p dir="ltr">Maybe it's useful to show a more complete sample containing an event definition as well.<br /><br />Let's start with the concept of a <strong>delegate</strong>, which is an object that can point to a method with a given signature:<br /><br /><font face="Courier New">delegate int BinOp(int a, int b);<br /></font><br />BinOp can point to any method that takes two ints and returns an int. For example:<br /><br /><font face="Courier New">int Add(int a, int b) { return a &#43; b; }<br /></font><br />can be pointed at by a BinOp delegate, like this:<br /><br /><font face="Courier New">BinOp add = new BinOp(Add);<br /></font><br />or even (starting from C# 2.0):<br /><br /><font face="Courier New">BinOp add = Add; //the same as the code above, just more convenient</font><br /><br />One can call the target method a delegate is pointing at by using regular method call syntax on the delegate instance, like this:<br /><br /><font face="Courier New">add(1,2); //notice the casing of <u>a</u>dd - we're not calling
<u>A</u>dd directly but through the delegate created above<br /></font><br />For the following, consider a delegate defined like this:<br /><br /><font face="Courier New">delegate void ChangedEventHandler(object o, ChangedEventArgs e);</font><br /><br /><u>Note:</u> there's a generic EventHandler class that provides a shortcut to create typical *EventHandler delegates.<br /><br /><br />Now we can cook up an <strong>event</strong> using this delegate. This allows users of a class to &quot;subscribe&quot; to an event by pointing at a method with the appropriate signature:<br /><br /><font face="Courier New">class Controller<br />{<br />&nbsp;&nbsp; public event ChangedEventHandler StateChanged; //events&nbsp;have a delegate type, in this case ChangedEventHandler<br /><br />&nbsp;&nbsp; public void DoWork()<br />&nbsp;&nbsp; {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //do work<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (StateChanged != null) //make sure someone has subscribed<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; StateChanged(this, new ChangedEventArgs(...)); //delegate calling syntax<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //do more work<br />&nbsp;&nbsp; }<br />}<br /></font><br />This allows the user of the class to subscribe to the event like this:<br /><br /><font face="Courier New">Controller ctrl = new Controller();<br />ctrl.StateChanged &#43;= new ChangedEventHandler(ctrl_Changed);<br /></font><br />where ctrl_Changed is defined like this:<br /><br /><font face="Courier New">void ctrl_Changed(object o, ChangedEventArgs e)<br />{<br />&nbsp;&nbsp; //do something to handle the event; o points at the object that raised the event - you can cast it back to Controller<br />}<br /></font><br />Or, using anonymous method syntax in C# 2.0 and higher:<br /><br /><font face="Courier New">Controller ctrl = new Controller();<br />ctrl.StateChanged &#43;= delegate (object o, ChangedEventArgs e)<br />{<br />&nbsp;&nbsp; //do something to handle the event; o points at the object that raised the event - you can cast it back to Controller<br />};<br /></font><br />Hope this helps.</p></p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/257232-basic-C-question/8edb060c405949f0a6b09dfa00b0a45a#8edb060c405949f0a6b09dfa00b0a45a</link>
		<pubDate>Tue, 04 Sep 2007 23:38:55 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/257232-basic-C-question/8edb060c405949f0a6b09dfa00b0a45a#8edb060c405949f0a6b09dfa00b0a45a</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>12</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
	<item>
		<title>Tech Off - Philosophical INotifyPropertyChanged question</title>
		<description><![CDATA[<p>It's a good question indeed but I do have a few concerns with the design of the properties you're describing. If setting one property causes another property's value&nbsp;to be changed as well, this is pretty much to be considered as a side-effect and maybe
 a property isn't the best choice in that case (in more complicated situations you'll have to make sure you're not causing a cycle in the chain of setter calls). For more info, check the Framework Design Guidelines on
<a href="http://msdn2.microsoft.com/en-us/library/ms229054.aspx">Choosing Between Properties and Methods</a>.<br /><br />Nevertheless, there's no silver bullet to this kind of problems - the one thing that's key is to be
<strong>consistent</strong>. Concerning your WPF sample, I'd rely on the SelectedWidget property to become null automatically.</p>]]></description>
		<link>http://channel9.msdn.com/Forums/TechOff/257406-Philosophical-INotifyPropertyChanged-question/6330254153654c0ba7e39dfa0089ec8f#6330254153654c0ba7e39dfa0089ec8f</link>
		<pubDate>Tue, 04 Sep 2007 23:23:21 GMT</pubDate>
		<guid isPermaLink="false">http://channel9.msdn.com/Forums/TechOff/257406-Philosophical-INotifyPropertyChanged-question/6330254153654c0ba7e39dfa0089ec8f#6330254153654c0ba7e39dfa0089ec8f</guid>
		<dc:creator>Bart De Smet</dc:creator>
		<slash:comments>6</slash:comments>
		<wfw:commentRss>http://channel9.msdn.com/Niners/bdesmet/Discussions/RSS</wfw:commentRss>
	</item>
</channel>
</rss>