<?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>using System.Reflection; &#187; .net</title>
	<atom:link href="http://caxaria.org/category/programacao/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://caxaria.org</link>
	<description>System.Brain.Dump();</description>
	<lastBuildDate>Tue, 01 Jun 2010 15:27:25 +0000</lastBuildDate>
	<language>pt</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='caxaria.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/866b65b16b713e6eb6f8d8c57f7b8e1f?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>using System.Reflection; &#187; .net</title>
		<link>http://caxaria.org</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://caxaria.org/osd.xml" title="using System.Reflection;" />
	<atom:link rel='hub' href='http://caxaria.org/?pushpress=hub'/>
		<item>
		<title>Visual Studio AddIn</title>
		<link>http://caxaria.org/2009/09/14/visual-studio-addin/</link>
		<comments>http://caxaria.org/2009/09/14/visual-studio-addin/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 10:21:23 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/?p=275</guid>
		<description><![CDATA[Quem trabalha com TestDrivenDevelopment sabe que a pior parte de conseguir uma boa code coverage é que há alguns testes que são demasiado entediantes para se estarem sempre a fazer&#8230; Infelizmente, não os fazendo, a % de code coverage desce, o que leva a que os outros testes &#8211; estes mais importantes &#8211; também passem [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=275&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Quem trabalha com TestDrivenDevelopment sabe que a pior parte de conseguir uma boa code coverage é que há alguns testes que são demasiado entediantes para se estarem sempre a fazer&#8230; Infelizmente, não os fazendo, a % de code coverage desce, o que leva a que os outros testes &#8211; estes mais importantes &#8211; também passem um pouco despercebidos.</p>
<p>Para ajudar na criação deste tipo de testes, estive há uns meses a trabalhar com uma biblioteca de geração automática de testes, <a href="http://stdl.sourceforge.net/">STDL</a>. Esta biblioteca, apesar de ser bastante interessante, implicava ainda algum trabalho manual para que os testes fossem gerados.</p>
<p>Para ajudar a combater isso (e para aprender um pouco mais sobre o ecosistema do VS), passei um fim de semana a criar um addin para o visual studio. Este addin acrescenta ao botão direito do rato uma nova opção para gerar uns stubs para os testes STDL (faz um pouco mais, por inspecção da classe em que se clica mas&#8230; <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Hoje lembrei-me disso e achei que seria interessante colocar aqui umas referências pois nunca se sabe quando irei precisar de gerar testes em .net novamente <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Podem ver mais <a href="http://groups.google.com/group/stdl/browse_thread/thread/92d638d84835cad0">neste post</a>, sacar uma beta release <a href="http://stdl.googlegroups.com/web/stdl_VS_AddIn.zip?gda=LLdcZ0MAAACT4uXTqZ1B7vH_lIKfer_mTTIHa2FPLZb2es-hoFJOriAJggvm-gsF-eFt9FyB2RQytiJ-HdGYYcPi_09pl8N7FWLveOaWjzbYnpnkpmxcWg">aqui</a> ou ver o código <a href="http://sourceforge.net/projects/stdl/develop">aqui</a>.</p>
<p>Se acharem a plataforma interessante, falem com o Neville Grech na <a href="http://groups.google.com/group/stdl">mailing list</a> para ver se o projecto recomeça&#8230; Eu era capaz de fazer mais uma ou duas linhas de código <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/275/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/275/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/275/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=275&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2009/09/14/visual-studio-addin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>F#</title>
		<link>http://caxaria.org/2008/11/14/f/</link>
		<comments>http://caxaria.org/2008/11/14/f/#comments</comments>
		<pubDate>Fri, 14 Nov 2008 10:54:15 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/11/14/f/</guid>
		<description><![CDATA[Para mais tarde recordar<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=238&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://channel9.msdn.com/pdc2008/TL11/">Para mais tarde recordar</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/238/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/238/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/238/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=238&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/11/14/f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>Yahoo Vs Microsoft</title>
		<link>http://caxaria.org/2008/10/22/yahoo-vs-microsoft/</link>
		<comments>http://caxaria.org/2008/10/22/yahoo-vs-microsoft/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 09:01:42 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Microsoft]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/10/22/yahoo-vs-microsoft/</guid>
		<description><![CDATA[Apesar de não ter escrito nada de novo, acho que o Phil Wright resumiu tudo perfeitamente: Yahoo have announced they are cutting 10% of its global workforce, about 1500 people, as it tries to cope with its ongoing problems. They also reported a 64% drop in third quarter profits and only a 1% increase in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=211&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Apesar de não ter escrito nada de novo, acho que o Phil Wright <a href="http://www.componentfactory.com/blog/?p=399">resumiu tudo perfeitamente</a>:</p>
<blockquote><p>Yahoo have announced they are cutting 10% of its global workforce, about 1500 people, as it tries to cope with its ongoing problems. They also reported a 64% drop in third quarter profits and only a 1% increase in revenue over a year ago(&#8230;) Remember this is the same company that turned down a $47.5bn offer from Microsoft last January. I wonder if Jerry Yang still thinks that offer ‘undervalued the company’. This is where the problem of having the original founder as CEO really causes problems. </p>
</blockquote>
<p> E, já que vão ao site, aproveitem para experimentar os <a href="http://www.componentfactory.com/products.php">controlos .net</a> que ele vende. Ah, e <a href="http://www.componentfactory.com/products_toolkit.php">alguns são grátis</a>!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=211&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/10/22/yahoo-vs-microsoft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>Aplica&#231;&#245;es &#8216;Single-instance&#8217;</title>
		<link>http://caxaria.org/2008/08/04/aplicaes-single-instance/</link>
		<comments>http://caxaria.org/2008/08/04/aplicaes-single-instance/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 11:15:14 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Links]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/08/04/aplicaes-single-instance/</guid>
		<description><![CDATA[Podem ver aqui.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=206&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Podem ver <a href="http://www.hanselman.com/blog/TheWeeklySourceCode31SingleInstanceWinFormsAndMicrosoftVisualBasicdll.aspx">aqui</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/206/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/206/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/206/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/206/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/206/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=206&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/08/04/aplicaes-single-instance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>Plugins: Shell</title>
		<link>http://caxaria.org/2008/07/31/plugins-shell/</link>
		<comments>http://caxaria.org/2008/07/31/plugins-shell/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 14:33:18 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/31/plugins-shell/</guid>
		<description><![CDATA[Depois de terminadas as férias (incluindo as férias mentais), está na altura de recomeçar a escrever sobre coisas mais técnicas, como os plugins. O próximo passo vai permitir que o primeiro grande objectivo se aproxime: permitir que os plugins adicionem elementos à interface usando a &#8216;Shell&#8217;, que representa a aplicação vista pelos olhos dos plugins. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=200&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Depois de terminadas as férias (incluindo as férias mentais), está na altura de recomeçar a escrever sobre coisas mais técnicas, como os plugins.</p>
<p>O próximo passo vai permitir que o primeiro grande objectivo se aproxime: permitir que os plugins adicionem elementos à interface usando a &#8216;Shell&#8217;, que representa a aplicação vista pelos olhos dos plugins. A partir desta classe vai ser possível adicionar elementos aos forms, abrir novos documentos, lançar eventos para outros plugins, etc:</p>
<p><span id="more-200"></span>
</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:234px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> Shell(IMainForm mainForm)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">lock</span>(<span style="color:#0000ff;">typeof</span>(Shell))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         <span style="color:#0000ff;">if</span>(_instance != <span style="color:#0000ff;">null</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>             <span style="color:#0000ff;">throw</span> <span style="color:#0000ff;">new</span> InvalidOperationException();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         _mainForm = mainForm;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         _pluginManager = <span style="color:#0000ff;">new</span> PluginManager();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         _documents = <span style="color:#0000ff;">new</span> Documents(<span style="color:#0000ff;">this</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         _instance = <span style="color:#0000ff;">this</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>     _dataAccess = <span style="color:#0000ff;">new</span> DataAccess();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     InitializePlugins();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     Events.SafeFire(Initialized, <span style="color:#0000ff;">this</span>, <span style="color:#0000ff;">new</span> ShellEventArgs(<span style="color:#0000ff;">this</span>));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> }</pre>
</div>
</div>
<p>As classes descritas até agora (<a href="http://caxaria.wordpress.com/2008/07/07/plugins-o-inicio/">parte 1</a> e <a href="http://caxaria.wordpress.com/2008/07/09/plugins-pluginmanager/">parte 2</a>) estão num projecto sem qualquer acesso a forms, não incluindo o &#8216;System.Windows.Forms&#8217; nem qualquer assembly parecida. Por isso o construtor da &#8216;Shell&#8217; recebe um objecto do tipo <em>IMainForm</em>, que será o ponto de passagem para o mundo real ou, no neste caso, o form.</p>
<p>Depois de garantir que apenas uma instância da classe é inicializada &#8211; pois não seria desejável que os plugins pudessem inicializar <em>Shell&#8217;s</em> a gosto &#8211; iniciamos uma série de classes que irão tratar de diferentes aspectos da aplicação: Acesso a dados, gestão dos documentos, etc, acabando com a inicialização dos plugins e acabando a lançar um evento, representando o final da inicialização da <em>&#8216;Shell&#8217;.</em></p>
<p>Sendo que as outras classes serão descritas posteriormente, resta mostrar o que se faz na inicialização dos plugins:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">void</span> InitializePlugins()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     _loadedPlugins = _pluginManager.LoadPlugins();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">foreach</span> (IPlugin plugin <span style="color:#0000ff;">in</span> _loadedPlugins)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         plugin.Initialize(_instance);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         <span style="color:#0000ff;">try</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             plugin.Initialize(_instance);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>         <span style="color:#0000ff;">catch</span> (Exception)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>             <span style="color:#008000;">//Could not initialize plugin</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>             <span style="color:#008000;">//TODO: report back result of process.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span> }</pre>
</div>
</div>
<p>Como no <a href="http://caxaria.wordpress.com/2008/07/09/plugins-pluginmanager/">PluginManager</a>, em que ao invocar o plugin se tinha o cuidado de apanhar qualquer excepção, aqui, ao chamar o método de inicialização, apanhamos qualquer excepção que possa ocorrer de modo a salvaguardar a aplicação. Novamente, fica por fazer alguma forma de conseguir mostrar ao utilizador que houve plugins que falharam mas isso será feito posteriormente, assim que o código nos mostrar como.</p>
<p>Finalmente, também temos de actualizar a interface do IPlugin:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> IPlugin</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">void</span> Initialize(Shell shell);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> }</pre>
</div>
</div>
<p>E, deixando assim, o código quase que compila e, melhor que tudo, quase que corre! O próximo passo será realmente colocar o código a correr e com um plugin a introduzir qualquer coisa no ecrã!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/200/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/200/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/200/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/200/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/200/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=200&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/31/plugins-shell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>I/O Ass&#237;ncrono</title>
		<link>http://caxaria.org/2008/07/09/io-assncrono/</link>
		<comments>http://caxaria.org/2008/07/09/io-assncrono/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 11:02:08 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/09/io-assncrono/</guid>
		<description><![CDATA[Hoje a ler a revista MSDN online reparei num artigo que fala sobre implementar I/O assíncrono usando delegados para os callbacks. Imediatamente lembrei-me do projecto que fiz na Inosat, em que para conseguir rede, GPS ou GRPS no dispositivo móvel, usei um sistema parecido com este. A maior diferença é que, em vez de declarar [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=144&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Hoje a ler a revista <a href="http://msdn.microsoft.com/en-gb/magazine/">MSDN online</a> reparei num artigo que fala sobre implementar I/O assíncrono usando delegados para os callbacks. Imediatamente lembrei-me do projecto que fiz na <a href="http://www.inosat.com/index_home.php">Inosat</a>, em que para conseguir rede, GPS ou GRPS no dispositivo móvel, usei um sistema parecido com este. </p>
<p>A maior diferença é que, em vez de declarar dois métodos para callback &#8211; um em caso de sucesso e outro em caso de falha &#8211; usei apenas um, retornando sempre um valor do tipo Result&lt;T&gt;. Por exemplo, se virem no artigo, o autor para o método:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">string</span> ReadAllText(<span style="color:#0000ff;">string</span> path);</pre>
</div>
</div>
<p>define a seguinte assinatura:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> ReadAllTextAsync(<span style="color:#0000ff;">string</span> path, Action&lt;<span style="color:#0000ff;">string</span>&gt; success, Action&lt;Exception&gt; failure);</pre>
</div>
</div>
<p>em que o primeiro método é chamado se o método correr como esperado, o segundo é invocado se houver algum problema pelo caminho. No meu caso, a definição seria algo como:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> ReadAllTextAsync(<span style="color:#0000ff;">string</span> path, Result&lt;<span style="color:#0000ff;">string</span>&gt; success);</pre>
</div>
</div>
<p>em que a classe Result é:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span> Result&lt;T&gt;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> T _value;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#0000ff;">bool</span> _valid;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">readonly</span> <span style="color:#0000ff;">string</span> _error;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>     <span style="color:#0000ff;">private</span> Result(T <span style="color:#0000ff;">value</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>         _value = <span style="color:#0000ff;">value</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>         _valid = <span style="color:#0000ff;">true</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>     <span style="color:#0000ff;">private</span> Result(<span style="color:#0000ff;">string</span> error) : <span style="color:#0000ff;">this</span>()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span>         _error = error;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  16:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  17:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  18:</span>     <span style="color:#0000ff;">private</span> Result()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  19:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  20:</span>         _valid = <span style="color:#0000ff;">false</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  21:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  22:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  23:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Result&lt;T&gt; Failed()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  24:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  25:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> Result&lt;T&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  26:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  27:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  28:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Result&lt;T&gt; Failed(<span style="color:#0000ff;">string</span> error)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  29:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  30:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> Result&lt;T&gt;(error);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  31:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  32:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  33:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> Result&lt;T&gt; Success(T <span style="color:#0000ff;">value</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  34:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  35:</span>         <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">new</span> Result&lt;T&gt;(<span style="color:#0000ff;">value</span>);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  36:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  37:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  38:</span>     <span style="color:#0000ff;">public</span> T Value</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  39:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  40:</span>         get { <span style="color:#0000ff;">return</span> _value; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  41:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  42:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  43:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">bool</span> Valid</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  44:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  45:</span>         get { <span style="color:#0000ff;">return</span> _valid; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  46:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  47:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  48:</span>     <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">string</span> Error</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  49:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  50:</span>         get { <span style="color:#0000ff;">return</span> _error; }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  51:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  52:</span> }</pre>
</div>
</div>
<p>Isto permite que o Result seja usado para notificar se o método teve sucesso ou não e, caso não tenha, que se possa passar o erro que se encontrou.</p>
<p>A diferença que encontro nas duas abordagens é que, no meu caso, quem trata do resultado tem de verificar se a operação teve sucesso chamando o result.Valid. No caso da implementação que está na <a href="http://msdn.microsoft.com/en-gb/magazine/cc700359.aspx">revista</a>, cada método sabe se o resultado teve sucesso ou não. Para os <a href="http://blog.objectmentor.com/articles/2007/06/05/are-else-blocks-the-root-of-all-evil">puristas que defendem a abolição do uso de If&#8217;s</a> (já trabalhei com alguns <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ), isto é puro ouro!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/144/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/144/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=144&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/09/io-assncrono/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>Plugins: PluginManager</title>
		<link>http://caxaria.org/2008/07/09/plugins-pluginmanager/</link>
		<comments>http://caxaria.org/2008/07/09/plugins-pluginmanager/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 09:19:44 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/09/plugins-pluginmanager/</guid>
		<description><![CDATA[O primeiro problema que tenho de enfrentar para conseguir usar um sistema de plugins é conseguir identificar os plugins que temos disponíveis e os inicializar. Para isso vou criar o PluginManager. De alguma forma tenho de definir como é que os plugins serão descobertos pela aplicação: Lidos automaticamente de uma directoria pré-definida, previamente registados usando [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=140&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">O primeiro problema que tenho de enfrentar para conseguir usar um sistema de plugins é conseguir identificar os plugins que temos disponíveis e os inicializar. Para isso vou criar o PluginManager.</p>
<p align="justify">De alguma forma tenho de definir como é que os plugins serão descobertos pela aplicação: Lidos automaticamente de uma directoria pré-definida, previamente registados usando uma UI na aplicação, ficheiros de configuração, etc. Como não quero ter de decidir isso para já, vou implementar a forma mais fácil que me consiga lembrar e, tendo o cuidado de não ter o uso desta implementação especifica &#8216;hard-coded&#8217; em lugar algum, vou permitir que mais tarde se possa mudar o plugin manager (quem sabe usando.. um plugin!)</p>
<p><span id="more-140"></span>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> IPluginManager</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     IPlugin[] LoadPlugins();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>&nbsp; </pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">interface</span> IPlugin</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> }</pre>
</div>
</div>
<p>Temos então uma interface para o PluginManager que apenas tem um método, que devolve todos os plugins que foram encontrados.</p>
<p>Para a primeira implementação do PluginManager, segui o caminho mais simples: Primeiro procuro na directoria da aplicação por todas as dll&#8217;s que existam:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:62px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> List &lt;IPlugin&gt; result = <span style="color:#0000ff;">new</span> List&lt;IPlugin&gt;();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> DirectoryInfo directoryInfo = <span style="color:#0000ff;">new</span> DirectoryInfo(Application.StartupPath);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> FileInfo[] files = directoryInfo.GetFiles(<span style="color:#006080;">"*.dll"</span>);</pre>
</div>
</div>
<p>De seguida, em cada ficheiro, procuro por classes que implementem a interface IPlugin:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:223px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">foreach</span> (FileInfo file <span style="color:#0000ff;">in</span> files)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     Assembly assembly = Assembly.LoadFile(file.FullName);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">foreach</span> (Type type <span style="color:#0000ff;">in</span> assembly.GetTypes())</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>         Type[] interfaces = type.GetInterfaces();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span>         <span style="color:#0000ff;">foreach</span> (Type interfaceType <span style="color:#0000ff;">in</span> interfaces)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span>         {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>             <span style="color:#0000ff;">if</span> (interfaceType == <span style="color:#0000ff;">typeof</span>(IPlugin))</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>             {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span>                 ...</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span>             }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  13:</span>         }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  14:</span>     }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  15:</span> }</pre>
</div>
</div>
<p>Basta depois criar instâncias destas classes, retornando-as no final.</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">try</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">if</span> (constructor != <span style="color:#0000ff;">null</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>         result.Add((IPlugin) constructor.Invoke(<span style="color:#0000ff;">new</span> <span style="color:#0000ff;">object</span>[0]));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span> }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> <span style="color:#0000ff;">catch</span> (Exception)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   8:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   9:</span>     <span style="color:#008000;">//Could not load plugin</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  10:</span>     <span style="color:#008000;">//TODO: report back result of process.</span></pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  11:</span> }</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">  12:</span> <span style="color:#0000ff;">break</span>;</pre>
</div>
</div>
<p>Aqui, após procurar o construtor sem argumentos, o único cuidado a ter é, ao invocar o construtor, apanhar todas as excepções que possam ocorrer. Isto, na verdade, deverá ser um cuidado a ter sempre que no código vá invocar algo num plugin, pois temos de assumir que, eventualmente, algum plugin irá estar menos bem implementado, prevenindo assim possíveis problemas na aplicação.</p>
<p>&nbsp;</p>
<p>Para já as excepções encontradas ao invocar os plugins são ignoradas mas, após a implementação do host/controller da aplicação, vamos arranjar uma forma de apresentar feedback ao utilizador sobre as operações dos plugins. </p>
<p>Já agora, se tiverem a usar a framework 3.5, vejam antes <a href="http://msdn.microsoft.com/en-gb/magazine/cc700355.aspx">aqui</a> como initializar os plugins usando o novo namespace System.AddIn. Reduz-se esta classe a 3 linhas de código!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/140/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/140/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=140&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/09/plugins-pluginmanager/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>
	</item>
		<item>
		<title>log4net &#8211; Event Viewer</title>
		<link>http://caxaria.org/2008/07/08/log4net-event-viewer/</link>
		<comments>http://caxaria.org/2008/07/08/log4net-event-viewer/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 12:48:05 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/08/log4net-event-viewer/</guid>
		<description><![CDATA[Para o projecto em que estou envolvido desde Janeiro, desde cedo fiz questão que fossem usados muitos processos relacionados com o desenvolvimento para, no final, facilitar o processo de manutenção. No top 3 destes processos estão, sem qualquer sombra de dúvida: Uso de source control (SVN) Integração contínua Log4net Desde que usei o log4net na [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=139&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p align="justify">Para o projecto em que estou envolvido desde Janeiro, desde cedo fiz questão que fossem usados muitos processos relacionados com o desenvolvimento para, no final, facilitar o processo de manutenção.</p>
<p align="justify">No top 3 destes processos estão, sem qualquer sombra de dúvida:</p>
<ul>
<li>
<div align="justify">Uso de source control (SVN)</div>
<li>
<div align="justify">Integração contínua</div>
<li>
<div align="justify">Log4net</div>
</li>
</ul>
<p align="justify">Desde que usei o log4net na <a href="http://www.inosat.com/index_home.php">Inosat</a> que acho que é uma ferramenta indispensável para a grande maioria* dos programadores. Desde que a aplicação foi instalada nos servidores que, invariavelmente, todos os problemas podem ser identificados usando os logs para verificar o que se passou.</p>
<p><span id="more-139"></span>
<p align="justify"></p>
<p align="justify">Do <a href="http://logging.apache.org/log4net/">site</a>:</p>
<blockquote><p align="justify">log4net is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4net it is possible to enable logging at runtime without modifying the application binary.</p>
</blockquote>
<p align="justify">Para além de ter o log básico para ficheiro, pode ser usado para fazer log para qualquer outro output imaginável, bastando para isso alterar o ficheiro de configuração da aplicação/serviço/website. Pode-se, por exemplo, mandar todos os logs de determinado nível &#8211; ex Error &#8211; por email, UDP, Event Viewer, etc. </p>
<p align="center"><a href="http://caxaria.files.wordpress.com/2008/07/event-log.jpg"><img style="border-width:0;" height="165" alt="EDMS Event log" src="http://caxaria.files.wordpress.com/2008/07/event-log-thumb.jpg?w=283&#038;h=165" width="283" border="0"></a> </p>
<p align="justify">No caso do projecto em que estou envolvido a <a href="http://bt.com/">BT</a> pediu que o log fosse colocado no Event Viewer do windows para que eles pudessem monitorizar de forma automatizada. Um pedido destes, numa altura em que nos estávamos a preparar para iniciar a fase de testes <em>pré-live</em>, seria algo que nos obrigaria a mudar bastante o código para acomodar o novo tipo de logs. Felizmente, usando o log4net, bastou mudar o ficheiro de configuração**.</p>
<p align="justify">&nbsp;</p>
<p align="justify"><font color="#808080" size="2">*Digo a grande maioria pois não quero generalizar mas&#8230; vá lá, 99,99%</font></p>
<p align="justify"><font color="#808080" size="2">**Bastou.. quer dizer&#8230; No principio não funcionou pois websites e services não têm permissão para criar <em>Queues</em> no <em>&#8216;Event Viewer&#8217;. </em>Para este problema, basta inicializar a <em>Queue </em>numa &#8216;custom action&#8217; do instalador do site/serviço. A partir daí, visto que para escrever para o &#8216;Event Viewer&#8217; não são precisas permissões especiais, tudo funcionou até nos servidores super-restritos da BT.</font></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/139/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/139/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/139/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/139/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/139/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=139&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/08/log4net-event-viewer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>

		<media:content url="http://caxaria.files.wordpress.com/2008/07/event-log-thumb.jpg" medium="image">
			<media:title type="html">EDMS Event log</media:title>
		</media:content>
	</item>
		<item>
		<title>Plugins: O inicio</title>
		<link>http://caxaria.org/2008/07/07/plugins-o-inicio/</link>
		<comments>http://caxaria.org/2008/07/07/plugins-o-inicio/#comments</comments>
		<pubDate>Mon, 07 Jul 2008 14:27:05 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/07/plugins-o-inicio/</guid>
		<description><![CDATA[Ultimamente tenho andado a trabalhar num pequeno projecto pessoal que entretanto decide partilhar. O objectivo deste projecto é fazer uma aplicação semelhante ao Outlook em termos de interface mas, em vez de tratar de emails, esta aplicação seria direccionada para&#8230; qualquer coisa. Este qualquer coisa não está aqui apenas porque não decidi sobre o que [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=136&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Ultimamente tenho andado a trabalhar num pequeno projecto pessoal que entretanto decide partilhar. </p>
<p>O objectivo deste projecto é fazer uma aplicação semelhante ao Outlook em termos de interface mas, em vez de tratar de emails, esta aplicação seria direccionada para&#8230; <em>qualquer coisa</em>.</p>
<p align="center"><a href="http://caxaria.files.wordpress.com/2008/07/base-ui.jpg"><img style="border-right:0;border-top:0;border-left:0;border-bottom:0;" height="187" alt="base_ui" src="http://caxaria.files.wordpress.com/2008/07/base-ui-thumb.jpg?w=244&#038;h=187" width="244" border="0"></a></p>
<p><span id="more-136"></span>
<p align="center"></p>
<p>Este <em>qualquer coisa</em> não está aqui apenas porque não decidi sobre o que quero fazer a aplicação mas sim porque quero que seja possível que se consiga alterar completamente o alvo da aplicação, bastando para isso alterar os plugins registados na aplicação. </p>
<p>Se, por exemplo, eu fizer um plugin para gerir a minha colecção de DVDs&#8217;, nada impede que alguém faça um plugin para gerir a sua biblioteca de livros. Hey, e porque não fazer um plugin para gerir emails? E porque não ter isto tudo na mesma aplicação, com os plugins a comunicarem uns com os outros?</p>
<p>Aqui não há nada certo, apenas que a interface respeita a barra da esquerda &#8211; tal como o outlook -&nbsp; e que os documentos &#8211; sejam eles o que forem, aparecem à direita. Os plugins, no final, poderão interagir uns com os outros, não havendo qualquer restrição ao que será possível fazer com eles.</p>
<p>Assim, a primeira coisa que fiz foi a interface. Como podem ver na imagem, o nosso form principal é bastante&#8230; err, simples!</p>
<p>Tenho um <em>&#8216;SplitContainer&#8217;</em> a separar as duas grandes áreas da interface e, à esquerda, um painel que irá ser semelhante &#8211; quando preenchido com alguma coisa &#8211; ao oferecido pelo outlook.</p>
<p>Usando os excelentes controlos <a href="http://www.componentfactory.com/">Krypton Toolkit</a>, ainda não fiz código nenhum&#8230; mas infelizmente a aplicação ainda não faz nada!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/136/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/136/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=136&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/07/plugins-o-inicio/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>

		<media:content url="http://caxaria.files.wordpress.com/2008/07/base-ui-thumb.jpg" medium="image">
			<media:title type="html">base_ui</media:title>
		</media:content>
	</item>
		<item>
		<title>Obter a parte decimal em C#</title>
		<link>http://caxaria.org/2008/07/04/obter-a-parte-decimal-em-c/</link>
		<comments>http://caxaria.org/2008/07/04/obter-a-parte-decimal-em-c/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 14:30:35 +0000</pubDate>
		<dc:creator>caxaria</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Programação]]></category>

		<guid isPermaLink="false">http://caxaria.wordpress.com/2008/07/04/obter-a-parte-decimal-em-c/</guid>
		<description><![CDATA[Ao ler o blog do Tiago vi uma solução que ele colocou para encontrar a parte decimal de um número usando C#. À primeira vista pareceu-me que a solução dele era um pouco ingénua: 1: public static int GetDecimalPart(double value) 2: { 3: int number = (int) value; 4: string numbString = number.ToString(); 5: int [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=132&subd=caxaria&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>Ao ler o blog do Tiago vi uma <a href="http://sousatiago.blogspot.com/2008/07/obter-parte-decimal-em-c.html">solução</a> que ele colocou para encontrar a parte decimal de um número usando C#. </p>
<p>À primeira vista pareceu-me que a solução dele era um pouco ingénua:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:132px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">int</span> GetDecimalPart(<span style="color:#0000ff;">double</span> <span style="color:#0000ff;">value</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">int</span> number = (<span style="color:#0000ff;">int</span>) <span style="color:#0000ff;">value</span>;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     <span style="color:#0000ff;">string</span> numbString = number.ToString();</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span>     <span style="color:#0000ff;">int</span> stringLength = numbString.Length;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   6:</span>     <span style="color:#0000ff;">return</span> Int32.Parse(<span style="color:#0000ff;">value</span>.ToString().Substring(stringLength + 1));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   7:</span> }</pre>
</div>
</div>
<p>Quer dizer, usar strings e Lenght&#8217;s? :S Não me parece que seja bom&#8230; Então decidi tentar fazer melhor &#8230; Esta foi a minha primeira iteração:</p>
<p><span id="more-132"></span></p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:87px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">double</span> GetDecimalPart2(<span style="color:#0000ff;">double</span> <span style="color:#0000ff;">value</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">return</span> <span style="color:#0000ff;">value</span> % 1;</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> }</pre>
</div>
</div>
<p>Ah! Genial, não é? Claro que já tinha um teste unitário à espera:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> CheckSimpleSolution()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     Assert.AreEqual(0.02, DecimalTools.GetDecimalPart2(1.02));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
</div>
</div>
<p>Infelizmente o resultado não é o esperado:</p>
<p align="center"><a href="http://caxaria.files.wordpress.com/2008/07/firsttest.jpg"><img style="border-width:0;" height="87" alt="firsttest" src="http://caxaria.files.wordpress.com/2008/07/firsttest-thumb.jpg?w=443&#038;h=87" width="443" border="0"></a> </p>
<p align="justify">Mas OK. Aqui o problema é que o double, que estamos a usar no GetDecimalPart2 não tem precisão suficiente para estas operações&#8230; Basta então tentar assim:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:81px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">double</span> GetDecimalPart2(<span style="color:#0000ff;">double</span> <span style="color:#0000ff;">value</span>)</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span>     <span style="color:#0000ff;">return</span> (<span style="color:#0000ff;">double</span>)(((<span style="color:#0000ff;">decimal</span>)<span style="color:#0000ff;">value</span>) % 1);</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span> }</pre>
</div>
</div>
<p align="justify">Basta converter o valor para um decimal que, tendo maior precisão, não perde valores na cálculo do &#8216;% 1&#8242;. Como o número original com que estou a trabalhar é um double no final posso voltar a converter para double, sem ter receio de encontrar uma excepção. Correndo o teste:</p>
<p align="center"><a href="http://caxaria.files.wordpress.com/2008/07/test2.jpg"><img style="border-width:0;" height="55" alt="test2" src="http://caxaria.files.wordpress.com/2008/07/test2-thumb.jpg?w=444&#038;h=55" width="444" border="0"></a> </p>
<p align="justify">Excelente&#8230; Mas&#8230; vendo o código original&#8230; o retorno é um inteiro! Isso quer dizer que o valor de retorno de um argumento de 1.02 seria 2, em vez de 0.02. Mas isso também seria o resultado de 1.2&#8230; Tenho de ver:</p>
<div style="border-right:gray 1px solid;border-top:gray 1px solid;font-size:8pt;overflow:auto;border-left:gray 1px solid;width:97.5%;cursor:text;max-height:200px;line-height:12pt;border-bottom:gray 1px solid;font-family:consolas, 'Courier New', courier, monospace;height:103px;background-color:#f4f4f4;margin:20px 0 10px;padding:4px;">
<div style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;padding:0;">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   1:</span> [Test]</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   2:</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span> CheckOriginalForDuplicateResults()</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   3:</span> {</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   4:</span>     Assert.AreNotEqual(DecimalTools.GetDecimalPart(1.2), DecimalTools.GetDecimalPart(1.02));</pre>
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;line-height:12pt;font-family:consolas, 'Courier New', courier, monospace;background-color:#f4f4f4;border-style:none;margin:0;padding:0;"><span style="color:#606060;">   5:</span> }</pre>
</div>
</div>
<p align="center"><a href="http://caxaria.files.wordpress.com/2008/07/test3.jpg"><img style="border-width:0;" height="46" alt="test3" src="http://caxaria.files.wordpress.com/2008/07/test3-thumb.jpg?w=444&#038;h=46" width="444" border="0"></a> </p>
<p align="justify">Ouch&#8230; Ao que parece, enganei-me nos requisitos&#8230; Pela implementação do Tiago, vejo que ele quer algo completamente diferente. Vejam a seguinte tabela:</p>
<div align="center">
<table cellspacing="0" cellpadding="2" width="400" align="center" border="1">
<tbody>
<tr>
<td valign="top" width="127"><strong>Valor Original</strong></td>
<td valign="top" width="135"><strong>GetDecimalPart</strong></td>
<td valign="top" width="136"><strong>GetDecimalPart2</strong></td>
</tr>
<tr>
<td valign="top" width="124">1.02</td>
<td valign="top" width="136">2</td>
<td valign="top" width="138">0.02</td>
</tr>
<tr>
<td valign="top" width="123">1.2</td>
<td valign="top" width="136">2</td>
<td valign="top" width="139">0.2</td>
</tr>
<tr>
<td valign="top" width="122">1</td>
<td valign="top" width="137">0</td>
<td valign="top" width="141">0.0</td>
</tr>
</tbody>
</table>
</div>
<p align="justify">&nbsp;</p>
<p align="justify">Apesar do que se pretendido no GetDecimalPart não me fazer sentido, se isso é mesmo o desejado, não me lembro de nenhuma implementação que seja melhor que a do Tiago&#8230;</p>
<p>Agora acho que deviam ir ao <a href="http://sousatiago.blogspot.com/2008/07/obter-parte-decimal-em-c.html">site dele</a> dizer quanto admiram o código dele e deixarem aqui uns comentários para me dizerem o quanto me odeiam por vos ter feito perder este tempo todo <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/caxaria.wordpress.com/132/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/caxaria.wordpress.com/132/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/caxaria.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/caxaria.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/caxaria.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/caxaria.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/caxaria.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/caxaria.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/caxaria.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/caxaria.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/caxaria.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/caxaria.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=caxaria.org&blog=1600018&post=132&subd=caxaria&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://caxaria.org/2008/07/04/obter-a-parte-decimal-em-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/40405d52c92e546d32046560f4e5c40a?s=96&#38;d=wavatar&#38;r=G" medium="image">
			<media:title type="html">caxaria</media:title>
		</media:content>

		<media:content url="http://caxaria.files.wordpress.com/2008/07/firsttest-thumb.jpg" medium="image">
			<media:title type="html">firsttest</media:title>
		</media:content>

		<media:content url="http://caxaria.files.wordpress.com/2008/07/test2-thumb.jpg" medium="image">
			<media:title type="html">test2</media:title>
		</media:content>

		<media:content url="http://caxaria.files.wordpress.com/2008/07/test3-thumb.jpg" medium="image">
			<media:title type="html">test3</media:title>
		</media:content>
	</item>
	</channel>
</rss>