<?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/"
	>

<channel>
	<title>The Blog of Zachary Snow &#187; c#</title>
	<atom:link href="http://zacharysnow.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://zacharysnow.net</link>
	<description></description>
	<lastBuildDate>Tue, 20 Dec 2011 19:41:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WinQuakeCon</title>
		<link>http://zacharysnow.net/2011/12/20/winquakecon/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=winquakecon</link>
		<comments>http://zacharysnow.net/2011/12/20/winquakecon/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 19:34:10 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[WinQuakeCon]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[command-shell]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[quake]]></category>
		<category><![CDATA[terminal]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[winquakecon]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=471</guid>
		<description><![CDATA[I&#8217;ve always wanted a program that would allow me have a command line that I could slide in and out of view as needed like the console from Quake. I never could find one and so I finally got decided &#8230; <a href="http://zacharysnow.net/2011/12/20/winquakecon/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always wanted a program that would allow me have a command line that I could slide in and out of view as needed like the console from <a href="http://en.wikipedia.org/wiki/Quake_(video_game)">Quake</a>. I never could find one and so I finally got decided to write a tool myself. A picture is below and the source code is on <a href="https://github.com/smack0007/WinQuakeCon">GitHub</a>.</p>
<p>I&#8217;m not putting a download here as I don&#8217;t consider this finished just yet.</p>
<p><a href="http://zacharysnow.net/wp-content/uploads/2011/12/WinQuakeCon.png"><img src="http://zacharysnow.net/wp-content/uploads/2011/12/WinQuakeCon-300x160.png" alt="" title="WinQuakeCon" width="300" height="160" class="alignnone size-medium wp-image-475" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2011/12/20/winquakecon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Snowball and what do I want to do with it?</title>
		<link>http://zacharysnow.net/2011/10/16/what-is-snowball-and-what-do-i-want-to-do-with-it/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=what-is-snowball-and-what-do-i-want-to-do-with-it</link>
		<comments>http://zacharysnow.net/2011/10/16/what-is-snowball-and-what-do-i-want-to-do-with-it/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 18:30:47 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[SlimDX]]></category>
		<category><![CDATA[Snowball]]></category>
		<category><![CDATA[2D]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[codeplex]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[slimdx]]></category>
		<category><![CDATA[snowball]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=462</guid>
		<description><![CDATA[I originally got the idea for Snowball after working with the Xna Framework. The Xna Framework is a good piece of software for what it is but there are some things about which I just do not agree with: The &#8230; <a href="http://zacharysnow.net/2011/10/16/what-is-snowball-and-what-do-i-want-to-do-with-it/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I originally got the idea for Snowball after working with the Xna Framework. The Xna Framework is a good piece of software for what it is but there are some things about which I just do not agree with:</p>
<ul>
<li>The content pipeline only works with content in the serialized .xnb format.</li>
<li>There are certain content types which can only be loaded via the content pipeline.</li>
<li>Certain features don&#8217;t exist on the PC because they don&#8217;t exist on the XBox or Windows Phone 7.</li>
</ul>
<p>Xna was designed as an abstraction layer for all the 3 platforms mentioned in the last point, so that one is somewhat understandable. I don&#8217;t want to write games for my XBox right now though, so why should things like drawing lines not be available to me?</p>
<p>With these points in mind I started working on <a href="http://snowball.codeplex.com/">Snowball</a>. It&#8217;s designed to be an Xna like framework for making 2D games. It uses SlimDX on the backend, but that is completely abstracted away from consumers of the framework. What I want to do is design the API so that the backend can be swapped out <strong>somewhat</strong> painlessly.</p>
<p>I still have a ways to go before I will consider it a version 1.0 release. As of this writing, I&#8217;m transitioning to more of a ContentLoader class style for loading your game&#8217;s content. Any resource type from within the framework can be loaded by hand if you want, the ContentLoader class will just make it easier. After that I have a few other features like GamePad and Music which I would like to implement before saying I have a Beta type release.</p>
<p>The future after that is up in the air. I would love to try and have different implementations of the API for Xna and/or OpenTK.</p>
<p>I recommend for anyone who is interested as to why an API designer choose to implement the API in the way they did to try it for themselves. I have learned many things from this project including why certain design decisions were made by the Xna Framework team. </p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2011/10/16/what-is-snowball-and-what-do-i-want-to-do-with-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WMI: Converting datetime values to DateTime objects</title>
		<link>http://zacharysnow.net/2011/07/12/wmi-converting-datetime-values-to-datetime-objects/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wmi-converting-datetime-values-to-datetime-objects</link>
		<comments>http://zacharysnow.net/2011/07/12/wmi-converting-datetime-values-to-datetime-objects/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 17:35:55 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[wmi]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=433</guid>
		<description><![CDATA[Just a quick post to hopefully save anyone who finds this some time. If you need to convert a datetime value from a WMI query to a native DateTime object in C#, you can use the method ToDateTime() on the &#8230; <a href="http://zacharysnow.net/2011/07/12/wmi-converting-datetime-values-to-datetime-objects/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick post to hopefully save anyone who finds this some time. If you need to convert a datetime value from a WMI query to a native DateTime object in C#, you can use the method ToDateTime() on the ManagementDateTimeConverter class.</p>
<pre name="code" class="c#">
ManagementDateTimeConverter.ToDateTime(dmtfDate);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2011/07/12/wmi-converting-datetime-values-to-datetime-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Zune Playlist / SMIL Copier</title>
		<link>http://zacharysnow.net/2011/06/27/zune-playlist-smil-copier/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zune-playlist-smil-copier</link>
		<comments>http://zacharysnow.net/2011/06/27/zune-playlist-smil-copier/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 19:40:50 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[playlist]]></category>
		<category><![CDATA[smil]]></category>
		<category><![CDATA[windows-media-player]]></category>
		<category><![CDATA[zune]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=411</guid>
		<description><![CDATA[I needed a tool that would copy the contents of a Zune playlist to a given directory. I did a bit of googling and I couldn&#8217;t find anything, so like any good nerd I wrote a tool that did. The &#8230; <a href="http://zacharysnow.net/2011/06/27/zune-playlist-smil-copier/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I needed a tool that would copy the contents of a Zune playlist to a given directory. I did a bit of googling and I couldn&#8217;t find anything, so like any good nerd I wrote a tool that did.</p>
<p><a href="http://zacharysnow.net/images/SMILCopier.png"><img alt="SMILCopier" src="/images/SMILCopier.png" title="SMILCopier" class="alignnone" width="411" height="167" /></a></p>
<p>The format of Zune playlists is a simple XML format known as <a href="http://www.w3.org/AudioVideo/">SMIL</a>. I think Windows Media Player also stores playlists in this format but I haven&#8217;t confirmed that yet.</p>
<p>I wrote the tool very quickly and have only tested it on my machine with my test data, but I&#8217;ll provide the source if you&#8217;d like to modify for yourself. The source is C#.</p>
<p><a href="http://zacharysnow.net/files/SMILCopier.zip">Download Executable</a><br />
<a href="http://zacharysnow.net/files/SMILCopier-src.zip">Download Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2011/06/27/zune-playlist-smil-copier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Component Glue</title>
		<link>http://zacharysnow.net/2010/11/02/component-glue/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=component-glue</link>
		<comments>http://zacharysnow.net/2010/11/02/component-glue/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 19:00:00 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[codeplex]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=353</guid>
		<description><![CDATA[The other day I started an open source project for an IoC Container I call Component Glue. It&#8217;s in a toy / learning tool status right now and I don&#8217;t know how far I actually want to take the project. &#8230; <a href="http://zacharysnow.net/2010/11/02/component-glue/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The other day I started an open source project for an <a href="http://www.martinfowler.com/articles/injection.html">IoC Container</a> I call <a href="http://componentglue.codeplex.com/">Component Glue</a>. It&#8217;s in a toy / learning tool status right now and I don&#8217;t know how far I actually want to take the project. More to come though.</p>
<p><a href="http://componentglue.codeplex.com/">Component Glue on Codeplex</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/11/02/component-glue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing basic Dependency Injection using a Service Container</title>
		<link>http://zacharysnow.net/2010/06/21/implementing-basic-dependency-injection-using-services-container/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=implementing-basic-dependency-injection-using-services-container</link>
		<comments>http://zacharysnow.net/2010/06/21/implementing-basic-dependency-injection-using-services-container/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 19:09:14 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dependency-injection]]></category>
		<category><![CDATA[design-patterns]]></category>
		<category><![CDATA[service-continer]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=321</guid>
		<description><![CDATA[By extending your Service Container class, a very basic version of dependency injection can be implemented. We&#8217;ll implement two forms of dependency injection: constructor and property injection. We&#8217;ll start by defining the Injectable attribute. [AttributeUsage(AttributeTargets.Constructor &#124; AttributeTargets.Property, AllowMultiple = false, &#8230; <a href="http://zacharysnow.net/2010/06/21/implementing-basic-dependency-injection-using-services-container/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>By extending your Service Container class, a very basic version of dependency injection can be implemented. We&#8217;ll implement two forms of dependency injection: constructor and property injection. </p>
<p><span id="more-321"></span></p>
<p>We&#8217;ll start by defining the <strong>Injectable</strong> attribute. </p>
<pre name="code" class="c#">
[AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Property,
	AllowMultiple = false, Inherited = true)]
public class InjectableAttribute : Attribute
{
}
</pre>
<p>We&#8217;ll use this attribute to mark our constructors and properties for dependency injection. Next we&#8217;ll define an interface for our dependency injector:</p>
<pre name="code" class="c#">
public interface IDependencyInjector
{
	T Construct&lt;T&gt;();
	void Inject(object instance);
}
</pre>
<p>We&#8217;ll define our service container like so:</p>
<pre name="code" class="c#">
public class ServiceContainer : IDependencyInjector, IServiceProvider
{
	Dictionary&lt;Type, Object&gt; services;

	public ServiceContainer()
		: base()
	{
		this.services = new Dictionary&lt;Type, object&gt;();
	}

	public void AddService(Type type, Object provider)
	{
		if(null == type)
			throw new ArgumentNullException("type");

		if(null == provider)
			throw new ArgumentNullException("provider");

		if(this.services.ContainsKey(type))
			throw new InvalidOperationException("A provider is already registered the type " + type);

		var providerType = provider.GetType();

		if(!type.IsAssignableFrom(providerType))
			throw new InvalidOperationException(providerType + " is not an instance of " + type);

		this.services.Add(type, provider);
	}

	public object GetService(Type type)
	{
		if(null == type)
			throw new ArgumentNullException("type");

		if(this.services.ContainsKey(type))
			return this.services[type];

		return null;
	}

	public void RemoveService(Type type)
	{
		if(null == type)
			throw new ArgumentNullException("type");

		this.services.Remove(type);
	}

	protected object GetInjectableService(Type type)
	{
		if(type == typeof(IDependencyInjector) ||
		   type == typeof(IServiceProvider))
		{
			return this;
		}
		else
		{
			object service = this.GetService(type);

			if(service == null)
				throw new InvalidOperationException("Failed to find " + type + " depenedency.");

			return service;
		}
	}

	public T Construct&lt;T&gt;()
	{
		ConstructorInfo injectableConstructor = null;
		foreach(ConstructorInfo constructor in typeof(T).GetConstructors())
		{
			foreach(Attribute attribute in constructor.GetCustomAttributes(true))
			{
				if(attribute is InjectableAttribute)
				{
					injectableConstructor = constructor;
					break;
				}
			}

			if(injectableConstructor != null)
				break;
		}

		if(injectableConstructor == null)
			throw new InvalidOperationException("No injectable constructor found.");

		var parameters = injectableConstructor.GetParameters();
		var services = new object[parameters.Length];

		int i = 0;
		foreach(ParameterInfo parameter in parameters)
			services[i++] = GetInjectableService(parameter.ParameterType);

		return (T)injectableConstructor.Invoke(services);
	}

	public void Inject(object instance)
	{
		foreach(PropertyInfo property in instance.GetType().GetProperties())
		{
			foreach(Attribute attribute in property.GetCustomAttributes(true))
			{
				if(attribute is InjectableAttribute)
				{
					if(!property.CanWrite)
						throw new InvalidOperationException(property.Name + " is marked as Injectable but not writable.");

					property.SetValue(instance, GetInjectableService(property.PropertyType), null);
				}
			}
		}
	}
}
</pre>
<p>You can now construct new instances and inject dependencies on existing instances. Some usage examples:</p>
<pre name="code" class="c#">
public interface IFoo
{
	int Value { get; }
}

public class Foo : IFoo
{
	public int Value
	{
		get;
		set;
	}

	[Injectable]
	public Foo()
	{
	}

	public void DoIt()
	{
		Console.WriteLine(this.Value);
	}
}

public interface IBar
{
	string Value { get; }
}

public class Bar : IBar
{
	IFoo foo;

	public string Value
	{
		get;
		set;
	}

	[Injectable]
	public Bar(IFoo foo)
	{
		this.foo = foo;
	}

	public void DoIt()
	{
		Console.WriteLine(this.Value + ": " + this.foo.Value);
	}
}

public class Baz
{
	[Injectable]
	public IFoo Foo
	{
		get;
		set;
	}

	[Injectable]
	public IBar Bar
	{
		get;
		set;
	}

	public void DoIt()
	{
		Console.WriteLine(this.Bar.Value + " | " + this.Foo.Value);
	}
}

class Program
{
	static void Main(string[] args)
	{
		var container = new ServiceContainer();

		var foo = container.Construct&lt;Foo&gt;();
		foo.Value = 5;
		container.AddService(typeof(IFoo), foo);

		var bar = container.Construct&lt;Bar&gt;();
		container.AddService(typeof(IBar), bar);
		bar.Value = "Hello World!";
		bar.DoIt();

		var baz = new Baz();
		container.Inject(baz);
		baz.DoIt();
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/06/21/implementing-basic-dependency-injection-using-services-container/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Progress Bar in Windows 7 Taskbars</title>
		<link>http://zacharysnow.net/2010/06/01/progress-bars-in-windows-7-taskbars/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=progress-bars-in-windows-7-taskbars</link>
		<comments>http://zacharysnow.net/2010/06/01/progress-bars-in-windows-7-taskbars/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 18:00:20 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[win7]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=308</guid>
		<description><![CDATA[I decided to add progress bar to the Windows 7 Taskbar in my Timer app. I started by downloading and compiling the Windows API Code Pack in Release mode. I then added a reference to the Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll files &#8230; <a href="http://zacharysnow.net/2010/06/01/progress-bars-in-windows-7-taskbars/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I decided to add progress bar to the Windows 7 Taskbar in my Timer app.</p>
<p><a href="http://zacharysnow.net/wp-content/uploads/2010/06/TimerProgressBar.png"><img src="http://zacharysnow.net/wp-content/uploads/2010/06/TimerProgressBar.png" alt="" title="TimerProgressBar" width="139" height="40" class="alignnone size-full wp-image-309" /></a></p>
<p>I started by downloading and compiling the <a href="http://code.msdn.microsoft.com/WindowsAPICodePack">Windows API Code Pack</a> in Release mode. I then added a reference to the Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll files to the project. After that add the lines:</p>
<pre name="code" class="c#">
using Microsoft.WindowsAPICodePack.Taskbar;
</pre>
<p>to your using statements. When the clock starts running I create the progress bar in the taskbar with:</p>
<pre name="code" class="c#">
// Initialize progress bar
if(TaskbarManager.IsPlatformSupported)
{
	TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal);
	TaskbarManager.Instance.SetProgressValue(0, (int)this.totalTime.TotalSeconds, this.Handle);
}
</pre>
<p>to stop the progress bar:</p>
<pre name="code" class="c#">
// Stop progress bar
if(TaskbarManager.IsPlatformSupported)
	TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.NoProgress);
</pre>
<p>and finally to update the progress bar on each tick:</p>
<pre name="code" class="c#">
// Update progress bar
if(TaskbarManager.IsPlatformSupported)
	TaskbarManager.Instance.SetProgressValue((int)this.totalTime.TotalSeconds - (int)this.time.TotalSeconds, (int)this.totalTime.TotalSeconds, this.Handle);
</pre>
<p><a href="http://zacharysnow.net/files/TimerWin7Taskbar.zip">Download Binary</a><br />
<a href="http://zacharysnow.net/files/TimerWin7TaskbarSrc.zip">Download Source</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/06/01/progress-bars-in-windows-7-taskbars/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing Rectangles with SpriteBatch</title>
		<link>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=drawing-rectangles-with-spritebatch</link>
		<comments>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 17:14:25 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[XNA]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/</guid>
		<description><![CDATA[Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch: public static class SpriteBatchHelper { static Texture2D pixel; private static void LoadPixel(GraphicsDevice graphicsDevice) { if(pixel == null) { pixel = new Texture2D(graphicsDevice, 1, 1); pixel.SetData&#60;Color&#62;(new &#8230; <a href="http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just a quick code snippet which adds an extension method for drawing Rectangles to SpriteBatch:</p>
<pre name="code" class="c#">
public static class SpriteBatchHelper
{
	static Texture2D pixel;

	private static void LoadPixel(GraphicsDevice graphicsDevice)
	{
		if(pixel == null)
		{
			pixel = new Texture2D(graphicsDevice, 1, 1);
			pixel.SetData&lt;Color&gt;(new Color[] { Color.White });
		}
	}

	public static void DrawRectangle(this SpriteBatch spriteBatch, Rectangle rectangle, Color color)
	{
		LoadPixel(spriteBatch.GraphicsDevice);
		spriteBatch.Draw(pixel, rectangle, color);
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/03/29/drawing-rectangles-with-spritebatch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Extension Methods in your own Library</title>
		<link>http://zacharysnow.net/2010/03/08/csharp-extension-methods-in-your-own-library/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=csharp-extension-methods-in-your-own-library</link>
		<comments>http://zacharysnow.net/2010/03/08/csharp-extension-methods-in-your-own-library/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:54:06 +0000</pubDate>
		<dc:creator>Zachary</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[extension-methods]]></category>

		<guid isPermaLink="false">http://zacharysnow.net/?p=295</guid>
		<description><![CDATA[Normally I use extension methods in C# to extend a library that I did not write and therefore I have no control over. There are situations where it makes sense to use extension methods for a library that you yourself &#8230; <a href="http://zacharysnow.net/2010/03/08/csharp-extension-methods-in-your-own-library/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Normally I use extension methods in C# to extend a library that I did not write and therefore I have no control over. There are situations where it makes sense to use extension methods for a library that you yourself are writing.</p>
<p>For example, when you have interfaces in your library. You want to keep the number of methods in that interface as low as possible so that classes implementing the interface don&#8217;t have to do a lot of heavy lifting. This means cutting out methods in an interface that are for the most part just syntactic sugar for another method in the interface.</p>
<pre name="code" class="c#">

public interface IServiceContainer
{
    void AddService(Type type, Object provider);
    object GetService(Type type);
}

public static class IServiceContainerExtensions
{
    public static void AddService&lt;T&gt;(this IServiceContainer services, object provider)
    {
        services.AddService(typeof(T), provider);
    }

    public static T GetService&lt;T&gt;(this IServiceContainer services) where T : class
    {
        return services.GetService(typeof(T)) as T;
    }

    public static T GetRequiredService&lt;T&gt;(this IServiceContainer services) where T : class
    {
        T service = services.GetService(typeof(T)) as T;

        if(service == null)
            throw new ServiceNotFoundException(typeof(T));

        return service;
    }
}
</pre>
<p>All of the methods in IServiceContainerExtensions are just helper methods for method in IServiceContainer. By making them extension methods in our own library though, we&#8217;ve made the barrier to entry lower. Other people can implement the interface and in a sense &#8220;inherit&#8221; the helper methods as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://zacharysnow.net/2010/03/08/csharp-extension-methods-in-your-own-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

