<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Umar's Blog</title>
	<atom:link href="http://umarja.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://umarja.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Mon, 27 Apr 2009 13:38:57 +0000</lastBuildDate>
	<language>it</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='umarja.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Umar's Blog</title>
		<link>http://umarja.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://umarja.wordpress.com/osd.xml" title="Umar&#039;s Blog" />
	<atom:link rel='hub' href='http://umarja.wordpress.com/?pushpress=hub'/>
		<item>
		<title>estendere un&#8217;applicazione asp.net con plugin/estensioni</title>
		<link>http://umarja.wordpress.com/2009/04/27/estendere-unapplicazione-aspnet-con-pluginestensioni/</link>
		<comments>http://umarja.wordpress.com/2009/04/27/estendere-unapplicazione-aspnet-con-pluginestensioni/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 13:15:00 +0000</pubDate>
		<dc:creator>umarja</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Extension]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://umarja.wordpress.com/2009/04/27/estendere-unapplicazione-aspnet-con-pluginestensioni/</guid>
		<description><![CDATA[Un’applicazione ben scritta è un’applicazione che piace, ma un’applicazione ben scritta e che può essere migliorata, è il massimo. In questo tutorial spiegherò come creare e mettere in produzione un completo framework per la creazione di plugin o estensioni (io preferisco chiamarle estensioni) che permetterà di estendere e/o modificare il comportamento delle proprie applicazioni scrivendo [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=43&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Un’applicazione ben scritta è un’applicazione che piace, ma un’applicazione ben scritta e che può essere migliorata, è il massimo. In questo tutorial spiegherò come creare e mettere in produzione un completo framework per la creazione di plugin o estensioni (io preferisco chiamarle estensioni) che permetterà di estendere e/o modificare il comportamento delle proprie applicazioni scrivendo una decina di righe di codice.</p>
<p><span id="more-43"></span></p>
<p>Prima di cimentarci nella scrittura di codice è bene chiarire due concetti: che cosa si intende per estensione e come funziona il framework.</p>
<p>Per estensione si intende una libreria che viene inserita nella cartella Bin dell’applicazione. Per semplicità e per maggiori performance ho preferito caricare una sola estensione da ogni assembly, quindi ogni assembly può contenere solo un’estensione.</p>
<p>Il framework ha un funzionamento molto semplice: All’avvio dell’applicazione (Application_Start nel Global.asax), si caricano tutti gli assebly contenente estensioni e si caricano anche queste. Dopo di ché le si inizializzano ed il gioco è fatto.</p>
<p>Il framework richiede che ogni estensione implementi un’interfaccia: IExtension. L’interfaccia IExtension presenta un unico metodo: Initialize; Quest’ultimo viene chiamato dall’applicazione (di solito) nell’evento Application_Start e il suo unico scopo è quello di inizializzare le proprietà e oggetti correlati.</p>
<p>Il punto di incontro tra un’applicazione ed il framework sono gli eventi: Nell’evento Initialize ogni estensione aggiungerà un handler ad un evento statico di un oggetto che interessa all’estensione. Questo permette di creare un’interfaccia globale delle estensioni che poi provvederanno da sole al proprio ciclo di vita. Ad esempio supponiamo di avere un Blog con un oggetto Post e che quest’ultimo abbia un evento Show statico. Ogni volta che il Blog mostrerà il post, scatenerà l’evento Show, che, richiamerà a sua volta ogni handler associato e quindi anche le estensioni che vogliono modificare il comportamento per la visualizzazione dei post. Semplice no?</p>
<p>Il primo passo è la definizione del Framework, quindi dell’interfaccia IExtension e di una classe che gestisca le estensioni caricate. Creiamo quindi un nuovo progetto web di nome MyApplication, e aggiungiamo un’altro progetto C# Library che chiamiamo MyApplication.Extensibility.</p>
<p>Poi aggiungiamo un un’interfaccia, IExtension.cs nel profetto del framework (MyApplication.Extensibility) e scriviamo il seguente codice:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">namespace</span> MyApplication.Extensibility</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span>     <span style="color:blue;">public</span> <span style="color:blue;">interface</span> <span style="color:#2b91af;">IExtension</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span>         <span style="color:blue;">void</span> Initialize();</p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span> }</p>
</div>
<p>L’interfaccia è molto semplice: espone solo il metodo Initialize. Ora passiamo alla creazione di una classe (ExtensionsManager) che gestirà tutte le estensioni caricate:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">namespace</span> MyApplication.Extensibility</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span>     <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ExtensionManager</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>         <span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">ExtensionManager</span> m_Instance;</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span>         <span style="color:blue;">private</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">IExtension</span>&gt; m_Extensions = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">IExtension</span>&gt;();</p>
<p style="margin:0;"><span style="color:#2b91af;">    8</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    9</span>         <span style="color:blue;">public</span> ExtensionManager() { }</p>
<p style="margin:0;"><span style="color:#2b91af;">   10</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   11</span>         <span style="color:blue;">public</span> <span style="color:blue;">void</span> CollectExtensions(<span style="color:blue;">string</span> configurationSection)</p>
<p style="margin:0;"><span style="color:#2b91af;">   12</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   13</span>             <span style="color:#2b91af;">ExtensionsConfigurationSection</span> section = (<span style="color:#2b91af;">ExtensionsConfigurationSection</span>)<span style="color:#2b91af;">WebConfigurationManager</span>.GetSection(configurationSection);</p>
<p style="margin:0;"><span style="color:#2b91af;">   14</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   15</span>             <span style="color:blue;">if</span> (!section.Enabled)</p>
<p style="margin:0;"><span style="color:#2b91af;">   16</span>                 <span style="color:blue;">return</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;">   17</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   18</span>             <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">ExtensionElement</span> extension <span style="color:blue;">in</span> section.Extensions)</p>
<p style="margin:0;"><span style="color:#2b91af;">   19</span>             {</p>
<p style="margin:0;"><span style="color:#2b91af;">   20</span>                 <span style="color:blue;">if</span> (extension.Enabled)</p>
<p style="margin:0;"><span style="color:#2b91af;">   21</span>                     m_Extensions.Add((<span style="color:#2b91af;">IExtension</span>)<span style="color:#2b91af;">Activator</span>.CreateInstance(<span style="color:#2b91af;">Type</span>.GetType(extension.TypeName)));</p>
<p style="margin:0;"><span style="color:#2b91af;">   22</span>             }</p>
<p style="margin:0;"><span style="color:#2b91af;">   23</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   24</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   25</span>         <span style="color:blue;">public</span> <span style="color:blue;">void</span> InitializeExtensions()</p>
<p style="margin:0;"><span style="color:#2b91af;">   26</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   27</span>             <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">IExtension</span> extension <span style="color:blue;">in</span> m_Extensions)</p>
<p style="margin:0;"><span style="color:#2b91af;">   28</span>             {</p>
<p style="margin:0;"><span style="color:#2b91af;">   29</span>                 extension.Initialize();</p>
<p style="margin:0;"><span style="color:#2b91af;">   30</span>             }</p>
<p style="margin:0;"><span style="color:#2b91af;">   31</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   32</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   33</span>         <span style="color:blue;">public</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">IExtension</span>&gt; Extensions</p>
<p style="margin:0;"><span style="color:#2b91af;">   34</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   35</span>             <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_Extensions; }</p>
<p style="margin:0;"><span style="color:#2b91af;">   36</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   37</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   38</span>         <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:#2b91af;">ExtensionManager</span> Instance</p>
<p style="margin:0;"><span style="color:#2b91af;">   39</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   40</span>             <span style="color:blue;">get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">   41</span>             {</p>
<p style="margin:0;"><span style="color:#2b91af;">   42</span>                 <span style="color:blue;">if</span> (m_Instance == <span style="color:blue;">null</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">   43</span>                     m_Instance = <span style="color:blue;">new</span> <span style="color:#2b91af;">ExtensionManager</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;">   44</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   45</span>                 <span style="color:blue;">return</span> m_Instance;</p>
<p style="margin:0;"><span style="color:#2b91af;">   46</span>             }</p>
<p style="margin:0;"><span style="color:#2b91af;">   47</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   48</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   49</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   50</span> }</p>
</div>
<p>La prima cosa da notare dopo la lettura del codice è la presenza di un field di tipo ExtensionsManager, infatti questo field permette di rendere una classe Singleton, cioé una sola istanza della classe per tutto il ciclo dei vita dell’applicazione. La seconda cosa particolare è il riferimento alla classe ExtensionsConfigurationSection. Quest’ultima non è altro che una classe che definisce una sezione personalizzata all’interno di web.config. Dato che parlare dei files di configurazione è fuori dal tema del post, rimando ad un link dell’msdn: <a href="http://msdn.microsoft.com/en-us/library/2tw134k3.aspx" target="_blank">ConfigurationSection</a>. Per il resto è molto semplice: Si utilizza la proprietà statica Instance per accedere all’istanza della classe e si richiama il metodo CollectExtensions che non fa altro che leggere tutte le estensioni definite del file web.config e aggiungerle alla collezione interna di estensioni (Extensions). Dopo aver caricato le estensioni, le si inizializzano utilizzando il metodo InitializeExtensions. Il tutto verrà eseguito nel file Global.asax.</p>
<p>Nel file di codice è presente la classe ExtensionsConfigurationSection.</p>
<p>Ora che si è creato il framework per l’estensibilità si può passare al ritrovamento e caricamento delle estensioni definite: andiamo nel file Global.asax del progetto Web e scriviamo quanto segue</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">void</span> Application_Start(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span>             <span style="color:#2b91af;">ExtensionManager</span> manager = <span style="color:#2b91af;">ExtensionManager</span>.Instance;</p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>             manager.CollectExtensions(<span style="color:#a31515;">&#8220;MyApplication.Extensibility&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span>             manager.InitializeExtensions();</p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>         }</p>
</div>
<p>Le estensioni vengono caricate all’avvio del programma, così si evita di caricarle ogni volta che se ne ha bisogno. Non dovrebbero creare troppo overhead, anzi per niente se si utilizza il metodo descritto in questo post. Il metodo CollectExtensions accetta un parametro che indica il nome della sezione del file web.config.</p>
<p>Ora che il framework è pronto, le estensioni vengono caricate, mancano solo due cose: le estensioni e la parte ‘Core’ del programma, cioé tutte le classi che il nostro programma vuole esporre alle estensioni.</p>
<p>Il Core dell’applicazione d’esempio che ho creato è molto semplice: essendo il mio applicativo un editor di codici BBCode (quella sintassi usata spesso nei forum per sostituire l’HTML, ad esempio il tag [B] sostituisce il tag &lt;strong&gt; dell’HTML. BBCode è stato creato semplicemente per non permettere all’utente di scrivere tag HTML, evitando code injections ecc..) ho definito una classe Document molto semplice ed una classe DocumentParser un po piu’ interessante:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">public</span> <span style="color:blue;">delegate</span> <span style="color:blue;">void</span> <span style="color:#2b91af;">DocumentEventHandler</span>(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">DocumentEventArgs</span> e);</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">DocumentEventArgs</span> : <span style="color:#2b91af;">EventArgs</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>     <span style="color:blue;">public</span> DocumentEventArgs(<span style="color:blue;">string</span> documentText)</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">    8</span>         <span style="color:blue;">this</span>.DocumentText = documentText;</p>
<p style="margin:0;"><span style="color:#2b91af;">    9</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   10</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   11</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> DocumentText { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">   12</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   13</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;">   14</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   15</span> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Document</span></p>
<p style="margin:0;"><span style="color:#2b91af;">   16</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">   17</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   18</span>     <span style="color:blue;">public</span> Document(<span style="color:blue;">string</span> name, <span style="color:blue;">string</span> author, <span style="color:blue;">string</span> text)</p>
<p style="margin:0;"><span style="color:#2b91af;">   19</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">   20</span>         Name = name;</p>
<p style="margin:0;"><span style="color:#2b91af;">   21</span>         Author = author;</p>
<p style="margin:0;"><span style="color:#2b91af;">   22</span>         Text = text;</p>
<p style="margin:0;"><span style="color:#2b91af;">   23</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   24</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   25</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> Name { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">   26</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   27</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> Text { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">   28</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   29</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> Author { <span style="color:blue;">get</span>; <span style="color:blue;">set</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">   30</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   31</span>     <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> OnServing(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">DocumentEventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">   32</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">   33</span>         <span style="color:blue;">if</span> (Serving != <span style="color:blue;">null</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">   34</span>             Serving(sender, e);</p>
<p style="margin:0;"><span style="color:#2b91af;">   35</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   36</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   37</span>     <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> OnParsing(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">DocumentEventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">   38</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">   39</span>         <span style="color:blue;">if</span> (Parsing != <span style="color:blue;">null</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">   40</span>             Parsing(sender, e);</p>
<p style="margin:0;"><span style="color:#2b91af;">   41</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   42</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   43</span>     <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">DocumentEventHandler</span> Serving;</p>
<p style="margin:0;"><span style="color:#2b91af;">   44</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   45</span>     <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">DocumentEventHandler</span> Parsing;</p>
<p style="margin:0;"><span style="color:#2b91af;">   46</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   47</span> }</p>
</div>
<p>classe DocumentParser:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">DocumentParser</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>     <span style="color:blue;">private</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt; BasicTags;</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span>     <span style="color:blue;">public</span> DocumentParser()</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">    8</span>         BasicTags = <span style="color:blue;">new</span> <span style="color:#2b91af;">Dictionary</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt;()</p>
<p style="margin:0;"><span style="color:#2b91af;">    9</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   10</span>             { <span style="color:#a31515;">&#8220;[B]&#8220;</span>, <span style="color:#a31515;">&#8220;&lt;strong&gt;&#8221;</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;">   11</span>             { <span style="color:#a31515;">&#8220;[/B]&#8220;</span>, <span style="color:#a31515;">&#8220;&lt;/strong&gt;&#8221;</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;">   12</span>             { <span style="color:#a31515;">&#8220;[I]&#8220;</span>, <span style="color:#a31515;">&#8220;&lt;i&gt;&#8221;</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;">   13</span>             { <span style="color:#a31515;">&#8220;[/I]&#8220;</span>, <span style="color:#a31515;">&#8220;&lt;/i&gt;&#8221;</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;">   14</span>             { <span style="color:#a31515;">&#8220;[A]&#8220;</span>, <span style="color:#a31515;">&#8220;&lt;a href=\&#8221;"</span> },</p>
<p style="margin:0;"><span style="color:#2b91af;">   15</span>             { <span style="color:#a31515;">&#8220;[/A]&#8220;</span>, <span style="color:#a31515;">&#8220;\&#8221; &gt;&#8221;</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;">   16</span>         };</p>
<p style="margin:0;"><span style="color:#2b91af;">   17</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   18</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   19</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> Parse(<span style="color:#2b91af;">Document</span> document, <span style="color:blue;">bool</span> raiseEvents)</p>
<p style="margin:0;"><span style="color:#2b91af;">   20</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">   21</span>         <span style="color:blue;">string</span> parsed = document.Text;</p>
<p style="margin:0;"><span style="color:#2b91af;">   22</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   23</span>         <span style="color:blue;">foreach</span> (<span style="color:#2b91af;">KeyValuePair</span>&lt;<span style="color:blue;">string</span>, <span style="color:blue;">string</span>&gt; tag <span style="color:blue;">in</span> BasicTags)</p>
<p style="margin:0;"><span style="color:#2b91af;">   24</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   25</span>             parsed = parsed.Replace(tag.Key, tag.Value);</p>
<p style="margin:0;"><span style="color:#2b91af;">   26</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   27</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   28</span>         <span style="color:blue;">if</span> (raiseEvents)</p>
<p style="margin:0;"><span style="color:#2b91af;">   29</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   30</span>             <span style="color:#2b91af;">DocumentEventArgs</span> args = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentEventArgs</span>(parsed);</p>
<p style="margin:0;"><span style="color:#2b91af;">   31</span>             <span style="color:#2b91af;">Document</span>.OnParsing(<span style="color:blue;">this</span>, args);</p>
<p style="margin:0;"><span style="color:#2b91af;">   32</span>             parsed = args.DocumentText;</p>
<p style="margin:0;"><span style="color:#2b91af;">   33</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   34</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   35</span>         <span style="color:blue;">return</span> parsed;</p>
<p style="margin:0;"><span style="color:#2b91af;">   36</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   37</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   38</span>     <span style="color:blue;">public</span> <span style="color:blue;">string</span> Show(<span style="color:#2b91af;">Document</span> document, <span style="color:blue;">bool</span> raiseEvents)</p>
<p style="margin:0;"><span style="color:#2b91af;">   39</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">   40</span>         <span style="color:blue;">string</span> newText = document.Text;</p>
<p style="margin:0;"><span style="color:#2b91af;">   41</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   42</span>         newText = newText.Insert(0, <span style="color:#a31515;">&#8220;&lt;pre&gt;&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;">   43</span>         newText += <span style="color:#a31515;">&#8220;&lt;/pre&gt;&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;">   44</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   45</span>         <span style="color:blue;">if</span> (raiseEvents)</p>
<p style="margin:0;"><span style="color:#2b91af;">   46</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   47</span>             <span style="color:#2b91af;">DocumentEventArgs</span> args = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentEventArgs</span>(newText);</p>
<p style="margin:0;"><span style="color:#2b91af;">   48</span>             <span style="color:#2b91af;">Document</span>.OnServing(<span style="color:blue;">this</span>, args);</p>
<p style="margin:0;"><span style="color:#2b91af;">   49</span>             newText = args.DocumentText;</p>
<p style="margin:0;"><span style="color:#2b91af;">   50</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   51</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   52</span>         <span style="color:blue;">return</span> newText;</p>
<p style="margin:0;"><span style="color:#2b91af;">   53</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   54</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   55</span> }</p>
</div>
<p>La classe Document è molto semplice: espone le proprietà Name (Nome), Text (Corpo), Author (Autore) e due eventi, ai quali le estensioni andranno ad aggiungere un handler nel caso il documento venga mostrato (Serving) o durante la fase di parsing del BBCode (Parsing).</p>
<p>La classe DocumentParser non è altro che una classe che sostituisce i tag BB con quelli HTML nel metodo Parse ed aggiunge il testo tra due tag ‘pre’ nel metodo Show. Entrambi i metodi accettano un parametro boolean chiamato <em>raiseEvents</em> che permette di ignorare l’azione delle estensioni, in quanto gli eventi ai quali sono registrati, non vengono scatenati.</p>
<p>E’ meglio provare subito il codice. Quindi si crea una semplice pagina con una TextBox che contiene il testo, un pulsante ‘Transform’ che effettua il parsing e mostra il risultato in una Label. Inoltre è presente anche una CheckBox che indica se gli eventi devono essere scatenati o meno.</p>
<p><a href="http://umarja.files.wordpress.com/2009/04/pluginframeworkexamplepage.png"><img style="display:inline;border-width:0;" title="PluginFramework - Example Page" src="http://umarja.files.wordpress.com/2009/04/pluginframeworkexamplepage-thumb.png?w=655&#038;h=301" border="0" alt="PluginFramework - Example Page" width="655" height="301" /></a></p>
<p>Come si può notare, la pagina è molto semplice. Passiamo al codice:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> Page_Load(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span> }</p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> <span style="color:blue;">protected</span> <span style="color:blue;">void</span> ButtonTransform_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span>     <span style="color:#2b91af;">Document</span> document = <span style="color:blue;">new</span> <span style="color:#2b91af;">Document</span>(<span style="color:#a31515;">&#8220;Semplice Documento&#8221;</span>, <span style="color:#a31515;">&#8220;Umar Jamil&#8221;</span>, TextBoxDocument.Text);</p>
<p style="margin:0;"><span style="color:#2b91af;">    8</span>     <span style="color:#2b91af;">DocumentParser</span> parser = <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentParser</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;">    9</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   10</span>     <span style="color:blue;">string</span> parsed = parser.Parse(document, CheckBoxRaiseEvents.Checked);</p>
<p style="margin:0;"><span style="color:#2b91af;">   11</span>     document.Text = parsed;</p>
<p style="margin:0;"><span style="color:#2b91af;">   12</span>     LabelTransformed.Text = parser.Show(document, CheckBoxRaiseEvents.Checked);</p>
<p style="margin:0;"><span style="color:#2b91af;">   13</span> }</p>
</div>
<p>Si crea un documento, si specifica il nome, l’autore ed il contenuto. Poi si crea un’istanza della classe DocumentParser, si effettua il parsing e si mostra il risultato nella label LabelTransformed. Da notare l’uso del metodo Show della classe DocumentParser per mostrare il documento.</p>
<p>Proviamo a lanciare il programma scrivendo un testo che contiene tag del tipo [B], [I]  e notiamo l’effetto: l’output verrà formattato usando tag HTML.</p>
<p><a href="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage.png"><img style="display:inline;border-width:0;" title="PluginFramework - Running Example Page" src="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage-thumb.png?w=491&#038;h=309" border="0" alt="PluginFramework - Running Example Page" width="491" height="309" /></a></p>
<p>Ma l’obiettivo dell’articolo non è mica creare un editor di testo, ma è quello di creare un editor di testo estendibile. Quindi cominciamo la fase di scrittura dell’estensione: creiamo un nuovo progetto, di tipo class library e chiamiamolo ParsingExtensions, dopodiché aggiungiamo una classe DateExtension con questo codice:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">namespace</span> ParsingExtensions</p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span>     <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">DateExtension</span> : <span style="color:#2b91af;">IExtension</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span>     {</p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    6</span> <span style="color:blue;">        #region</span> IExtension Members</p>
<p style="margin:0;"><span style="color:#2b91af;">    7</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">    8</span>         <span style="color:blue;">public</span> <span style="color:blue;">void</span> Initialize()</p>
<p style="margin:0;"><span style="color:#2b91af;">    9</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   10</span>             <span style="color:#2b91af;">Document</span>.Parsing += <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentEventHandler</span>(Document_Parsing);</p>
<p style="margin:0;"><span style="color:#2b91af;">   11</span>             <span style="color:#2b91af;">Document</span>.Serving += <span style="color:blue;">new</span> <span style="color:#2b91af;">DocumentEventHandler</span>(Document_Serving);</p>
<p style="margin:0;"><span style="color:#2b91af;">   12</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   13</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   14</span>         <span style="color:blue;">void</span> Document_Serving(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">DocumentEventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">   15</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   16</span>             e.DocumentText += <span style="color:#a31515;">&#8220;&lt;pre&gt;Modified by Date Extension 1.0&lt;/pre&gt;&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;">   17</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   18</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   19</span>         <span style="color:blue;">void</span> Document_Parsing(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">DocumentEventArgs</span> e)</p>
<p style="margin:0;"><span style="color:#2b91af;">   20</span>         {</p>
<p style="margin:0;"><span style="color:#2b91af;">   21</span>             e.DocumentText = e.DocumentText.Replace(<span style="color:#a31515;">&#8220;[DATE]&#8220;</span>, <span style="color:#2b91af;">DateTime</span>.Now.ToShortDateString());</p>
<p style="margin:0;"><span style="color:#2b91af;">   22</span>         }</p>
<p style="margin:0;"><span style="color:#2b91af;">   23</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   24</span> <span style="color:blue;">        #endregion</span></p>
<p style="margin:0;"><span style="color:#2b91af;">   25</span> </p>
<p style="margin:0;"><span style="color:#2b91af;">   26</span>     }</p>
<p style="margin:0;"><span style="color:#2b91af;">   27</span> }</p>
</div>
<p>Ovviamente bisogna aggiungere i vari riferimenti: Business Layer, Data Layer e ovviamente al framework… cioé tutti quelli di cui l’estensione ha bisogno per funzionare.</p>
<p>Analizziamo l’estensione: non fa altro che aggiungere una data ogni volta che incontra il tag [DATE] e ogni volta che il documento è mostrato, aggiunge del testo finale indicando che il testo è stato modificato dall’estensione. Notiamo che nel metodo Initialize l’estensione aggiunge due handler ed il gioco è fatto. Ogni volta che un documento verrà mostrato o verrà effettuato il parsing, la nostra estensione entrerà in azione.</p>
<p>Non è ancora finita, in quanto l’estensione è pronta, ma l’applicazione web non la conosce. Quindi andiamo a modificare la sezione del web.config che contiene le estensioni:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">    1</span> <span style="color:blue;">&lt;</span><span style="color:#a31515;">MyApplication.Extensibility</span><span style="color:blue;"> </span><span style="color:red;">enabled</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    2</span> <span style="color:blue;">  &lt;</span><span style="color:#a31515;">extensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    3</span> <span style="color:blue;">    &lt;</span><span style="color:#a31515;">add</span><span style="color:blue;"> </span><span style="color:red;">enabled</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">true</span>&#8220;<span style="color:blue;"> </span><span style="color:red;">typeName</span><span style="color:blue;">=</span>&#8220;<span style="color:blue;">ParsingExtensions.DateExtension, ParsingExtensions</span>&#8220;<span style="color:blue;">/&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    4</span> <span style="color:blue;">  &lt;/</span><span style="color:#a31515;">extensions</span><span style="color:blue;">&gt;</span></p>
<p style="margin:0;"><span style="color:#2b91af;">    5</span> <span style="color:blue;">&lt;/</span><span style="color:#a31515;">MyApplication.Extensibility</span><span style="color:blue;">&gt;</span></p>
</div>
<p>Ora basta aggiungere l’assembly che contiene l’estensione nella cartella Bin dell’applicazione web ed il gioco è fatto. Vediamo il risultato:</p>
<p><a href="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage2.png"><img style="display:inline;border-width:0;" title="PluginFramework - Running Example Page 2" src="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage2-thumb.png?w=480&#038;h=367" border="0" alt="PluginFramework - Running Example Page 2" width="480" height="367" /></a></p>
<p>Evviva! finalmente, dopo ore di lavoro a sbattere la testa, il framework funziona. Per disabilitarlo basterà impostare l’attributo ‘enabled’ a ‘false’ per la sezione delle estensioni o su ciascuna estensione da disabilitare.</p>
<p>Vorrei concludere dicendo che il mio è uno dei tanti metodi che è possibile utilizzare per creare estensioni, magari il mio non è neanche molto bello, però funziona. Ogni persona è libera di proporre, creare e condividere le propie invenzioni.</p>
<p>Ovviamente è possibile riutilizzare il mio framework senza alcuna modifica. Mi raccomando, se lo utilizzate, specificate l’autore ed il link al blog.</p>
<p>Ecco infine il codice che ho scritto: <a href="http://cid-e964b53d74662059.skydrive.live.com/self.aspx/Projects/MyApplication.zip" target="_blank">Download</a>.</p>
<p>Ringrazio per la lettura, Umar J.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/umarja.wordpress.com/43/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/umarja.wordpress.com/43/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/umarja.wordpress.com/43/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=43&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umarja.wordpress.com/2009/04/27/estendere-unapplicazione-aspnet-con-pluginestensioni/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91822b4ed2c380d676d941584b0f8f49?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umarja</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/pluginframeworkexamplepage-thumb.png" medium="image">
			<media:title type="html">PluginFramework - Example Page</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage-thumb.png" medium="image">
			<media:title type="html">PluginFramework - Running Example Page</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/pluginframeworkrunningexamplepage2-thumb.png" medium="image">
			<media:title type="html">PluginFramework - Running Example Page 2</media:title>
		</media:content>
	</item>
		<item>
		<title>controllare il task scheduler di Windows in .NET</title>
		<link>http://umarja.wordpress.com/2009/04/08/controllare-il-task-scheduler-di-windows-in-net/</link>
		<comments>http://umarja.wordpress.com/2009/04/08/controllare-il-task-scheduler-di-windows-in-net/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 19:29:00 +0000</pubDate>
		<dc:creator>umarja</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[Interop]]></category>
		<category><![CDATA[Task Scheduler]]></category>

		<guid isPermaLink="false">http://umarja.wordpress.com/2009/04/08/controllare-il-task-scheduler-di-windows-in-net/</guid>
		<description><![CDATA[Il Task Scheduler (o Utilità di pianificazione, per coloro che hanno Windows in italiano), è uno strumento molto versatile ed integrato in Windows, che permette di programmare delle operazioni da eseguire in determinati momenti della giornata, della settimana, del mese o anno, a determinate condizioni e permette di associare delle azioni nel caso venga verificato [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=32&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Il Task Scheduler (o Utilità di pianificazione, per coloro che hanno Windows in italiano), è uno strumento molto versatile ed integrato in Windows, che permette di programmare delle operazioni da eseguire in determinati momenti della giornata, della settimana, del mese o anno, a determinate condizioni e permette di associare delle azioni nel caso venga verificato l’evento.</p>
<p>In questo articolo descrivo brevemente come utilizzare le API esposte dal Task Scheduler in C#.</p>
<p><a href="http://umarja.files.wordpress.com/2009/04/taskschedulermainpicture.png"><img style="display:inline;border-width:0;" title="TaskScheduler - Finestra principale" src="http://umarja.files.wordpress.com/2009/04/taskschedulermainpicture-thumb.png?w=644&#038;h=390" border="0" alt="TaskScheduler - Finestra principale" width="644" height="390" /></a></p>
<p><span id="more-32"></span></p>
<p>Bisogna partire creando un progetto Console o Windows Forms.</p>
<p>Ora bisogna aggiungere un riferimento alla libreria che contiene il set di API: <strong>taskschd.dll. </strong>Il file si trova in %WINDIR%\System32\.</p>
<p><img class="alignnone size-full wp-image-35" title="taskscheduler-addreference1" src="http://umarja.files.wordpress.com/2009/04/taskscheduler-addreference1.png?w=655" alt="taskscheduler-addreference1"   /></p>
<p>Una volta aggiunto il riferimento si può notare che Visual Studio genera automaticamente un wrapper per la libreria aggiunta, dovrebbe chiamarsi TaskScheduler.</p>
<p>Ora finalmente si può passare al codice.</p>
<p>La prima cosa da sapere è che il Task Scheduler può essere anche amministrato da remoto, quindi bisogna prima connettersi ad esso passando a tutti i parametri il valore null, in quanto l’obiettivo è connettersi a quello locale:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">TaskSchedulerClass</span> scheduler = <span style="color:blue;">new</span> <span style="color:#2b91af;">TaskSchedulerClass</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> scheduler.Connect(<span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>);</p>
</div>
<p>Ora si possiede un’istanza della classe TaskSchedulerClass, questa classe permette di creare Task, gestire le cartelle ecc… La classe TaskSchedulerClass dovrebbe trovarsi nel namespace TaskScheduler (che è anche il nome del wrapper creato da Visual Studio).</p>
<p>Il prossimo passo è creare il Task vero e proprio, quindi si richiama il metodo NewTask della classe TaskSchedulerClass e si assegnano le varie proprietà:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">ITaskDefinition</span> task = scheduler.NewTask(0);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> task.RegistrationInfo.Author = <span style="color:#a31515;">&#8220;Umar Jamil&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> task.RegistrationInfo.Date = <span style="color:#2b91af;">DateTime</span>.Now.ToString(<span style="color:#a31515;">&#8220;yyyy-MM-ddThh:mm:ss&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> task.RegistrationInfo.Description = <span style="color:#a31515;">&#8220;Visualizza un messaggio di promemoria&#8221;</span>;</p>
</div>
<p>La prima cosa da notare è il formato della data che la libreria accetta: ANNO-MESE-GIORNO + T + ORA:MINUTI:SECONDI. Io ho impostato veramente poche proprietà. La classe task espone una proprietà Settings che permette di specificare a quali condizioni di batteria, di connessione internet ecc.. il task deve essere avviato. Provate voi a smanettare.</p>
<p>Ora che si ha un oggetto ITaskDefinition, bisogna specificare il trigger, cioé l’azione che scatenerà il task, che può essere un range temporale, un task che viene ripetuto ogni giorno alla stessa ora ed altro. Per fare ciò, bisogna creare il trigger ed assegnarlo ad un’istanza dell’oggetto che lo rappresenta:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">IDailyTrigger</span> dailyTrigger = (<span style="color:#2b91af;">IDailyTrigger</span>)task.Triggers.Create(<span style="color:#2b91af;">_TASK_TRIGGER_TYPE2</span>.TASK_TRIGGER_DAILY);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> dailyTrigger.DaysInterval = 1;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> dailyTrigger.StartBoundary = <span style="color:#2b91af;">DateTime</span>.Now.ToString(<span style="color:#a31515;">&#8220;yyyy-MM-ddThh:mm:ss&#8221;</span>);</p>
</div>
<p>Bisogna inanzitutto specificare che tipo di trigger si vuole, e quindi effettuare il casting al tipo che lo rappresenta. Nel caso di IDailyTrigger, bisogna specificare a partire da quale data esso è attivo e ogni quanti gioni deve essere avviato, in questo caso ogni giorno (se fosse stato DaysInterval = 2, il task sarebbe stato avviato un giorno si ed un giorno no).</p>
<p>Dopo aver specificato i vari triggers, si può passare a cosa deve effettuare il task se avviato. Esistono tre tipi di azioni:</p>
<ul>
<li>Avvio di un programma</li>
<li>Invio di una email</li>
<li>Visualizzazione di un messaggio (la classica MessageBox).</li>
</ul>
<p>Ecco mostrati i tre casi:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">IExecAction</span> execAction = (<span style="color:#2b91af;">IExecAction</span>)task.Actions.Create(<span style="color:#2b91af;">_TASK_ACTION_TYPE</span>.TASK_ACTION_EXEC);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> execAction.Arguments = <span style="color:#a31515;">&#8220;newfile.txt&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> execAction.Path = <span style="color:#a31515;">&#8220;notepad.exe&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> execAction.WorkingDirectory = <span style="color:#a31515;">@&#8221;C:\Windows\System32&#8243;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> execAction.Id = <span style="color:#a31515;">&#8220;Avvia internet explorer&#8221;</span>;</p>
</div>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">IEmailAction</span> emailAction = (<span style="color:#2b91af;">IEmailAction</span>)task.Actions.Create(<span style="color:#2b91af;">_TASK_ACTION_TYPE</span>.TASK_ACTION_SEND_EMAIL);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> emailAction.Body = <span style="color:#a31515;">&#8220;qui va il testo del messaggio&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> emailAction.From = <span style="color:#a31515;">&#8220;XXXXXXXXXX&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> emailAction.To = <span style="color:#a31515;">&#8220;XXXXXXXXXX&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> emailAction.Subject = <span style="color:#a31515;">&#8220;Oggetto della email&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span> emailAction.Id = <span style="color:#a31515;">&#8220;Invia una email&#8221;</span>;</p>
</div>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">IShowMessageAction</span> action = (<span style="color:#2b91af;">IShowMessageAction</span>)task.Actions.Create(<span style="color:#2b91af;">_TASK_ACTION_TYPE</span>.TASK_ACTION_SHOW_MESSAGE);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> action.MessageBody = <span style="color:#a31515;">&#8220;Dai un&#8217;occhiata ai commenti sul blog e rispondi!&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> action.Title = <span style="color:#a31515;">&#8220;Urgente!&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> action.Id = <span style="color:#a31515;">&#8220;Mostra il messaggio&#8221;</span>;</p>
</div>
<p>L’ultimo passo da effettuare è la registrazione del task in una cartella. I task sono organizzati in cartelle, ad esempio esiste la cartella Microsoft. Io utilizzerò quella di root, così che il task appaia subito all’apertura dell’utility Task Scheduler. Il processo di registrazione avviene prima di tutto ottenendo un’istanza della classe ITaskFolder e poi registrando il task dentro quest’ultima.</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">ITaskFolder</span> mainFolder = scheduler.GetFolder(<span style="color:#a31515;">@&#8221;\&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span> <span style="color:#2b91af;">IRegisteredTask</span> registeredTask = mainFolder.RegisterTask(</p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> <span style="color:#a31515;">&#8220;TaskSchedulerDemo&#8221;</span>, task.XmlText,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span> (<span style="color:blue;">int</span>)<span style="color:#2b91af;">_TASK_CREATION</span>.TASK_CREATE_OR_UPDATE,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:#2b91af;">_TASK_LOGON_TYPE</span>.TASK_LOGON_INTERACTIVE_TOKEN, <span style="color:blue;">null</span>);</p>
</div>
<p>Il primo parametro del metodo RegisterTask è il nome che si vuole assegnare a quest’ultimo, il secondo è la definizione del task, il terzo indica il tipo di azione desiderata: Aggiunta, Sostituzione, Validare, Disabilitare. In questo caso si vuole solo aggiungere il task, e se esiste già, aggiornarlo.</p>
<p>Ora il task è pronto! Lo si può avviare da codice utilizzando l’istanza della classe IRegisteredTask e richiamando il metodo Run, o aspettare che Windows lo esegua quando previsto.</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;"> 1</span> <span style="color:#2b91af;">TaskSchedulerClass</span> scheduler = <span style="color:blue;">new</span> <span style="color:#2b91af;">TaskSchedulerClass</span>();</p>
<p style="margin:0;"><span style="color:#2b91af;"> 2</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 3</span> scheduler.Connect(<span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:blue;">null</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 4</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 5</span> <span style="color:#2b91af;">ITaskDefinition</span> task = scheduler.NewTask(0);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 6</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 7</span> task.RegistrationInfo.Author = <span style="color:#a31515;">&#8220;Umar Jamil&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 8</span> task.RegistrationInfo.Date = <span style="color:#2b91af;">DateTime</span>.Now.ToString(<span style="color:#a31515;">&#8220;yyyy-MM-ddThh:mm:ss&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 9</span> task.RegistrationInfo.Description = <span style="color:#a31515;">&#8220;Visualizza un messaggio di promemoria&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 10</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 11</span> <span style="color:green;">// Triggers</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 12</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 13</span> <span style="color:#2b91af;">IDailyTrigger</span> dailyTrigger = (<span style="color:#2b91af;">IDailyTrigger</span>)task.Triggers.Create(<span style="color:#2b91af;">_TASK_TRIGGER_TYPE2</span>.TASK_TRIGGER_DAILY);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 14</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 15</span> dailyTrigger.DaysInterval = 1;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 16</span> dailyTrigger.StartBoundary = <span style="color:#2b91af;">DateTime</span>.Now.ToString(<span style="color:#a31515;">&#8220;yyyy-MM-ddThh:mm:ss&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 17</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 18</span> <span style="color:#2b91af;">IShowMessageAction</span> action = (<span style="color:#2b91af;">IShowMessageAction</span>)task.Actions.Create(<span style="color:#2b91af;">_TASK_ACTION_TYPE</span>.TASK_ACTION_SHOW_MESSAGE);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 19</span> action.MessageBody = <span style="color:#a31515;">&#8220;Dai un&#8217;occhiata ai commenti sul blog e rispondi!&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 20</span> action.Title = <span style="color:#a31515;">&#8220;Urgente!&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 21</span> action.Id = <span style="color:#a31515;">&#8220;Mostra il messaggio&#8221;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;"> 22</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 23</span> <span style="color:green;">//IEmailAction emailAction = (IEmailAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_SEND_EMAIL);</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 24</span> <span style="color:green;">//emailAction.Body = &#8220;qui va il testo del messaggio&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 25</span> <span style="color:green;">//emailAction.From = &#8220;XXXXXXXXXX&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 26</span> <span style="color:green;">//emailAction.To = &#8220;XXXXXXXXXX&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 27</span> <span style="color:green;">//emailAction.Subject = &#8220;Oggetto della email&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 28</span> <span style="color:green;">//emailAction.Id = &#8220;Invia una email&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 29</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 30</span> <span style="color:green;">//IExecAction execAction = (IExecAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 31</span> <span style="color:green;">//execAction.Arguments = &#8220;www.contoso.com&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 32</span> <span style="color:green;">//execAction.Path = &#8220;notepad.exe&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 33</span> <span style="color:green;">//execAction.WorkingDirectory = @&#8221;C:\Windows\System32&#8243;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 34</span> <span style="color:green;">//execAction.Id = &#8220;Avvia internet explorer&#8221;;</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 35</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 36</span> <span style="color:#2b91af;">ITaskFolder</span> mainFolder = scheduler.GetFolder(<span style="color:#a31515;">@&#8221;\&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 37</span> <span style="color:#2b91af;">IRegisteredTask</span> registeredTask = mainFolder.RegisterTask(</p>
<p style="margin:0;"><span style="color:#2b91af;"> 38</span> <span style="color:#a31515;">&#8220;TaskSchedulerDemo&#8221;</span>, task.XmlText,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 39</span> (<span style="color:blue;">int</span>)<span style="color:#2b91af;">_TASK_CREATION</span>.TASK_CREATE_OR_UPDATE,</p>
<p style="margin:0;"><span style="color:#2b91af;"> 40</span> <span style="color:blue;">null</span>, <span style="color:blue;">null</span>, <span style="color:#2b91af;">_TASK_LOGON_TYPE</span>.TASK_LOGON_INTERACTIVE_TOKEN, <span style="color:blue;">null</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 41</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> 42</span> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&#8220;Done!&#8221;</span>);</p>
<p style="margin:0;"><span style="color:#2b91af;"> 43</span> <span style="color:#2b91af;">Console</span>.ReadLine();</p>
</div>
<p>Ecco qui il risultato finale nella finestra del Task Scheduler o Utilità di pianificazione:</p>
<p><a href="http://umarja.files.wordpress.com/2009/04/taskschedulerfinalresult.png"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="TaskScheduler - Final Result" src="http://umarja.files.wordpress.com/2009/04/taskschedulerfinalresult-thumb.png?w=644&#038;h=440" border="0" alt="TaskScheduler - Final Result" width="644" height="440" /></a></p>
<p>E’ tutto, per i geeks che non si accontentano, rimando alla <a href="http://msdn.microsoft.com/en-us/library/aa383614(VS.85).aspx" target="_blank">documentazione</a> su MSDN.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/umarja.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/umarja.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/umarja.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=32&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umarja.wordpress.com/2009/04/08/controllare-il-task-scheduler-di-windows-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91822b4ed2c380d676d941584b0f8f49?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umarja</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/taskschedulermainpicture-thumb.png" medium="image">
			<media:title type="html">TaskScheduler - Finestra principale</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/taskscheduler-addreference1.png" medium="image">
			<media:title type="html">taskscheduler-addreference1</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/04/taskschedulerfinalresult-thumb.png" medium="image">
			<media:title type="html">TaskScheduler - Final Result</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging efficiente parte 2: Attributi</title>
		<link>http://umarja.wordpress.com/2009/03/29/debugging-efficiente-parte-2-attributi/</link>
		<comments>http://umarja.wordpress.com/2009/03/29/debugging-efficiente-parte-2-attributi/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 18:52:00 +0000</pubDate>
		<dc:creator>umarja</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Logging]]></category>
		<category><![CDATA[Tracing]]></category>

		<guid isPermaLink="false">http://umarja.wordpress.com/2009/03/29/debugging-efficiente-parte-2-attributi/</guid>
		<description><![CDATA[Nella prima parte dell’articolo è stato introdotto il debugging e come fosse possibile sfruttare le classi Debug e Debugger messe a disposizione dal .NET Framework. In questa seconda parte si parlerà di Attributi che semplificano il debugging. Gli attributi sono un metodo dichiarativo per eseguire azioni che altrimenti richiederebbero tempo e righe di codice. Inoltre, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=25&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Nella <a href="http://umarja.wordpress.com/2009/03/26/debugging-efficiente/">prima parte</a> dell’articolo è stato introdotto il debugging e come fosse possibile sfruttare le classi Debug e Debugger messe a disposizione dal .NET Framework.</p>
<p>In questa seconda parte si parlerà di Attributi che semplificano il debugging. Gli attributi sono un metodo dichiarativo per eseguire azioni che altrimenti richiederebbero tempo e righe di codice. Inoltre, il vantaggio degli attributi è il fatto che non riempono il codice di righe inutili e sono facilmente rinuovibili nella fase di deployment.</p>
<p> <span id="more-25"></span>
<p>Di seguito verranno discussi i seguenti attributi:</p>
<ul>
<li>DebuggerBrowsableAttribute </li>
<li>DebuggerDisplayAttribute </li>
<li>DebuggerHiddenAttribute </li>
<li>DebuggerNonUserCodeAttribute </li>
<li>DebuggerStepperBoundaryAttribute </li>
<li>DebuggerStepThroughAttribute </li>
<li>DebuggerTypeProxyAttribute </li>
<li>DebuggerVisualizerAttribute </li>
</ul>
<p>Prima di cominciare è meglio creare una classe che verrà utilizzata nei codici sorgenti d’esempio:</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 1</span>&#160;<span style="color:blue;">Class</span> Person</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 2</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Private</span> m_Name <span style="color:blue;">As</span> <span style="color:blue;">String</span> = <span style="color:blue;">String</span>.Empty</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Private</span> m_LastName <span style="color:blue;">As</span> <span style="color:blue;">String</span> = <span style="color:blue;">String</span>.Empty</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Private</span> m_Phones <span style="color:blue;">As</span> <span style="color:blue;">String</span>() = <span style="color:blue;">New</span> <span style="color:blue;">String</span>() {}</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Private</span> m_Addresses <span style="color:blue;">As</span> <span style="color:blue;">String</span>() = <span style="color:blue;">New</span> <span style="color:blue;">String</span>() {}</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Private</span> m_Age <span style="color:blue;">As</span> <span style="color:blue;">Integer</span> = 0</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 8</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Property</span> Name() <span style="color:blue;">As</span> <span style="color:blue;">String</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 10</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 11</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Return</span> m_Name</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Set</span>(<span style="color:blue;">ByVal</span> value <span style="color:blue;">As</span> <span style="color:blue;">String</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 14</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_Name = value</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 15</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Set</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Property</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 17</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 18</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Property</span> LastName() <span style="color:blue;">As</span> <span style="color:blue;">String</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 19</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 20</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Return</span> m_LastName</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 21</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Set</span>(<span style="color:blue;">ByVal</span> value <span style="color:blue;">As</span> <span style="color:blue;">String</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_LastName = value</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 24</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Set</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 25</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Property</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 26</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 27</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Property</span> Phones() <span style="color:blue;">As</span> <span style="color:blue;">String</span>()</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 29</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Return</span> m_Phones</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 30</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 31</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Set</span>(<span style="color:blue;">ByVal</span> value <span style="color:blue;">As</span> <span style="color:blue;">String</span>())</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_Phones = value</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Set</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Property</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 35</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 36</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Property</span> Addresses() <span style="color:blue;">As</span> <span style="color:blue;">String</span>()</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 37</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 38</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Return</span> m_Addresses</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 39</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 40</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Set</span>(<span style="color:blue;">ByVal</span> value <span style="color:blue;">As</span> <span style="color:blue;">String</span>())</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 41</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_Addresses = value</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 42</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Set</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 43</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Property</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 44</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 45</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Property</span> Age() <span style="color:blue;">As</span> <span style="color:blue;">Integer</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 46</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 47</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Return</span> m_Age</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 48</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Get</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 49</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">Set</span>(<span style="color:blue;">ByVal</span> value <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>)</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 50</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; m_Age = value</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 51</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Set</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 52</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Property</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 53</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 54</span>&#160;<span style="color:blue;">End</span> <span style="color:blue;">Class</span></p>
</p></div>
<p>Ed ecco il codice sorgente in C#</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 1</span>&#160;<span style="color:blue;">class</span> <span style="color:#2b91af;">Person</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 3</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">string</span> m_Name = <span style="color:blue;">string</span>.Empty;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 5</span>&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">string</span> m_LastName = <span style="color:blue;">string</span>.Empty;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">string</span>[] m_Phones = <span style="color:blue;">new</span> <span style="color:blue;">string</span>[] { };</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">string</span>[] m_Addresses = <span style="color:blue;">new</span> <span style="color:blue;">string</span>[] { };</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 8</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; <span style="color:blue;">private</span> <span style="color:blue;">int</span> m_Age = 0;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 10</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 11</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> Name {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 12</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_Name; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 13</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">set</span> { m_Name = <span style="color:blue;">value</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 14</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 15</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 16</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> LastName {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 17</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_LastName; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 18</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">set</span> { m_LastName = <span style="color:blue;">value</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 19</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 20</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 21</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span>[] Phones {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 22</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_Phones; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 23</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">set</span> { m_Phones = <span style="color:blue;">value</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 24</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 25</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 26</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span>[] Addresses {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 27</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_Addresses; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 28</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">set</span> { m_Addresses = <span style="color:blue;">value</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 29</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 30</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 31</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">int</span> Age {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 32</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">get</span> { <span style="color:blue;">return</span> m_Age; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 33</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color:blue;">set</span> { m_Age = <span style="color:blue;">value</span>; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 34</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 35</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 36</span> }</p>
</p></div>
<p>Ora si comincia con l’esplorazione dei vari attributi, partendo da DebuggerBrowsable.</p>
<h3>DebuggerBrowsable</h3>
<p>L’Attributo DebuggerBrowsable permette di nascondere un membro di un oggetto dalla finestra Locals di Visual Studio. Ad esempio, nella classe Person, se non si vuole vedere i campi della classe (quelli che iniziano per ‘m_’ per intenderci) basta applicare un attributo DebuggerBrowsable e specificare come valore <em>Never</em>. Ecco il risultato:</p>
<p><a href="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable.jpg"><img style="display:inline;border-width:0;" title="Debugging2 - DebuggerBrowsable" border="0" alt="Debugging2 - DebuggerBrowsable" src="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable-thumb.jpg?w=578&#038;h=177" width="578" height="177" /></a> </p>
</p>
<p>Come si può notare i campi privati non sono stati mostrati, cosa che sarebbe successa se non fosse stato applicato loro l’attributo DebuggerBrowsable. L’Attributo DebuggerBrowsable accetta un’enumerazione che presenta i seguenti valori:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td valign="top" width="85">Collapsed</td>
<td valign="top" width="315">Il membro viene visualizzato, ma con un ‘+’ accanto che permette di espanderlo.</td>
</tr>
<tr>
<td valign="top" width="85">Never</td>
<td valign="top" width="315">Il membro non viene mai visualizzato</td>
</tr>
<tr>
<td valign="top" width="85">RootHidden</td>
<td valign="top" width="315">Il membro, se facente parte di una collezione, viene visualizzato come mebro dell’oggetto che contiene la collezione. Di seguito è mostrato un esempio.</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<p><a href="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable2.jpg"><img style="display:inline;border-width:0;" title="Debugging2 - DebuggerBrowsable2" border="0" alt="Debugging2 - DebuggerBrowsable2" src="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable2-thumb.jpg?w=578&#038;h=178" width="578" height="178" /></a></p>
<p>In questo caso l’attributo DebuggerBrowsable è stato applicato agli array Phones e Addresses con valore RootHidden.</p>
<h3>DebuggerDisplay</h3>
<p>L’attributo DebuggerDisplay indica al debugger come un oggetto deve essere mostrato.</p>
<p>Accetta un unico argomento che indica come deve essere formattato l’output mostrato dal debugger. Nella stringa di formattazione, qualsiasi nome contenuto tra due parentesi graffe o tonde viene valutato come campo, metodo o proprietà.</p>
<p>Si consideri il seguente attributo applicato alla classe Person descritta sopra:</p>
<p>&#160;&#160;&#160; 1 [DebuggerDisplay(&quot;Name = {Name}, LastName = {LastName}, Phones = {Phones(0)}, Age = {Age}&quot;)]</p>
<p>&#160;&#160;&#160; 1 &lt;DebuggerDisplay(&quot;Name = {Name}, LastName = {LastName}, Phones = {Phones(0)}, Age = {Age}&quot;)&gt;</p>
<p>L’attributo specifica che la classe, nella colonna <em>Value</em> della finestra Locals di Visual Studio avrà come output il Nome, il Cognome, il primo numero di telefono (Phones è un array) e l’Età della persona, come mostrato in seguito:</p>
<p><a href="http://umarja.files.wordpress.com/2009/03/debugging2debuggerdisplay.jpg"><img style="display:inline;border-width:0;" title="Debugging2 - DebuggerDisplay" border="0" alt="Debugging2 - DebuggerDisplay" src="http://umarja.files.wordpress.com/2009/03/debugging2debuggerdisplay-thumb.jpg?w=601&#038;h=175" width="601" height="175" /></a> </p>
</p>
<p>Un metodo per evitare di utilizzare questa classe è ereditare il metodo ToString della classe Object. Questo viene considerato come output da Visual Studio solo se all’oggetto non è applicato un attributo DebuggerDisplay. Ovviamente questa pratica è vietata in quanto il metodo ToString non serve solo in fase di debugging.</p>
<h3>DebuggerHidden, DebuggerNonUserCode, DebuggerStepThrough</h3>
<p>Indica che il codice o il metodo a cui è applicato l’attributo non viene mostrato dal debugger nel caso di DebuggerHidden. La stessa cosa vale anche per DebuggerNonUserCode, che indica al debugger il fatto&#160; che il codice è stato generato da un tool, o è stato già testato e quindi è inutile testarlo. Il debugger non permette quindi di inserire breakpoint o di fermare l’esecuzione del programma (usando Debugger.Break). L’attributo DebuggerStepThrough indica al debugger di saltare il metodo o il codice a cui è applicato e di andare al sucessivo codice scritto dall’utente, quindi senza l’attributo DebuggerStepThough, DebuggerHidden o DebuggerNonUserCode.</p>
<h3>DebuggerStepperBoundary</h3>
<p>L’attributo specifica che il codice da questo momento in poi non deve essere eseguito in step-mode, ma deve essere eseguito senza stepping. Quindi ha lo stesso effetto di premere il pulsante F5 in Visual Studio per continuare l’esecuzione del programma senza effettuare lo stepping (effettuato usando il pulsante F10).</p>
<h3>DebuggerTypeProxy</h3>
<p>L’attributo specifica che il contenuto dell’oggetto a cui è applicato, nella finestra Locals di Visual Studio, non deve essere mostrato, ma deve essere sostituito dal contenuto di un oggetto <em>proxy</em>, che viene istanziata e vengono mostrati i suoi membri pubblici. L’oggetto proxy solitamente è un oggetto annidato all’interno di quello che rappresenta e deve avere un costruttore che accetta come parametro l’oggetto che rappresenta.</p>
<p>Si applichi il seguente attributo alla classe Person descritta sopra e copiare il codice della classe PersonProxy mostra di seguito come classe annidata di Person.</p>
<p>&#160;&#160;&#160; 1 [DebuggerTypeProxy(typeof(PersonProxy))]</p>
<p>&#160;&#160;&#160; 1 &lt;DebuggerTypeProxy(GetType(Person.PersonProxy))&gt;</p>
<p>Ed ecco il codice della classe PersonProxy</p>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 1</span>&#160;<span style="color:blue;">class</span> <span style="color:#2b91af;">PersonProxy</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 2</span> {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> Message = <span style="color:#a31515;">&quot;Dovresti vedere questo messaggio e il nome invece dei membri della classe Person&quot;</span>;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> <span style="color:blue;">string</span> Name = <span style="color:#2b91af;">String</span>.Empty;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 5</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; <span style="color:blue;">public</span> PersonProxy(<span style="color:#2b91af;">Person</span> person)</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160; {</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = person.Name;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 9</span>&#160;&#160;&#160;&#160; }</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 10</span> }</p>
</p></div>
<div style="font-family:courier new;background:white;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 1</span>&#160;<span style="color:blue;">Class</span> PersonProxy</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 2</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 3</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> Name <span style="color:blue;">As</span> <span style="color:blue;">String</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 4</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> Message <span style="color:blue;">As</span> <span style="color:blue;">String</span> = <span style="color:#a31515;">&quot;Dovresti vedere questo messaggio e il nome invece dei membri della classe Person&quot;</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 5</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 6</span>&#160;&#160;&#160;&#160; <span style="color:blue;">Public</span> <span style="color:blue;">Sub</span> <span style="color:blue;">New</span>(<span style="color:blue;">ByVal</span> pers <span style="color:blue;">As</span> Person)</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 7</span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Name = pers.Name</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 8</span>&#160;&#160;&#160;&#160; <span style="color:blue;">End</span> <span style="color:blue;">Sub</span></p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160;&#160; 9</span>&#160;</p>
<p style="margin:0;"><span style="color:#2b91af;">&#160;&#160; 10</span>&#160;<span style="color:blue;">End</span> <span style="color:blue;">Class</span></p>
</p></div>
</p>
<p>Si otterrà un risultato del genere</p>
<p><a href="http://umarja.files.wordpress.com/2009/03/debugging2debuggertypeproxy.jpg"><img style="border-bottom:0;border-left:0;display:inline;border-top:0;border-right:0;" title="Debugging2 - DebuggerTypeProxy" border="0" alt="Debugging2 - DebuggerTypeProxy" src="http://umarja.files.wordpress.com/2009/03/debugging2debuggertypeproxy-thumb.jpg?w=597&#038;h=178" width="597" height="178" /></a> </p>
<p>Conclusioni</p>
<p>Con questo articolo concludo il debugging efficiente. Spero che sia interessante e spero che abbia aiutato qualcuno, sopratutto per l’esame 70-536. Auguro buon coding a tutti.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/umarja.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/umarja.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/umarja.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=25&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umarja.wordpress.com/2009/03/29/debugging-efficiente-parte-2-attributi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91822b4ed2c380d676d941584b0f8f49?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umarja</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable-thumb.jpg" medium="image">
			<media:title type="html">Debugging2 - DebuggerBrowsable</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/03/debugging2debuggerbrowsable2-thumb.jpg" medium="image">
			<media:title type="html">Debugging2 - DebuggerBrowsable2</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/03/debugging2debuggerdisplay-thumb.jpg" medium="image">
			<media:title type="html">Debugging2 - DebuggerDisplay</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/03/debugging2debuggertypeproxy-thumb.jpg" medium="image">
			<media:title type="html">Debugging2 - DebuggerTypeProxy</media:title>
		</media:content>
	</item>
		<item>
		<title>Debugging efficiente</title>
		<link>http://umarja.wordpress.com/2009/03/26/debugging-efficiente/</link>
		<comments>http://umarja.wordpress.com/2009/03/26/debugging-efficiente/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 19:15:00 +0000</pubDate>
		<dc:creator>umarja</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Logging]]></category>

		<guid isPermaLink="false">http://umarja.wordpress.com/2009/03/26/debugging-efficiente/</guid>
		<description><![CDATA[Il debugging è una fase fondamentale nello sviluppo di software. L’utilizzo di buone tecniche di debugging è alla base per la creazione di software efficienti e funzionanti. Ma il punto di questo articolo non è solo il come utilizzare il debugging per creare codice migliore, ma come risparmiare tempo utilizzando un po di classi offerte [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=6&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Il debugging è una fase fondamentale nello sviluppo di software. L’utilizzo di buone tecniche di debugging è alla base per la creazione di software efficienti e funzionanti. Ma il punto di questo articolo non è solo il come utilizzare il debugging per creare codice migliore, ma come risparmiare tempo utilizzando un po di classi offerte dal .NET Framework orientate proprio a questa fase del development life life-cycle.</p>
<p><span id="more-6"></span></p>
<p>Il primo passo per il testing/debugging efficiente è l’utilizzo della classe Debugger. Essa ha solo due metodi importanti: Log e Break. Il primo permette di scrivere sulla finestra del debugger (nel caso di Visual Studio si parla della Output Window). Il metodo Log permette di specificare il livello di gravità del messaggio, un messaggio ed una categoria che descriva il messaggio. Il metodo Break, invece, permette di bloccare l’esecuzione del software per passare all’interfaccia del software di debugging. Nel caso di Visual Studio, la chiamata al metodo Break ha lo stesso effetto dato dall’inserimento di un breakpoint. Il vantaggio nell’uso del metodo Break sta nel fatto che è possibile bloccare l’applicazione dopo il verificarsi di un evento o in modo condizionale, ad esempio dopo aver validato il contenuto restituito da un server remoto.</p>
<p>Altro strumento utile per il debugging è il logging degli eventi dell’applicazione. Per logging si intende la possibilità di scrivere in qualche luogo dei messaggi, anche piccoli per descrivere lo stato attuale delle operazioni del software, o dopo il verificarsi di determinati eventi.</p>
<p>La classe Debug è simile alla classe Debugger come finalità, ma molto piu’ potente. I metodi e le proprietà pricipali della classe Debug sono:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="57" valign="top">Assert</td>
<td width="343" valign="top">Mostra un messaggio di errore se la condizione passata come argomento risulta falsa.</td>
</tr>
<tr>
<td width="57" valign="top">Close</td>
<td width="343" valign="top">Effettua il Flush del buffer e chiude tutti i listeners associati.</td>
</tr>
<tr>
<td width="57" valign="top">Fail</td>
<td width="343" valign="top">Mostra un messaggio di errore.</td>
</tr>
<tr>
<td width="57" valign="top">Flush</td>
<td width="343" valign="top">Scrive il buffer in memoria sui listeners associati.</td>
</tr>
<tr>
<td width="57" valign="top">Indent</td>
<td width="343" valign="top">Indenta il testo di output. Serve sopratutto per la fomattazione di testo.</td>
</tr>
<tr>
<td width="57" valign="top">Print</td>
<td width="343" valign="top">Scrive un messaggio seguito da un line terminator sui vari listeners associati.</td>
</tr>
<tr>
<td width="57" valign="top">Unindent</td>
<td width="343" valign="top">Operazione opposta al metodo Indent, quindi diminuisce l’indentazione del testo.</td>
</tr>
<tr>
<td width="57" valign="top">Write</td>
<td width="343" valign="top">Scrive il messaggio specificato sui listeners associati</td>
</tr>
<tr>
<td width="57" valign="top">WriteIf</td>
<td width="343" valign="top">Come il metodo Write, solo che accetta una condizione che, se valutata, richiama il metodo Write.</td>
</tr>
<tr>
<td width="57" valign="top">WriteLine</td>
<td width="343" valign="top">Scrive il messaggio specificato seguito da un line terminator sui vari listeners associati</td>
</tr>
<tr>
<td width="57" valign="top">WriteLineIf</td>
<td width="343" valign="top">Come il metodo WriteLine, solo che accetta una condizione che, se valutata, richiama il metodo WriteLine</td>
</tr>
</tbody>
</table>
<p>Il metodo Assert è forse il metodo che più utilizzo della classe Debug, in quanto permette di mostrare un messaggio di errore se la condizione passata come argomento risulta restituisce ‘False’. Si consideri il seguente esempio</p>
<div style="font-family:courier new;background:white 0 0;color:black;font-size:10pt;">
<p style="margin:0;"><span style="color:#2b91af;">1</span> <span style="color:blue;">int</span> myResult = PerformCalculation();</p>
<p style="margin:0;"><span style="color:#2b91af;">2</span> <span style="color:#2b91af;">Debug</span>.Assert((myResult != 1), <span style="color:#a31515;">&#8220;myResult non ha il valore atteso, cioé 1&#8243;</span>);</p>
</div>
<p>Qui, se la variabile myResult ha un valore uguale a 1, l&#8217;esecuzione del programma viene interrotta e viene mostrato un messaggio di errore simile a questo:</p>
<div id="attachment_8" class="wp-caption alignnone" style="width: 420px"><img class="size-full wp-image-8" title="Figura 1" src="http://umarja.files.wordpress.com/2009/03/debug-asser.jpg?w=655" alt="Messaggio di erorre visualizzato dalla chiamata al metodo Assert"   /><p class="wp-caption-text">Messaggio di erorre visualizzato dalla chiamata al metodo Assert</p></div>
<p>Il metodo fail è simile al metodo Assert, solo che non accetta condizioni ma solo un messaggio di errore da visualizzare. E&#8217; utilizzato quando il programma raggiunge uno stato di instabilità e quindi non può proseguire con l&#8217;esecuzione.</p>
<p>I metodi Indent e Unindent permettono rispettivamente di indentare e rimuovere l&#8217;indentatura del testo. Sono utilizzati prevalentemente per la formattazione dei messaggi di output.</p>
<p>I metodi Write e WriteLine permettono di scrivere sui listeners dei messaggi, mentre i metodi WriteIf e WriteLineIf accettano una condizione e scrivono il messaggio solo se la condizione è verificata, cioé restituisce &#8216;True&#8217;.</p>
<p>Un fatto molto positivo riguardante la classe Debug e Debugger è che se il programma viene compilato con la configurazione &#8216;Release&#8217; in Visual Studio, ogni singola chiamata ai metodi delle classi Debug e Debugger viene eliminato automaticamente. Questo permette di risparmiare un sacco di tempo, in quanto non bisogna stare a cancellare/commentare ogni singola riga.</p>
<p>I listeners non sono altro che degli oggetti associato al programma e vengono configurati per il tracing. Per maggiori informazioni sul tracing e sui trace listeners andare <a title="MSDN Library" href="http://msdn.microsoft.com/it-it/library/aa984115.aspx" target="_blank">qui</a>.</p>
<p>Per ora è tutto. In futuro parlerò degli attributi che è possibile utilizzare per migliorare la fase di debugging in Visual Studio 2008.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/umarja.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/umarja.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/umarja.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=umarja.wordpress.com&amp;blog=7115602&amp;post=6&amp;subd=umarja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://umarja.wordpress.com/2009/03/26/debugging-efficiente/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/91822b4ed2c380d676d941584b0f8f49?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">umarja</media:title>
		</media:content>

		<media:content url="http://umarja.files.wordpress.com/2009/03/debug-asser.jpg" medium="image">
			<media:title type="html">Figura 1</media:title>
		</media:content>
	</item>
	</channel>
</rss>
