<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C# on toeb / notes</title><link>https://toeb.github.io/tags/c%23/</link><description>Recent content in C# on toeb / notes</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Mon, 09 Dec 2013 00:00:00 +0000</lastBuildDate><atom:link href="https://toeb.github.io/tags/c%23/index.xml" rel="self" type="application/rss+xml"/><item><title>A Fluent Way of Exception Handling</title><link>https://toeb.github.io/post/csharpfluentexceptionhandling/</link><pubDate>Mon, 09 Dec 2013 00:00:00 +0000</pubDate><guid>https://toeb.github.io/post/csharpfluentexceptionhandling/</guid><description>&lt;p&gt;While doing some filesystem work I encountered alot of AccessViolationExceptions because a file is being opened to write multiple times. Even after closing the file explicitly a short timeframe exists in which the file could not be opened again. The simplest approach for me was to retry openening the file until it worked. To allow a more easy and more general way to handle exceptions I developed a small extensible try-repeat-framework which allows me to configure waiting and repeating strategies and also to only react to specific exceptions. I have written it with a fluent interface which allows easy usage:&lt;/p&gt;</description></item><item><title>Parsing Web Asset Files</title><link>https://toeb.github.io/post/csharpparsingwebassetfiles/</link><pubDate>Wed, 04 Dec 2013 00:00:00 +0000</pubDate><guid>https://toeb.github.io/post/csharpparsingwebassetfiles/</guid><description>&lt;p&gt;To parse versioned web asset filenames ala&#10;jquery-1.4.min.js&#10;I wrote a simple class&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;/// &amp;lt;summary&amp;gt;&#10;/// simple class for describing a file library like &#10;/// jquery-1.4.min.js&#10;/// bootstrap.min.css&#10;/// libname-1.0.0.0.tag1.tag2.tag3.extension&#10;/// ... etc&#10;/// contains methods for parsing these kind of filenames&#10;/// &amp;lt;/summary&amp;gt;&#10;public class FileLib&#10;{&#10; /// &amp;lt;summary&amp;gt;&#10; /// regular expression for parsing the filename&#10; /// &amp;lt;/summary&amp;gt;&#10; public static readonly string libRegex = @&amp;#34;((?&amp;lt;libname&amp;gt;[^.-]*)-?)((?&amp;lt;major&amp;gt;d+))?(.(?&amp;lt;minor&amp;gt;d+)(.(?&amp;lt;revision&amp;gt;d+)(.(?&amp;lt;build&amp;gt;d+))?)?)?(.(?&amp;lt;tags&amp;gt;.+))*&amp;#34;;&#10; /// &amp;lt;summary&amp;gt;&#10; /// parses a path as a lib&#10; /// &amp;lt;/summary&amp;gt;&#10; /// &amp;lt;param name=&amp;#34;path&amp;#34;&amp;gt;&amp;lt;/param&amp;gt;&#10; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&#10; public static FileLib ParsePath(string path)&#10; {&#10; var filename = Path.GetFileName(path);&#10; return Parse(filename);&#10; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// parses a filename as a file lib &#10; /// &amp;lt;/summary&amp;gt;&#10; /// &amp;lt;param name=&amp;#34;filename&amp;#34;&amp;gt;&amp;lt;/param&amp;gt;&#10; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&#10; public static FileLib Parse(string filename)&#10; {&#10; var match = Regex.Match(filename, libRegex);&#10; match.NextMatch();&#10; var lib = match.Groups[&amp;#34;libname&amp;#34;].Value ?? &amp;#34;&amp;#34;;&#10; var major = match.Groups[&amp;#34;major&amp;#34;].Value ?? &amp;#34;&amp;#34;;&#10; var minor = match.Groups[&amp;#34;minor&amp;#34;].Value ?? &amp;#34;&amp;#34;;&#10; var revision = match.Groups[&amp;#34;revision&amp;#34;].Value ?? &amp;#34;&amp;#34;;&#10; var build = match.Groups[&amp;#34;build&amp;#34;].Value ?? &amp;#34;&amp;#34;;&#10; var tags = match.Groups[&amp;#34;tags&amp;#34;].Value.Split(&amp;#39;.&amp;#39;);&#10; var extension = tags.LastOrDefault() ?? &amp;#34;&amp;#34;;&#10; tags = tags.Reverse().Skip(1).Reverse().ToArray();&#10; var result = new FileLib()&#10; {&#10; Extension = extension,&#10; LibName = lib,&#10; Major = major,&#10; Minor = minor,&#10; Revision = revision,&#10; Build = build,&#10; Tags = tags&#10;&#10; };&#10; return result;&#10; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// Major version number&#10; /// &amp;lt;/summary&amp;gt;&#10; public string Major { get; set; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// Minor version number&#10; /// &amp;lt;/summary&amp;gt;&#10; public string Minor { get; set; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// Revision Number&#10; /// &amp;lt;/summary&amp;gt;&#10; public string Revision { get; set; }&#10;&#10; /// &amp;lt;summary&amp;gt;&#10; /// Build number&#10; /// &amp;lt;/summary&amp;gt;&#10; public string Build { get; set; }&#10;&#10; /// &amp;lt;summary&amp;gt;&#10; /// Creates a version object for the Filelib. returns null if this is not possible&#10; /// &amp;lt;/summary&amp;gt;&#10; /// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&#10; public Version GetVersion()&#10; {&#10;&#10; Version parsedVersion = null;&#10; try&#10; {&#10; var version = Major + &amp;#34;.&amp;#34; + Minor + &amp;#34;.&amp;#34; + Revision + &amp;#34;.&amp;#34; + Build;&#10; while (version.EndsWith(&amp;#34;.&amp;#34;)) version = version.Substring(0, version.Length - 1);&#10; if (version == &amp;#34;&amp;#34;) return parsedVersion;&#10; parsedVersion = new Version(version);&#10; }&#10; catch (Exception exception) { }&#10; return parsedVersion;&#10;&#10; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// the name of the lib&#10; /// &amp;lt;/summary&amp;gt;&#10; public string LibName { get; set; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// the tags of the lib. e.g. min, intellisense, etc&#10; /// &amp;lt;/summary&amp;gt;&#10; public IEnumerable&amp;lt;string&amp;gt; Tags { get; set; }&#10; /// &amp;lt;summary&amp;gt;&#10; /// the extension of the lib&#10; /// &amp;lt;/summary&amp;gt;&#10; public string Extension { get; set; }&#10;}&#10;&lt;/code&gt;&lt;/pre&gt;</description></item></channel></rss>