<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://bloggingabout.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mielz' Thingamajig : C# 3.0</title><link>http://bloggingabout.net/blogs/emile/archive/tags/C_2300_+3.0/default.aspx</link><description>Tags: C# 3.0</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Practical use of the operator "explicit" in XLinq</title><link>http://bloggingabout.net/blogs/emile/archive/2005/12/19/10621.aspx</link><pubDate>Mon, 19 Dec 2005 12:21:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:10621</guid><dc:creator>Emile Bosch</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/emile/rsscomments.aspx?PostID=10621</wfw:commentRss><comments>http://bloggingabout.net/blogs/emile/archive/2005/12/19/10621.aspx#comments</comments><description>&lt;p&gt;Probably everyone knows this already , but i am going to waste some energy on it anyway, as it raises way too many&amp;nbsp;eyebrows lately&amp;nbsp;:)&amp;nbsp;As long as C# exists it supports the type conversion operator &amp;quot;explicit&amp;quot;. Neverheless i've never seen any practical use on the operator before.&amp;nbsp; Probably because it has the tendency to release the &amp;quot;Huh?!1?&amp;quot; effect among developers.&amp;nbsp;Until i started working with XLinq for my &amp;quot;Visual Fx&amp;quot; objectbuilder strategy. I wrote this little example to clarify:&lt;/p&gt;
&lt;font size="2"&gt;
&lt;p&gt;&lt;font face="Courier New" size="2"&gt;public&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; Example(XElement node)&lt;br /&gt;
{&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&amp;nbsp; &amp;nbsp; string&lt;/font&gt;&lt;font size="2"&gt; str = (&lt;/font&gt;&lt;font size="2"&gt;string&lt;/font&gt;&lt;font size="2"&gt;)node.Attribute(&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;Name&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt;); //Convert it to a string&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;/font&gt;&lt;font size="2"&gt; number = (&lt;/font&gt;&lt;font size="2"&gt;int&lt;/font&gt;&lt;font size="2"&gt;)node.Attribute(&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;Number&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;);&amp;nbsp; //Convert it to an int &lt;br /&gt;
}&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;As you can see here, the Attribute property of type XAttribute can be casted to an String aswel to an Int (and several more types like DateTime, TimeSpan, Guid etc). Which is unusual since these types are not in the inheritence chain of XAttribute. This behaviour is conceived by using the &amp;quot;explicit&amp;quot; operator.&amp;nbsp;By implementing this operator, which is easy, &amp;nbsp;you are responsible for converting the current type to your target type by doing so: &lt;br /&gt;
&lt;br /&gt;
&lt;font face="Courier New"&gt;class XAttribute&amp;nbsp;{&lt;br /&gt;
&amp;nbsp; //More&amp;nbsp;really exciting code&lt;br /&gt;
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&amp;nbsp; public static explicit operator string(XAttribute a)&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (a == null)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return null;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return a.value;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;}&lt;br /&gt;
&lt;/font&gt;&lt;br /&gt;
Now this isn't exactly rocket science&amp;nbsp;or anything but good to know when you encouter this behaviour in your new Xlinq applications. I've somewhat mixed feelings on this operator since there is no visual intellisense support for it available, thus there is no way of telling wheter you can simply cast an type using that operator&amp;nbsp;or need to implement your own custom casting. Except for that minor point it certainly eases writing Xlink queries. Also, there is an &amp;quot;implicit&amp;quot; operator which does a&amp;nbsp; similiar thing except you don't have to write the cast explicitly, thus typing:&lt;br /&gt;
&lt;br /&gt;
&lt;font face="Courier New"&gt;string&lt;font size="2"&gt; str = &lt;/font&gt;&lt;font size="2"&gt;node.Attribute(&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;Name&amp;quot;&lt;/font&gt;&lt;font size="2"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;will suffice.&amp;nbsp;(Note that the XAttribute doesnt implement the &amp;quot;implicit&amp;quot; operator so this won't compile)&lt;/p&gt;
&lt;p&gt;More information on explicit and implicit on msdn: &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfexplicit.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfexplicit.asp&lt;/a&gt;&lt;/p&gt;
&lt;/font&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=10621" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/emile/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category><category domain="http://bloggingabout.net/blogs/emile/archive/tags/Basic+Knowledge/default.aspx">Basic Knowledge</category></item><item><title>Query your WMI with ease using WMILinq!</title><link>http://bloggingabout.net/blogs/emile/archive/2005/12/12/10514.aspx</link><pubDate>Mon, 12 Dec 2005 01:32:00 GMT</pubDate><guid isPermaLink="false">813b6dfd-644e-4573-a816-eebab56ba0d0:10514</guid><dc:creator>Emile Bosch</dc:creator><slash:comments>30</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://bloggingabout.net/blogs/emile/rsscomments.aspx?PostID=10514</wfw:commentRss><comments>http://bloggingabout.net/blogs/emile/archive/2005/12/12/10514.aspx#comments</comments><description>&lt;div&gt;When I first played with Linq I didnt move away from my computer for two days, neglecting my friends and family, while gazing at&amp;nbsp;Linqs&amp;nbsp;tremendous power to take over the world. I was particulary interrested in its sexy feature to convert an Linq expression into an so called expression tree, as you can experience with DLinq. As you probably already know,&amp;nbsp; when you write an linq query the default compiler behaviour converts the query to an delegate. But this behaviour radically changes when you assign your query to an Expression&amp;lt;T&amp;gt; class. In this way the compiler doesn&amp;rsquo;t convert your query into an delegate but into an expression tree. It is then up to you on how to process this tree in something useful and come up with the right output. There is an excellent article on this behaviour from Ian Griffiths you can find its link below.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;&lt;strong&gt;Making life easier with WmiLinq&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;To show the use and power of expression trees I wrote a basic WMILinq library which makes it possible to query directly against WMI store using an LINQ like syntax. Internally it works somewhat similar as Dlinq, it translates an LINQ query to an expression tree and feeds it to an Query Builder. This analyzes the expression tree and generates an WMI WQL compatible query, which is fed to the WMI library of .NET. When this query is executed the results are mapped back using reflection into entities. I kept it all a bit simple by only supporting the basic WHERE clauses, thus joins are not supported.&amp;nbsp;And for clarity i havent included any query validation, optimization or whatsoever.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;Its all pretty&amp;nbsp;beta and purely intended as an expression tree sample and educational project.&amp;nbsp;But it sure is interesting to see what is possible with expression trees with such a limited implementation. If microsoft isnt building the same thing right now i might continue developing it though.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;Lets say we want to query some process information.&amp;nbsp; For instance, query the processes which have more than 20 kernel threads:&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;using (WmiContext context = new WmiContext(@&amp;quot;\\localhost&amp;quot;))&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; var query = from process in context.Source&amp;lt;&lt;u&gt;Win32_Process&lt;/u&gt;&amp;gt;() where &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; process.ThreadCount &amp;gt; 20 &amp;amp;&amp;amp; process.Name.Contains(&amp;quot;.exe&amp;quot;) select process;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; foreach (Win32_Process process in query)&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; { &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(process.Name + process.CreationDate);&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; }&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp; //The above results in the following WQL Query:&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp; //SELECT * FROM Win32_Process WHERE ThreadCount &amp;gt; 20&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;}&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Or start doing things which make totally no sense but to show off our cross linq languages.&lt;/div&gt;
&lt;div&gt;For example: Query the desktops and&amp;nbsp; save the result in xml.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;using (WmiContext context = new WmiContext(@&amp;quot;\\localhost&amp;quot;))&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; var doc = new XDocument(&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new XElement(&amp;quot;Desktops&amp;quot;, (from desktop in context.Source&amp;lt;&lt;u&gt;Win32_Desktop&lt;/u&gt;&amp;gt;() &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where desktop.Name.Contains(&amp;quot;service&amp;quot;) select &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new XElement(&amp;quot;Desktop&amp;quot;, new XAttribute(&amp;quot;name&amp;quot;, desktop.Name)))));&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; //The above rusults in the folllowing WQL Query:&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&amp;nbsp; //SELECT * FROM Win32_Desktop WHERE Name Like &amp;lsquo;%service%&amp;rsquo;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;}&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;As you can see its easy to query every piece of&amp;nbsp; information that is available in WMI. Hardware information, network information, event logs, performance counters, installed software and so on. I never had much field experience on WMI, and i am not sure how long&amp;nbsp;WMI is going to last, but WMI seemed like an easy enough query language to create an sample for. &lt;/div&gt;
&lt;div&gt;&lt;font color="#ff9900"&gt;&amp;nbsp;&lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&lt;b&gt;Create your prototype entities with WmiClassGen&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;As you can see in the examples, im querying the WMI store using the prototype entities like &lt;u&gt;Win32_Process&lt;/u&gt; and &lt;u&gt;Win32_Desktop&lt;/u&gt; , these are used for specifying the WHERE clause and for mapping the flat data back into entities. Which are somewhat similar as the table classes you use with Dlinq.&amp;nbsp; Al this neatness isnt much worth if you have to write the WMI entities yourself. Its not much fun, error prone and way too much work for us lazy developers. Therefor I made little tool which generates the WMI entity for you using codedom. The tool does the same thing for WMI as SQLMetal does for databases. Creating an prototype class is as&amp;nbsp; easy like this.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;font face="Courier New"&gt;&amp;gt; &lt;b&gt;WmiClassGen.exe&lt;/b&gt; /wmi:Win32_UserAccount /out:&amp;rdquo;c:\Win32_UserAccount.cs&amp;rdquo;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;The first switch here tells the tool to create a prototype class for the Win32_UserAccount object. The out switch tells the tool where to save the result. There are a couple more switches&lt;/div&gt;
&lt;div&gt;which you can easily query by passing no parameters at all.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;And yes,&amp;nbsp; I made the same mistake as Dlinq I used attributes for mapping :) If I can find the time the next version of WMILinq it will have support for namespaces, WMI events, method invocations, associations and&amp;nbsp;probably XML based mapping. &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;a href="http://bloggingabout.net/UserFiles/Emile Bosch/File/Code/WMILINQ.zip"&gt;You can download&amp;nbsp; the initial version&amp;nbsp; here!&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;
Remember you'll need VS2005 RTM and Linq RTM. &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;strong&gt;Some related reading material on this subject&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;Ian G going mad on expression trees: &lt;a href="http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees"&gt;http://www.interact-sw.co.uk/iangblog/2005/09/30/expressiontrees&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;WMI at MSDN: &lt;a href="http://msdn.microsoft.com/library/en-us/wmisdk/wmi/wmi_start_page.asp"&gt;http://msdn.microsoft.com/library/en-us/wmisdk/wmi/wmi_start_page.asp&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;The folder documentation contains a (dutch) walktrough. I will write an english one tonight.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://bloggingabout.net/aggbug.aspx?PostID=10514" width="1" height="1"&gt;</description><category domain="http://bloggingabout.net/blogs/emile/archive/tags/C_2300_+3.0/default.aspx">C# 3.0</category></item></channel></rss>