<?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>chillijam.co.uk &#187; C# Snippets</title>
	<atom:link href="http://chillijam.co.uk/category/c-snippets/feed/" rel="self" type="application/rss+xml" />
	<link>http://chillijam.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 28 Jan 2021 10:32:11 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>WP7 Navigation Gotcha</title>
		<link>http://chillijam.co.uk/2012/02/16/wp7-navigation-gotcha/</link>
		<comments>http://chillijam.co.uk/2012/02/16/wp7-navigation-gotcha/#comments</comments>
		<pubDate>Thu, 16 Feb 2012 15:28:13 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=393</guid>
		<description><![CDATA[Navigating to a page, passing a key defined by a third party can present problems, but taking a little care over the way you pass the parameters can save a world of pain later.]]></description>
				<content:encoded><![CDATA[<p>This afternoon I discovered a bug in one of the apps I am working on.  It happens because the keys we use for navigation are provided in a third-party feed, and one of those keys contains a plus sign (&#8220;+&#8221;).  </p>
<p>This wasn&#8217;t immediately apparent as an issue, and we just navigated using </p>
<pre class="brush: csharp; title: ; notranslate">
NavigationService.Navigate(new Uri(&quot;/Views/TargetView.xaml?key=&quot; + myKeyValue, UriKind.Relative));
</pre>
<p>Unfortunately, this gets UrlDecoded on the receiving side as a sapce, so our key that started out being &#8220;text+text&#8221; was being resolved as &#8220;text text&#8221;.  When looking that value up in our tables the key was obviosuly not found and a NullReferenceException was thrown.</p>
<p>The fix was pretty simple, though.  We just had to UrlEncode the strong before we sent it, as follows.</p>
<pre class="brush: csharp; title: ; notranslate">
NavigationService.Navigate(new Uri(&quot;/Views/TargetView.xaml?key=&quot; + System.Net.HttpUtility.UrlEncode(myKeyValue), UriKind.Relative));
</pre>
<p>and all is good with the world again.</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2012/02/16/wp7-navigation-gotcha/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight Toolkit ExpanderView &#8211; Flat Objects</title>
		<link>http://chillijam.co.uk/2012/01/26/silverlight-toolkit-expanderview-flat-objects/</link>
		<comments>http://chillijam.co.uk/2012/01/26/silverlight-toolkit-expanderview-flat-objects/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 14:40:27 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Windows Phone]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=382</guid>
		<description><![CDATA[Just one of many ways in which you can bind an ExpanderView control to a collection of simple objects.]]></description>
				<content:encoded><![CDATA[<p>Before anyone posts, I know this is not the intended usage of the control.  I did, however, have a requirement to show a list of simple objects and some of their properties in an expander view, so I&#8217;m going to explain one of many possible ways to do it.</p>
<p>My ViewModel contains an ObservableCollection&lt;ServiceAddOn&gt;, where my ServiceAddOn class looks like this.</p>
<pre class="brush: csharp; title: ; notranslate">
public class ServiceAddOn : INotifyPropertyChanged
{
    string name;
    string cost;
    DateTime startDate;
    DateTime expiryDate;
    // encapsulation for public access to the 4 locals
    // INotifyPropertyChanged implementation
}
</pre>
<p>Don&#8217;t worry about the encapsulation or INotifyPropertyChanged implementation &#8211; they  are as simple as it gets.</p>
<p>Now, I want to create an ListBox of ExpanderView controls that will display the information like this :</p>
<p><code><br />
My Item Name<br />
- Cost : £123.99<br />
- Start Date : 1/1/2012<br />
- Expiry Date : 1/1/2013<br />
Second Item<br />
ThirdItem<br />
</code></p>
<p>That is where the problems start.  As far as I can tell (with admittedly limited research into the issue) there is no way to bind to properties of an object in both the HeaderTemplate and the ItemTemplate &#8211; something needed to be done to expose these properties as a collection.  Since I was not able to change the inplementation of the ServiceAddOn class, I decided the best way would be to write a converter.  Before starting that, though, I needed a class to hold each of myt exposed properties.  It looks like this :</p>
<pre class="brush: csharp; title: ; notranslate">
public class ExposedProperty
{
    public string Key { get; set; }
    public string Value { get; set; }
}
</pre>
<p>Again, it is about as simple as it gets. <img src="http://chillijam.co.uk/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<p>Now, the converter:</p>
<pre class="brush: csharp; title: ; notranslate">
public class ExposePropertyConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ServiceAddOn obj = value as ServiceAddOn;
        if (null == obj) return null;
        ObservableCollection&lt;ExposedProperty&gt; values = new ObservableCollection&lt;ExposedProperty&gt;();
        values.Add(new ExposedProperty { Key = &quot;Cost&quot;, Value = obj.Cost });
        values.Add(new ExposedProperty { Key = &quot;Start Date&quot;, Value = obj.StartDate.Date.ToShortDateString() });
        values.Add(new ExposedProperty { Key = &quot;Expiry Date&quot;, Value = obj.ExpiryDate.Date.ToShortDateString() });

        return values;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
</pre>
<p>So now, it is just a case of a bit of plumbing&#8230;</p>
<p>Add the namespace reference to the XAML, and declare a converter instance</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;phone:PhoneApplicationPage 
    x:Class=&quot;MyExampleApp.MainPage&quot;
    ...snip...
    xmlns:conv=&quot;clr-namespace:MyExampleApp.Converters&quot;
    ...snip...
    &gt;
    &lt;phone:PhoneApplicationPage.Resources&gt;
        &lt;conv:ExposePropertyConverter x:Key=&quot;GetMyProperties&quot; /&gt;
    &lt;/phone:PhoneApplicationPage.Resources&gt;
</pre>
<p>and then wire up the Listbox / ExpanderView combo&#8230; (note tht my collection of ServiceAddOn objects is called &#8220;DisplayItems&#8221;)</p>
<pre class="brush: xml; title: ; notranslate">
&lt;ListBox ItemsSource=&quot;{Binding DisplayItems}&quot;&gt;
    ...snip standard plumbing...
    &lt;ListBox.ItemTemplate&gt;
        &lt;DataTemplate&gt;
             &lt;toolkit:ExpanderView 
                 ItemsSource=&quot;{Binding Converter={StaticResource GetMyProperties}}&quot;
                 Header=&quot;{Binding}&quot;
                 NonExpandableHeader=&quot;{Binding}&quot;
                 Expander=&quot;{Binding}&quot;&gt;
                 &lt;toolkit:ExpanderView.HeaderTemplate&gt;
                     &lt;DataTemplate&gt;
                         &lt;TextBlock Text=&quot;{Binding Name}&quot; /&gt;
                     &lt;/DataTemplate&gt;
                 &lt;/toolkit:ExpanderView.HeaderTemplate&gt;
                 &lt;toolkit:ExpanderView.ItemTemplate&gt;
                     &lt;DataTemplate&gt;
                         &lt;TextBlock&gt;
                             &lt;Run Text=&quot;{Binding Key}&quot; /&gt;
                             &lt;Run Text=&quot; : &quot; /&gt;
                             &lt;Run Text=&quot;{Binding Value}&quot; /&gt;
                         &lt;/TextBlock&gt;
                     &lt;/DataTemplate&gt;
                 &lt;/toolkit:ExpanderView.ItemTemplate&gt;
             &lt;/toolkit:ExpanderView&gt;
        &lt;/DataTemplate&gt;
    &lt;/ListBox.ItemTemplate&gt;
&lt;/ListBox&gt;
</pre>
<p>The &#8220;magic&#8221; happens on line 6, where I bind the ItemsSource of the ExpanderView control to &#8220;{Binding Converter={StaticResource GetMyProperties}}&#8221;.</p>
<p>That&#8217;s it.</p>
<p>Next steps for this may be to make the Converter more generic, and possibly use reflection to get the properties instead of hard coding them, but for now this is good enough.</p>
<p>If you have any comments about a much simpler way to bind to flat, single-level objects in an ExpanderView control. I&#8217;d love to hear them.</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2012/01/26/silverlight-toolkit-expanderview-flat-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alter a Pivot controls header template the easy way (WP7)</title>
		<link>http://chillijam.co.uk/2012/01/11/alter-a-pivot-controls-header-template-the-easy-way-wp7/</link>
		<comments>http://chillijam.co.uk/2012/01/11/alter-a-pivot-controls-header-template-the-easy-way-wp7/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 11:01:49 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Mango]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=369</guid>
		<description><![CDATA[If you want to alter the template for a Windows Phone 7 pivot control&#8217;s header, the simplest way is as follows&#8230; Of course, all this example does is specifically set the colour used for the title and headers to black, but you can do (almost) whatever you like in those templates. Want to add an &#8230; <a href="http://chillijam.co.uk/2012/01/11/alter-a-pivot-controls-header-template-the-easy-way-wp7/" class="more-link">Continue reading <span class="screen-reader-text">Alter a Pivot controls header template the easy way (WP7)</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>If you want to alter the template for a Windows Phone 7 pivot control&#8217;s header, the simplest way is as follows&#8230;</p>
<pre class="brush: csharp; title: ; notranslate">
        &lt;controls:Pivot Title=&quot;Altered Styles&quot;&gt;
            &lt;controls:Pivot.HeaderTemplate&gt;  
                &lt;!-- This changes to look of the items headers --&gt;
                &lt;DataTemplate&gt;
                    &lt;TextBlock Text=&quot;{Binding}&quot; Foreground=&quot;Black&quot;/&gt;
                &lt;/DataTemplate&gt;
            &lt;/controls:Pivot.HeaderTemplate&gt;
            &lt;controls:Pivot.TitleTemplate&gt;
                &lt;!-- This changes to look of the pivot overall title --&gt;
                &lt;DataTemplate&gt;
                    &lt;TextBlock Text=&quot;{Binding}&quot; Foreground=&quot;Black&quot;/&gt;
                &lt;/DataTemplate&gt;
            &lt;/controls:Pivot.TitleTemplate&gt;
            &lt;controls:PivotItem Header=&quot;daily&quot;&gt;
                &lt;Grid/&gt;
            &lt;/controls:PivotItem&gt;
            &lt;controls:PivotItem Header=&quot;hourly&quot;&gt;
                &lt;Grid/&gt;
            &lt;/controls:PivotItem&gt;
        &lt;/controls:Pivot&gt;
</pre>
<p>Of course, all this example does is specifically set the colour used for the title and headers to black, but you can do (almost) whatever you like in those templates.  Want to add an image as a bullet?  Go for it.</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;controls:Pivot.HeaderTemplate&gt;
    &lt;DataTemplate&gt;
        &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
            &lt;Image Height=&quot;48&quot; Width=&quot;48&quot; Source=&quot;/MyBulletImage.png&quot; /&gt;
            &lt;TextBlock Text=&quot;{Binding}&quot; Foreground=&quot;Black&quot;/&gt;
        &lt;/StackPanel&gt;
    &lt;/DataTemplate&gt;
&lt;/controls:Pivot.HeaderTemplate&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2012/01/11/alter-a-pivot-controls-header-template-the-easy-way-wp7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternating ListBox item background colours in WP7</title>
		<link>http://chillijam.co.uk/2012/01/11/alternating-listbox-item-background-colours-in-wp7/</link>
		<comments>http://chillijam.co.uk/2012/01/11/alternating-listbox-item-background-colours-in-wp7/#comments</comments>
		<pubDate>Wed, 11 Jan 2012 10:34:42 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Mango]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[WP7]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=364</guid>
		<description><![CDATA[I had a requirement today to implement alternating row colours in a Windows Phone 7 ListBox. After a bit of frustration with searching and only finding answers tht said &#8220;You can&#8217;t do it&#8221;, or that you need to add a property on the model to bind the background to, I eventually hit upon a nugget &#8230; <a href="http://chillijam.co.uk/2012/01/11/alternating-listbox-item-background-colours-in-wp7/" class="more-link">Continue reading <span class="screen-reader-text">Alternating ListBox item background colours in WP7</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I had a requirement today to implement alternating row colours in a Windows Phone 7 ListBox. After a bit of frustration with searching and only finding answers tht said &#8220;You can&#8217;t do it&#8221;, or that you need to add a property on the model to bind the background to, I eventually hit upon a nugget of common sense on <a href="http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/f9e9ce26-d576-45f8-a79d-762176695385" target="_blank">this page</a>.</p>
<p>Basically you need to create a converter class that will handle the alternation of backgrounds for you. It is ridiculously simple once you see it in action.</p>
<p><strong>Step 1 : Create the converter.</strong></p>
<pre class="brush: csharp; title: ; notranslate">
public class AlternateRowColour : IValueConverter
{
bool isAlternate;
SolidColorBrush even = new SolidColorBrush(Colors.Transparent); // Set these two brushes to your alternating background colours.
SolidColorBrush odd = new SolidColorBrush(Color.FromArgb(255, 241, 241, 241));

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
isAlternate = !isAlternate;
return isAlternate ? even : odd ;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
</pre>
<p>Step 2 : Add the converter to the page</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;UserControl
 	...snip...
 	xmlns:conv=&quot;clr-namespace:MyApplication.Converters&quot; 
 	...snip...
 	&gt;
	&lt;UserControl.Resources&gt;
		&lt;conv:AlternateRowColour x:Key=&quot;RowColour&quot; /&gt;
	&lt;/UserControl.Resources&gt;
	...snip...
&lt;/UserControl&gt;
</pre>
<p>Step 3 : Bind to the ListBox</p>
<pre class="brush: csharp; title: ; notranslate">
&lt;ListBox ItemsSource=&quot;{Binding}&quot;&gt;
  &lt;ListBox.ItemTemplate&gt;
    &lt;DataTemplate&gt;
      &lt;Grid Background=&quot;{Binding Converter={StaticResource RowColour}}&quot;&gt;
        &lt;!-- layout XAML --&gt;
      &lt;/Grid&gt;
    &lt;/DataTemplate&gt;
  &lt;/ListBox.ItemTemplate&gt;
&lt;/ListBox&gt;				
</pre>
<p>And you&#8217;re done.</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2012/01/11/alternating-listbox-item-background-colours-in-wp7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using LINQ to randomise a list.</title>
		<link>http://chillijam.co.uk/2011/09/22/using-linq-to-randomise-a-list/</link>
		<comments>http://chillijam.co.uk/2011/09/22/using-linq-to-randomise-a-list/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 14:03:32 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=359</guid>
		<description><![CDATA[This afternoon I needed to select some random items from a collection, and my searching ran into this idea. I modified it a little to do what I needed (Select 4 items at random from a list), and this is what I came up with&#8230;]]></description>
				<content:encoded><![CDATA[<p>This afternoon I needed to select some random items from a collection, and my searching ran into this idea.</p>
<pre class="brush: csharp; title: ; notranslate">
Random rnd = new Random();
var randomizedList = from item in list
                     orderby rnd.Next()
                     select item;
</pre>
<p>I modified it a little to do what I needed (Select 4 items at random from a list), and this is what I came up with&#8230;</p>
<pre class="brush: csharp; title: ; notranslate">
Random rnd = new Random();
var MyCollection = (from icon in TheOriginalList orderby rnd.Next() select icon).Take(4));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2011/09/22/using-linq-to-randomise-a-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Split a Camel-Cased word into its components</title>
		<link>http://chillijam.co.uk/2010/05/20/split-a-camel-cased-word-into-its-components/</link>
		<comments>http://chillijam.co.uk/2010/05/20/split-a-camel-cased-word-into-its-components/#comments</comments>
		<pubDate>Thu, 20 May 2010 08:00:03 +0000</pubDate>
		<dc:creator><![CDATA[Marc]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://chillijam.co.uk/?p=304</guid>
		<description><![CDATA[I needed to split a camel-cased word into it&#8217;s component parts, and found the following extension method to help out. It works pretty well.]]></description>
				<content:encoded><![CDATA[<p>I needed to split a camel-cased word into it&#8217;s component parts, and found the following extension method to help out.  It works pretty well.</p>
<pre class="brush: csharp; title: ; notranslate">
public static string SplitCamelCase( this string str )
{
return Regex.Replace( Regex.Replace( str, @&quot;(\P{Ll})(\P{Ll}\p{Ll})&quot;, &quot;$1 $2&quot; ), @&quot;(\p{Ll})(\P{Ll})&quot;, &quot;$1 $2&quot; );
}</pre>
<p><a href="http://chillijam.co.uk/wp-content/uploads/2011/04/20110409-060908.jpg"><img src="http://chillijam.co.uk/wp-content/uploads/2011/04/20110409-060908.jpg" alt="20110409-060908.jpg" height=0 width=0 class="alignnone size-full" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2010/05/20/split-a-camel-cased-word-into-its-components/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apostrophe In RowFilter</title>
		<link>http://chillijam.co.uk/2006/09/25/apostrophe-in-rowfilter/</link>
		<comments>http://chillijam.co.uk/2006/09/25/apostrophe-in-rowfilter/#comments</comments>
		<pubDate>Mon, 25 Sep 2006 16:05:08 +0000</pubDate>
		<dc:creator><![CDATA[historical]]></dc:creator>
				<category><![CDATA[C# Snippets]]></category>

		<guid isPermaLink="false">http://playingwithanimport.wordpress.com/1999/11/30/apostrophe-in-rowfilter/</guid>
		<description><![CDATA[According to ASP.NET Resources, there is a problem when using apostrophes in a RowFilter expression in a DataView. The fix seems to be to double the apostrophes (&#8220;O&#8221;Mally&#8221;) rather than escaping them (&#8220;O\&#8217;Mally&#8221;)]]></description>
				<content:encoded><![CDATA[<p>According to <a href="http://www.aspnetresources.com/blog/apostrophe_in_rowfilter.aspx">ASP.NET Resources</a>, there is a problem when using apostrophes in a RowFilter expression in a DataView.  The fix seems to be to double the apostrophes (&#8220;O&#8221;Mally&#8221;) rather than escaping them (&#8220;O\&#8217;Mally&#8221;)</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2006/09/25/apostrophe-in-rowfilter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterate through enum values</title>
		<link>http://chillijam.co.uk/2006/08/30/iterate-through-enum-values/</link>
		<comments>http://chillijam.co.uk/2006/08/30/iterate-through-enum-values/#comments</comments>
		<pubDate>Wed, 30 Aug 2006 11:16:28 +0000</pubDate>
		<dc:creator><![CDATA[historical]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://playingwithanimport.wordpress.com/2006/08/30/iterate-through-enum-values/</guid>
		<description><![CDATA[A quick example of how to iterate through the values in an enum&#8230; enum myEnum { one, two, three, four, five, }; foreach (int i in Enum.GetValues(typeof(myEnum ))) { myEnum myItem = (myEnum )Enum.ToObject(typeof(myEnum ), i); System.Diagnostics.Debug.WriteLine(myItem.ToString()); }]]></description>
				<content:encoded><![CDATA[<p>A quick example of how to iterate through the values in an enum&#8230;</p>
<p><code>enum myEnum { one, two, three, four, five, };<br />
foreach (int i in Enum.GetValues(typeof(myEnum )))<br />
{<br />
  myEnum myItem = (myEnum )Enum.ToObject(typeof(myEnum ), i);<br />
  System.Diagnostics.Debug.WriteLine(myItem.ToString());<br />
}<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2006/08/30/iterate-through-enum-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Determine if an assembly is a debug or release build</title>
		<link>http://chillijam.co.uk/2006/05/04/determine-if-an-assembly-is-a-debug-or-release-build/</link>
		<comments>http://chillijam.co.uk/2006/05/04/determine-if-an-assembly-is-a-debug-or-release-build/#comments</comments>
		<pubDate>Thu, 04 May 2006 15:47:46 +0000</pubDate>
		<dc:creator><![CDATA[historical]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# Snippets]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://playingwithanimport.wordpress.com/2006/05/04/determine-if-an-assembly-is-a-debug-or-release-build/</guid>
		<description><![CDATA[*This code originally came from another blog, although it was written in VB over there. It has lost some elegance in the translation, but I needed a very quick solution this afternoon, and I needed it in C#, so here you go. * I needed to determine whether several assemblies were built as &#8220;Debug&#8221; or &#8230; <a href="http://chillijam.co.uk/2006/05/04/determine-if-an-assembly-is-a-debug-or-release-build/" class="more-link">Continue reading <span class="screen-reader-text">Determine if an assembly is a debug or release build</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>*This code originally came from <a href="http://msmvps.com/blogs/bill/archive/2004/06/17/8339.aspx">another blog</a>, although it was written in VB over there.  It has lost some elegance in the translation, but I needed a very quick solution this afternoon, and I needed it in C#, so here you go.  *</p>
<p>I needed to determine whether several assemblies were built as &#8220;Debug&#8221; or &#8220;Release&#8221; code this afternoon.  They didn&#8217;t have any debug symbols with them, so that was no help.  A bit of searching led me to the code below.</p>
<pre><pre class="brush: csharp; title: ; notranslate">private void testfile(string file)
{
	if(isAssemblyDebugBuild(file))
	{
		MessageBox.Show(String.Format(&quot;{0} seems to be a debug build&quot;,file));
	}
	else
	{
		MessageBox.Show(String.Format(&quot;{0} seems to be a release build&quot;,file));
	}
}

private bool isAssemblyDebugBuild(string filename)
{
	return isAssemblyDebugBuild(System.Reflection.Assembly.LoadFile(filename));
}

private bool isAssemblyDebugBuild(System.Reflection.Assembly assemb)
{
	bool retVal = false;
	foreach(object att in assemb.GetCustomAttributes(false))
	{
		if(att.GetType() == System.Type.GetType(&quot;System.Diagnostics.DebuggableAttribute&quot;))
		{
			retVal = ((System.Diagnostics.DebuggableAttribute)att).IsJITTrackingEnabled;
		}
	}
	return retVal;
}</pre>
<p>The overloaded &#8220;isAssemblyDebugBuild&#8221; method will accept either a string containing the fiull filename, or a System.Reflection.Assembly object.  It will return its best guess as to whether the the assembly is a debug build, as a boolean.</p>
<p>To call the methods as they are (to get a MessageBox), just use something like</p>
<pre><pre class="brush: csharp; title: ; notranslate">testfile(@&quot;C:\myUnknownAssembly.dll&quot;);</pre>
<p>or access the underlying methods directly :</p>
<pre><pre class="brush: csharp; title: ; notranslate">bool isDebug = isAssemblyDebugBuild(@&quot;C:\myUnkownAssembly.dll&quot;);</pre>
<p>I hope someone can use this to save themselves some time.</p>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2006/05/04/determine-if-an-assembly-is-a-debug-or-release-build/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sending an HTTP POST request to a web server</title>
		<link>http://chillijam.co.uk/2005/10/26/sending-an-http-post-request-to-a-web-server/</link>
		<comments>http://chillijam.co.uk/2005/10/26/sending-an-http-post-request-to-a-web-server/#comments</comments>
		<pubDate>Wed, 26 Oct 2005 10:20:30 +0000</pubDate>
		<dc:creator><![CDATA[historical]]></dc:creator>
				<category><![CDATA[C# Snippets]]></category>

		<guid isPermaLink="false">http://playingwithanimport.wordpress.com/2005/10/26/sending-an-http-post-request-to-a-web-server/</guid>
		<description><![CDATA[ASCIIEncoding encoding = new ASCIIEncoding(); string post_url = "http://172.16.24.44/postdata.php"; string poststring = "monkeys=blue\&#38;trousers=black"; byte[] data = encoding.GetBytes(poststring); HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(post_url); httpRequest.Method = "POST"; httpRequest.ContentType="application/x-www-form-urlencoded"; httpRequest.ContentLength = data.Length; Stream newStream=httpRequest.GetRequestStream(); newStream.Write(data,0,data.Length); newStream.Close(); HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse (); Stream receiveStream = response.GetResponseStream (); StreamReader readStream = new StreamReader (receiveStream); textBox1.Text = "Response stream received."; textBox1.Text = &#8230; <a href="http://chillijam.co.uk/2005/10/26/sending-an-http-post-request-to-a-web-server/" class="more-link">Continue reading <span class="screen-reader-text">Sending an HTTP POST request to a web server</span> <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<pre>ASCIIEncoding encoding = new ASCIIEncoding();
string post_url = "http://172.16.24.44/postdata.php";
string poststring = "monkeys=blue\&amp;trousers=black";
byte[] data = encoding.GetBytes(poststring);

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(post_url);
httpRequest.Method = "POST";
httpRequest.ContentType="application/x-www-form-urlencoded";
httpRequest.ContentLength = data.Length;
Stream newStream=httpRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();

HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse ();
Stream receiveStream = response.GetResponseStream ();
StreamReader readStream = new StreamReader (receiveStream);
textBox1.Text = "Response stream received.";
textBox1.Text = readStream.ReadToEnd().Replace("\\n","\\r\\n");
response.Close ();
readStream.Close ();</pre>
]]></content:encoded>
			<wfw:commentRss>http://chillijam.co.uk/2005/10/26/sending-an-http-post-request-to-a-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
