<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7512142430065227563</id><updated>2011-07-08T06:07:47.923-07:00</updated><title type='text'>FxCritic</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-5664531807741519858</id><published>2011-03-31T19:39:00.000-07:00</published><updated>2011-03-31T19:39:46.355-07:00</updated><title type='text'>SVG XmlSerializer Classes</title><content type='html'>I've been struggling lately with getting a parser of the &lt;a href="http://en.wikipedia.org/wiki/Scalable_Vector_Graphics"&gt;SVG&lt;/a&gt; file format working in .NET C#. Scavenging the web in search of one has turned out that the few that do exist are often coupled to vector graphics rendering libraries and/or are often incomplete.Some have turned to writing their own simple SVG parsers, usually just for the support of paths, which was not enough for my purposes.&lt;br /&gt;&lt;br /&gt;Thinking that SVG should be a simple enough file format to handle, being XML-based, I tried to automatically generate classes for it using the XSD tool.It didn't work right out the bat because of some ill-defined attributes in the &lt;a href="http://www.w3.org/TR/2002/WD-SVG11-20020108/SVG.xsd"&gt;SVG.xsd&lt;/a&gt; file, but I finally managed to work around it and generate class files that XmlSerializer can read and write, and even tested them successfully with an Inkscape file.&lt;br /&gt;&lt;br /&gt;Without further ado, you can find the source &lt;a href="http://www.box.net/shared/l7ce0hlcij"&gt;here&lt;/a&gt;. Feel free to use it for whatever you feel like and to comment if any questions pop out.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-5664531807741519858?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/5664531807741519858/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=5664531807741519858' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/5664531807741519858'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/5664531807741519858'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2011/03/svg-xmlserializer-classes.html' title='SVG XmlSerializer Classes'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-7012518503741262407</id><published>2009-11-11T12:24:00.000-08:00</published><updated>2009-11-11T14:16:10.776-08:00</updated><title type='text'>Ramblings on the Command design pattern</title><content type='html'>I was recently in need of implementing a mechanism for Undo/Redo for a GUI and was led to reminisce on the good old Command design pattern. Basically, I led myself to imagine what was the cleanest and most minimalist way of implementing the pattern in the .NET framework, specifically taking advantage of the C# 3.0 approach to functional programming.&lt;br /&gt;&lt;br /&gt;It turns out implementing a generic Command pattern in C# 3.0 is so easy and powerful, via a clever use of delegates, that even the need for some of the classes specified in the original pattern is virtually eliminated.&lt;br /&gt;&lt;br /&gt;Here's the implementation of a CommandExecutor class, which single-handedly deals with all the reusable aspects of the pattern:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: c-sharp" type="syntaxhighlighter"&gt;public class CommandExecutor&lt;br /&gt;{&lt;br /&gt;  private int currentCommand = -1;&lt;br /&gt;  private readonly List&amp;lt;Command&amp;gt; history = new List&amp;lt;Command&amp;gt;();&lt;br /&gt;&lt;br /&gt;  public event EventHandler StatusChanged;&lt;br /&gt;&lt;br /&gt;  public bool CanUndo&lt;br /&gt;  {&lt;br /&gt;    get { return currentCommand &amp;gt;= 0; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public bool CanRedo&lt;br /&gt;  {&lt;br /&gt;    get { return currentCommand &amp;lt; history.Count - 1; }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void Execute(Action command, Action undo)&lt;br /&gt;  {&lt;br /&gt;    if (command == null)&lt;br /&gt;    {&lt;br /&gt;      throw new ArgumentNullException("command");&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    command();&lt;br /&gt;    if (undo != null)&lt;br /&gt;    {&lt;br /&gt;      history.RemoveRange(&lt;br /&gt;        ++currentCommand,&lt;br /&gt;        history.Count - currentCommand&lt;br /&gt;      );&lt;br /&gt;      history.Add(new Command(command, undo));&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;      history.Clear();&lt;br /&gt;      currentCommand = -1;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    OnStatusChanged(EventArgs.Empty);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void Undo()&lt;br /&gt;  {&lt;br /&gt;    if (CanUndo)&lt;br /&gt;    {&lt;br /&gt;      history[currentCommand--].Undo();&lt;br /&gt;      OnStatusChanged(EventArgs.Empty);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public void Redo()&lt;br /&gt;  {&lt;br /&gt;    if (CanRedo)&lt;br /&gt;    {&lt;br /&gt;      history[++currentCommand].Execute();&lt;br /&gt;      OnStatusChanged(EventArgs.Empty);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  protected virtual void OnStatusChanged(EventArgs e)&lt;br /&gt;  {&lt;br /&gt;    var handler = StatusChanged;&lt;br /&gt;    if (handler != null)&lt;br /&gt;    {&lt;br /&gt;      handler(this, e);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  private class Command&lt;br /&gt;  {&lt;br /&gt;    private readonly Action execute;&lt;br /&gt;    private readonly Action undo;&lt;br /&gt;&lt;br /&gt;    public Command(Action execute, Action undo)&lt;br /&gt;    {&lt;br /&gt;      this.execute = execute;&lt;br /&gt;      this.undo = undo;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public Action Execute&lt;br /&gt;    {&lt;br /&gt;      get { return this.execute; }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public Action Undo&lt;br /&gt;    {&lt;br /&gt;      get { return this.undo; }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size: 100%;"&gt;With this functor based implementation, commands can be easily passed into the executor along with the undo method, either as references to existing methods, or as lambda expressions.&lt;br /&gt;&lt;br /&gt;Using the delegate indirection, this type of implementation conveniently eliminates the need for implementing a common Command interface, which makes it that much more reusable, as you can easily pass in methods of classes which you did not implement, or very easily and concisely transform specific method calls into commands without the need to implement yet another class.&lt;br /&gt;&lt;br /&gt;Also, using the scoping semantics of anonymous delegates, you can easily use closures as data members for sharing immutable data between the execution of the command and the undo operation. For instance, consider the following example:&lt;/span&gt;&lt;span style="font-size: 85%;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre class="brush: c-sharp" type="syntaxhighlighter"&gt;&lt;br /&gt;public class Test&lt;br /&gt;{&lt;br /&gt;  private readonly CommandExecutor executor = new CommandExecutor();&lt;br /&gt;&lt;br /&gt;  public void TestScoping()&lt;br /&gt;  {&lt;br /&gt;    int sum = 0;&lt;br /&gt;&lt;br /&gt;    for (int i = 0; i &amp;lt; 10; ++i)&lt;br /&gt;    {&lt;br /&gt;      var scopedData = i;&lt;br /&gt;      executor.Execute(&lt;br /&gt;        () =&amp;gt; sum += scopedData,&lt;br /&gt;        () =&amp;gt; sum -= scopedData&lt;br /&gt;      );&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    for (int i = 0; i &amp;lt; 10; ++i)&lt;br /&gt;    {&lt;br /&gt;      executor.Undo();&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;span style="font-size: 85%;"&gt;&lt;span style="font-size: 100%;"&gt;&lt;/span&gt;&lt;/span&gt;In this example, scopedData is used in the closure of the command delegates. Each iteration of the for-loop produces a different numeric value, which is implicitly preserved onto each command execution. By undoing the command, the sum gets preserved.&lt;br /&gt;&lt;br /&gt;If instead the for-loop counter variable was passed, the undo commands would not be adequately preserved, as that variable would have been shared between all closures of all commands. In this way, you can adequately choose the level of sharing between command executions and undo operations, and effectively use the principles of functional programming to insulate each command execution from side-effects, while at the same time writing clean, concise, and efficient code.&lt;span style="font-size: 85%;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-7012518503741262407?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/7012518503741262407/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=7012518503741262407' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/7012518503741262407'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/7012518503741262407'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2009/11/ramblings-on-command-design-pattern.html' title='Ramblings on the Command design pattern'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-7067898290704471452</id><published>2008-05-19T10:51:00.000-07:00</published><updated>2008-05-19T11:45:38.944-07:00</updated><title type='text'>Lightweight Fiber/Coroutines implementation on C#</title><content type='html'>I was recently experimenting with exploiting the C# 2.0 iterators to simulate continuations, as suggested in &lt;a href="http://wesnerm.blogs.com/net_undocumented/2004/08/iterators_not_j.html"&gt;Wesner Moise's blog&lt;/a&gt; and came up with what seems to be a general and reasonable way to implement fibers in .NET.&lt;br /&gt;&lt;br /&gt;To implement a logical execution thread we basically need a stack to keep track of "subroutines" and a "program counter" to keep track of the current execution point. Basically we take on the IEnumerator interface as the basic type for program counters.&lt;br /&gt;&lt;br /&gt;Simulating an execution is simply a matter of stepping through the current IEnumerator routine. Every time this enumerator yields a subroutine, the current routine is pushed onto the stack and the "program counter" switches to the new routine. Every time a routine terminates, the stack is tested for parent routines by popping().&lt;br /&gt;&lt;br /&gt;Here's the lightweight fiber implementation and a small sample of how to use it:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public &lt;/span&gt;class &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;Fiber&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;    private readonly &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;Stack&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;IEnumerator&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&gt; stackFrame =&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;        new &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;Stack&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;IEnumerator&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&gt;();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;private &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;IEnumerator &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;currentRoutine;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;public &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Fiber(&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;IEnumerator &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;entryPoint)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;this&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;.currentRoutine = entryPoint;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;public bool &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;Step()&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;if &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(currentRoutine.MoveNext())&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;var &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;subRoutine = currentRoutine.Current&lt;br /&gt;                           &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;as &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;IEnumerator&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;if &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(subRoutine != &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;null&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                stackFrame.Push(currentRoutine);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;                currentRoutine = subRoutine;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;else if &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(stackFrame.Count &gt; 0)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            currentRoutine = stackFrame.Pop();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;else&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;br /&gt;          OnFiberTerminated(&lt;span style="color: rgb(51, 51, 255);"&gt;&lt;br /&gt;              new &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;FiberTerminatedEventArgs&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(&lt;br /&gt;                  currentRoutine.Current&lt;br /&gt;              )&lt;br /&gt;          );&lt;br /&gt;          &lt;span style="color: rgb(51, 51, 255);"&gt;return false&lt;/span&gt;;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;return true&lt;/span&gt;;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;public event &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;EventHandler&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&lt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;FiberTerminatedEventArgs&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;&gt;&lt;br /&gt;       FiberTerminated;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;private void &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;OnFiberTerminated(&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;" &gt;&lt;br /&gt;       FiberTerminatedEventArgs &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;e)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;var &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;handler = FiberTerminated;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;if &lt;/span&gt;&lt;span style="font-family:courier new;"&gt;(handler != &lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;null&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        {&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;            handler(&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;" &gt;this&lt;/span&gt;&lt;span style="font-family:courier new;"&gt;, e);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;        }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;    }&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;public class &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;FiberTerminatedEventArgs&lt;/span&gt;&lt;br /&gt;  : &lt;span style="color: rgb(51, 102, 255);"&gt;EventArgs&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;private readonly object &lt;/span&gt;result;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;public &lt;/span&gt;FiberTerminatedEventArgs(&lt;span style="color: rgb(51, 51, 255);"&gt;object &lt;/span&gt;result)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;this&lt;/span&gt;.result = result;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;public object &lt;/span&gt;Result&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;get &lt;/span&gt;{ &lt;span style="color: rgb(51, 51, 255);"&gt;return this&lt;/span&gt;.result; }&lt;br /&gt;  }&lt;br /&gt;}&lt;/span&gt;&lt;/pre&gt;Now to implement a small recursive test program:&lt;br /&gt;&lt;pre&gt;&lt;span style="color: rgb(51, 51, 255);"&gt;class &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;FiberTest&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;private static &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;IEnumerator &lt;/span&gt;Recurse(&lt;span style="color: rgb(51, 51, 255);"&gt;int &lt;/span&gt;n)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(51, 102, 255);"&gt;Console&lt;/span&gt;.WriteLine(n);&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;yield return &lt;/span&gt;n;&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;if &lt;/span&gt;(n &gt; 0)&lt;br /&gt;      {&lt;br /&gt;          &lt;span style="color: rgb(51, 51, 255);"&gt;yield return &lt;/span&gt;Recurse(n - 1);&lt;br /&gt;      }&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(51, 51, 255);"&gt;static void &lt;/span&gt;Main(&lt;span style="color: rgb(51, 51, 255);"&gt;string&lt;/span&gt;[] args)&lt;br /&gt;  {&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;var &lt;/span&gt;fiber = &lt;span style="color: rgb(51, 51, 255);"&gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;Fiber&lt;/span&gt;(Recurse(5));&lt;br /&gt;      &lt;span style="color: rgb(51, 51, 255);"&gt;while &lt;/span&gt;(fiber.Step()) ;&lt;br /&gt;  }&lt;br /&gt;}&lt;/pre&gt;&lt;span style="font-family:georgia;"&gt;This will print all the numbers from 5 to 0 by using fiber recursion. By yielding an IEnumerator, the fiber recursive function pushes the current fiber execution down to the recursive call. &lt;/span&gt;&lt;span style="font-family:georgia;"&gt;Test it yourself on the debugger and step through the execution to understand the basic mechanism.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;With such a fiber/coroutine methodology its possible to define logical execution threads controlled by your application in which  all involved iterator routines can be arbitrarily interrupted and resumed while preserving context.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family:georgia;"&gt;Cool stuff once it clicks on you. Nice going for C# 2.0 iterators.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-7067898290704471452?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/7067898290704471452/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=7067898290704471452' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/7067898290704471452'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/7067898290704471452'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2008/05/lightweight-fibercoroutines.html' title='Lightweight Fiber/Coroutines implementation on C#'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-1713803417009710896</id><published>2008-05-09T02:18:00.000-07:00</published><updated>2008-05-09T07:02:05.745-07:00</updated><title type='text'>Immutability and the good old 'const' keyword</title><content type='html'>With all the recent interest on C# functional programming and language integration of related concepts, one of the more interesting ideas is that of expressing immutability of an object.&lt;br /&gt;&lt;br /&gt;An object is immutable if no methods beside the constructor can modify its internal state. Automatic verification of immutability is far from trivial, so many have been asking about how one could specify that an object is immutable at compile-time.&lt;br /&gt;&lt;br /&gt;This would be interesting in multithreaded functional algorithm implementation, since we could potentially disregard parallelism concerns during function composing. All the shared data between functions was carefully protected simply by the fact that the data itself is immutable.&lt;br /&gt;&lt;br /&gt;A couple of days ago I led myself to think: "Wait, isn't this what the &lt;span style="font-style: italic; font-weight: bold;"&gt;const&lt;/span&gt; keyword in C++ was all about?". In fact, it's interesting how easily one could verify in C++ that an object was indeed immutable. Simply declare a const reference to an object and you only have access to const methods, which are methods guaranteed by the programmer that they won't change the object's internal state.&lt;br /&gt;&lt;br /&gt;C# effectively abolished lots of extraneous keywords, in a quest to end unnecessary verbosity and functionality perhaps, but could it be that we're on the verge of witnessing a comeback of the so-called 'const programming'? No doubt it is an interesting way of solving the problem AND making sure that everything's verified at compile time. But it definitely increases the complexity in programming.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-1713803417009710896?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/1713803417009710896/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=1713803417009710896' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/1713803417009710896'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/1713803417009710896'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2008/05/immutability-and-goold-old-const.html' title='Immutability and the good old &apos;const&apos; keyword'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-4272616813676087656</id><published>2008-04-18T01:40:00.001-07:00</published><updated>2008-04-18T02:03:19.469-07:00</updated><title type='text'>NSvn Handling of Repository Externals Property Text</title><content type='html'>This first post does not entirely relate to the .NET framework itself, but rather to a useful assembly implemented by the &lt;a href="http://ankhsvn.open.collab.net/"&gt;Ankh SVN&lt;/a&gt; project: NSvn.&lt;br /&gt;&lt;br /&gt;NSvn is a managed assembly to programmatically call &lt;a href="http://subversion.tigris.org/"&gt;Subversion&lt;/a&gt; commands like Update, Commit and others. It's a rather straightforward, and usable, library. However, there are some strange behaviors regarding some of its innards which can lead to some rather obscure bugs.&lt;br /&gt;&lt;br /&gt;One of the latest I encountered was the handling of the svn:externals property in code. Here's a small annotated snippet of the endeavour:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Client &lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;client = &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;font-size:85%;"  &gt;new &lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Client&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;();&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;client.Checkout(sourceSVN, projectName, &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Revision&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.Head, NSvn.Common.&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Recurse&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.Full);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Basically I instantiated a NSvn.Client instance and performed a checkout of a repository located in sourceSVN into the directory projectName.&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;PropertyDictionary &lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;dictionary = client.PropGet(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;font-size:85%;"  &gt;"svn:externals"&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;, projectExternalsDir, &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Revision&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.Head, &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Recurse&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.None);&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Property &lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;externalsProperty = dictionary[&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Path&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.Combine(sourceSVN, &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;font-size:85%;"  &gt;"Externals"&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;)];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;One can then proceed to access the svn:externals property via the PropGet method. This returns a PropertyDictionary which one can access. The Externals property itself has to be retrieved from the dictionary by specifying the full repository path.&lt;br /&gt;&lt;br /&gt;The problem began when we reached the point of actually modifying the value of the property, in particular, when adding a new external repository line:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;font-size:85%;"  &gt;string &lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;values = &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Encoding&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.UTF8.GetString(externalsProperty.Data);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;values += newExternal + &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;font-size:85%;"  &gt;'\n'&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;values += newExternal + &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;font-size:85%;"  &gt;'\n'&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;font-size:85%;"  &gt;byte&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;[] vals = &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Encoding&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.UTF8.GetBytes(values);&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:courier new;"&gt;client.PropSet(new &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Property&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;(externalsProperty.Name, vals), projectExternals, &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Recurse&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.None);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;By doing this, NSvn threw a SvnClientException indicating that there was an error parsing the property text.&lt;br /&gt;&lt;br /&gt;It turns out that each Externals line has to be separated by a '\n' (not a '\r\n'!), but it's absolutely vital that the last line does not have ANY separator whatsoever. Adding a TrimEnd call to the end of values does the trick:&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(51, 51, 255);font-family:courier new;font-size:85%;"  &gt;byte&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;[] vals = &lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(51, 102, 255);font-family:courier new;font-size:85%;"  &gt;Encoding&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;.UTF8.GetBytes(newValues.TrimEnd(&lt;/span&gt;&lt;/span&gt;&lt;span style="color: rgb(204, 0, 0);font-family:courier new;font-size:85%;"  &gt;'\n'&lt;/span&gt;&lt;span style="font-size:85%;"&gt;&lt;span style="font-family:courier new;"&gt;));&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Strange thing...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-4272616813676087656?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/4272616813676087656/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=4272616813676087656' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/4272616813676087656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/4272616813676087656'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2008/04/nsvn-handling-of-repository-externals.html' title='NSvn Handling of Repository Externals Property Text'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7512142430065227563.post-2887983963495713449</id><published>2008-04-09T04:38:00.000-07:00</published><updated>2008-04-09T04:41:44.174-07:00</updated><title type='text'>FxCritic launches</title><content type='html'>"&lt;span style="font-style: italic;"&gt;The word &lt;/span&gt;&lt;b style="font-style: italic;"&gt;critic&lt;/b&gt;&lt;span style="font-style: italic;"&gt; comes from the &lt;/span&gt;&lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Greek_language" title="Greek language"&gt;Greek&lt;/a&gt;&lt;span style="font-style: italic;"&gt; &lt;/span&gt;&lt;i style="font-style: italic;"&gt;&lt;span lang="el" lang="el"&gt;κριτικός&lt;/span&gt;, &lt;span lang="el-Latn" lang="el-Latn"&gt;kritikós&lt;/span&gt; - one who discerns&lt;/i&gt;&lt;span style="font-style: italic;"&gt;, which itself arises from the &lt;/span&gt;&lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Ancient_Greek" title="Ancient Greek"&gt;Ancient Greek&lt;/a&gt;&lt;span style="font-style: italic;"&gt; word &lt;/span&gt;&lt;i style="font-style: italic;"&gt;&lt;span lang="grc" lang="grc"&gt;κριτής&lt;/span&gt;, &lt;span lang="grc-Latn" lang="grc-Latn"&gt;krités&lt;/span&gt;&lt;/i&gt;&lt;span style="font-style: italic;"&gt;, meaning a person who offers reasoned &lt;/span&gt;&lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Judgment" class="mw-redirect" title="Judgment"&gt;judgment&lt;/a&gt;&lt;span style="font-style: italic;"&gt; or analysis, &lt;/span&gt;&lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Value_judgment" title="Value judgment"&gt;value judgment&lt;/a&gt;&lt;span style="font-style: italic;"&gt;, interpretation, or &lt;/span&gt;&lt;a style="font-style: italic;" href="http://en.wikipedia.org/wiki/Observation" title="Observation"&gt;observation&lt;/a&gt;&lt;span style="font-style: italic;"&gt;. The term can be used to describe an adherent of a position disagreeing with or opposing the object of criticism.&lt;/span&gt;" - &lt;span style="font-weight: bold;"&gt;Wikipedia&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;FxCritic is a blog dedicated to giving reasonable criticism of Microsoft's .NET framework and associated libraries and utilities. Expect commentaries about new functionalities, framework inconsistencies, bugs, weird tweakings, workarounds, interesting ways to achieve desired effects, etc.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7512142430065227563-2887983963495713449?l=fxcritic.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://fxcritic.blogspot.com/feeds/2887983963495713449/comments/default' title='Enviar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7512142430065227563&amp;postID=2887983963495713449' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/2887983963495713449'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7512142430065227563/posts/default/2887983963495713449'/><link rel='alternate' type='text/html' href='http://fxcritic.blogspot.com/2008/04/fxcritic-launches.html' title='FxCritic launches'/><author><name>Chance</name><uri>http://www.blogger.com/profile/09270175973015325944</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
