<?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>Software Warlock &#187; C#</title>
	<atom:link href="http://softwareblog.morlok.net/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://softwareblog.morlok.net</link>
	<description></description>
	<lastBuildDate>Sat, 24 Dec 2011 16:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>BugAid</title>
		<link>http://softwareblog.morlok.net/2011/12/23/bugaid/</link>
		<comments>http://softwareblog.morlok.net/2011/12/23/bugaid/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 13:52:22 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=435</guid>
		<description><![CDATA[I&#8217;m excited; I just read this blog post about BugAid, a Visual Studio add-on that has many features I&#8217;ve often wanted myself. Chief among them is the ability to search through a live object for data. When you are working &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2011/12/23/bugaid/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m excited; I just read <a href="http://visualstudioaddins.svprogramming.net/2011/12/22/bugaid-tool-for-visual-studio-review/">this blog post</a> about <a href="http://www.bugaidsoftware.com/">BugAid</a>, a Visual Studio add-on that has many features I&#8217;ve often wanted myself. Chief among them is the ability to search through a live object for data. When you are working with an unfamiliar code base and you are trying to track how data flows through the system, this can be very useful. Another cool feature is that it shows the partial evaluation of expressions within an if statement. Very cool. Going to install the beta today.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2011/12/23/bugaid/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Naming collision in ASMX web service</title>
		<link>http://softwareblog.morlok.net/2010/07/02/naming-collision-in-asmx-web-service/</link>
		<comments>http://softwareblog.morlok.net/2010/07/02/naming-collision-in-asmx-web-service/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 20:32:55 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=392</guid>
		<description><![CDATA[I ran into an interesting problem today while working on an ASMX web service. Everything compiled fine, but when I went to view the ASXM in the browser to manually invoke the methods to test it, the normal automatically generated &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2010/07/02/naming-collision-in-asmx-web-service/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>I ran into an interesting problem today while working on an ASMX web service.</p>
<p>Everything compiled fine, but when I went to view the ASXM in the browser to manually invoke the methods to test it, the normal automatically generated info page didn&#8217;t come up.  Trying to grab the WSDL also gave me a blank page.</p>
<p>I then attached the debugger to IIS to observe what was happening when I tried to view the ASMX info page.  By breaking on exceptions, I was able to uncover the following error:</p>
<p><em>The XML element &#8216;XXX&#8217; from namespace &#8216;http://tempuri.org/&#8217; references a method and a type. Change the method&#8217;s message name using WebMethodAttribute or change the type&#8217;s root element using the XmlRootAttribute.</em></p>
<p>With a little more thinking, I was able uncover what I believe was going on.</p>
<p>I had defined a complex type to return as a response:</p>
<pre name="code" class="c#">
public class FooResponse {...}

[WebMethod]
public FooResponse Foo() {...}
</pre>
<p>Note that here the exact name pairing of Foo/Foo+Response is important. When I changed the method name as follows, the problem went away:</p>
<pre name="code" class="C#">
public class FooResponse {...}

[WebMethod]
public FooResponse Fooxxx() {...}
</pre>
<p>What I believe is happening is .NET is attempting to automatically wrap the response coming from the Foo method with an element named FooResponse. The use of that same name as the object you want to return creates ambiguity. By changing the name of my response object, or the name of my method I was able to avoid this collision.</p>
<p>I have also posted this information in response to a <a href="http://stackoverflow.com/questions/580042/c-web-service-client-multiple-web-service-methods-with-same-complex-return-ty/">question</a> on StackOverflow.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2010/07/02/naming-collision-in-asmx-web-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XSLT with Optional Extension Object</title>
		<link>http://softwareblog.morlok.net/2009/12/08/xslt-with-optional-extension-object/</link>
		<comments>http://softwareblog.morlok.net/2009/12/08/xslt-with-optional-extension-object/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 02:41:13 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=315</guid>
		<description><![CDATA[Extension objects provide a convenient way to allow an XSL transform to interact with the outside world. They can also be used to offload computations that are difficult to express in XSL. Recently, I ran into a scenario where I &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2009/12/08/xslt-with-optional-extension-object/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Extension objects provide a convenient way to allow an XSL transform to interact with the outside world.  They can also be used to offload computations that are difficult to express in XSL.</p>
<p>
Recently, I ran into a scenario where I wanted to write an XSLT that would use an extension object to record some additional information about intermediate steps of the transform.  The extension object was not required, however, and I wanted to leave it up to the user of the XSLT as to if they wanted to use the extension object.
</p>
<p>
The question then became how to conditionally execute calls to the extension object.  Suppose you have the following extension object defined in C#:
</p>
<pre name="code" class="C#">
public class FooExtensionObject
{
  private List&lt;string> recordedValues = new List&lt;string>();

  public void RecordValue(string val)
  {
    recordedValues.Add(val);
  }
}
</pre>
<p>Now suppose this extension object is used in the following XSLT:</p>
<pre name="code" class="xml">
&lt;xsl:transform
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:foo="urn:FooExObj">
  &lt;template match="/">
    ...
    &lt;xsl:value-of select="foo:RecordValue(@bar)"/>
    ...
  &lt;/template>
&lt;/transform>
</pre>
<p>The extension object is supplied to the XSLT through the arguments:</p>
<pre name="code" class="C#">
XslTransform transform = new XslTransform();
transform.Load("SomeFile.xsl");

XsltArgumentList args = new XsltArgumentList();
FooExtensionObject aFoo = new FooExtensionObject();
args.AddExtensionObject("urn:FooExObj", aFoo);

XmlTextWriter myWriter = new XmlTextWriter("output.xml", null);

transform.Transform(xpathDoc, args, myWriter);
</pre>
<p>
In the case where the extension object is not supplied, however, this XSLT will cause an error.  To correct this issue, you can use the <tt>function-available</tt> function.  Change the transform as follows:
</p>
<pre name="code" class="xml">
...
&lt;xsl:if test="function-available('foo:RecordValue')">
  &lt;xsl:value-of select="foo:RecordValue(@bar)"/>
&lt;/xsl:if>
...
</pre>
<p>
The conditional statement blocks out the use of the extension object when it does not exist.  Unfortunately, you must do this where ever you wish to use the extension object.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2009/12/08/xslt-with-optional-extension-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XML Comments</title>
		<link>http://softwareblog.morlok.net/2009/11/24/xml-comments/</link>
		<comments>http://softwareblog.morlok.net/2009/11/24/xml-comments/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 02:21:01 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[comment]]></category>
		<category><![CDATA[cref]]></category>
		<category><![CDATA[see]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/?p=309</guid>
		<description><![CDATA[Today I was looking for a reference for the proper notation for the cref attribute of the &#60;see cref="..."&#62; C# XML comment tag, and after finding it, I thought I&#8217;d post it here for future reference. The cref (code reference) &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2009/11/24/xml-comments/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Today I was looking for a reference for the proper notation for the <tt>cref</tt> attribute of the <tt>&lt;see cref="..."&gt;</tt> C# XML comment tag, and after finding it, I thought I&#8217;d post it here for future reference.</p>
<p>
The <tt>cref</tt> (<em>code reference</em>) tag is prefaced by a single character, then a colon, followed by the reference in question.  The character defines what is being referenced, as defined by the table below.</p>
<table>
<thead>
<tr>
<th>Character</td>
<th>Description</td>
</tr>
<tr>
</thead>
<tbody>
<td>N</td>
<td>Namespace</td>
</tr>
<tr>
<td>T</td>
<td>Type (class, interface, struct, enum, delegate)</td>
</tr>
<tr>
<td>F</td>
<td>Field (member variable, constant, etc)</td>
</tr>
<tr>
<td>P</td>
<td>Property (including indexers and indexed properties)</td>
</tr>
<tr>
<td>M</td>
<td>Method (including special methods like constructors &ndash; <tt>#ctor</tt> &ndash; operators, etc)</td>
</tr>
<tr>
<td>E</td>
<td>Event</td>
</tr>
<tr>
<td>!</td>
<td>Error string</td>
</tr>
</tbody>
</table>
<p>The full reference for the prefixes is available <a href="http://msdn.microsoft.com/en-us/library/fsbx0t7x.aspx">here</a>, with the full XML comment documentation available <a href="http://msdn.microsoft.com/en-us/library/b2s063f7.aspx">here</a>.  The Code Project also has an <a href="http://www.codeproject.com/KB/XML/csharpcodedocumentation.aspx">article on the topic</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2009/11/24/xml-comments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.NET Order of Initialization</title>
		<link>http://softwareblog.morlok.net/2008/11/03/net-order-of-initialization/</link>
		<comments>http://softwareblog.morlok.net/2008/11/03/net-order-of-initialization/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 16:42:39 +0000</pubDate>
		<dc:creator>Warlock</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[constructor]]></category>

		<guid isPermaLink="false">http://softwareblog.morlok.net/2008/11/03/net-order-of-initialization/</guid>
		<description><![CDATA[A while back I ran across this article about the dangers of calling a virtual method from the constructor of an inheritable class. The problem comes from the fact that if a new class is derived from the class with &#8230;<p class="read-more"><a href="http://softwareblog.morlok.net/2008/11/03/net-order-of-initialization/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>A while back I ran across <a href="http://www.dev102.com/2008/07/08/call-virtual-functions-from-constructors/">this article</a> about the dangers of calling a virtual method from the constructor of an inheritable class. The problem comes from the fact that if a new class is derived from the class with the virtual method call in its constructor (and overrides the virtual method), the base class will be calling the derived method on the child class, prior to the child class&#8217; constructor running to completion.</p>
<p>As an extension to the code example to the aforementioned article, I&#8217;ve created an example that can a great interview question for understanding the order in which things are initialized.  Consider the following set of classes (C#):</p>
<pre name="code" class="c-sharp">
public class StringLogger
{
	public static string GetString(string s)
	{
		Console.WriteLine(s);
		return s;
	}
}

public class BaseType
{
	private string baseString
		= StringLogger.GetString("Base string");
	private static string baseStaticString
		= StringLogger.GetString("Base static string");

	static BaseType()
	{
		Console.WriteLine("Base static constructor");
	}

	public BaseType()
	{
		Console.WriteLine("Base instance constructor");
		DoSomething();
	}

	public virtual void DoSomething()
	{
		Console.WriteLine("Base DoSomething");
	}
}

public class DerivedType : BaseType
{
	private string derivedString
		= StringLogger.GetString("Derived string");
	private static string derivedStaticString
		= StringLogger.GetString("Derived static string");

	static DerivedType()
	{
		Console.WriteLine("Derived static constructor");
	}

	public DerivedType()
	{
		Console.WriteLine("Derived instance constructor");
	}

	public override void DoSomething()
	{
		Console.WriteLine("Derived DoSomething");
	}
}
</pre>
<p>There is a base class and a derived class, and a utility class that is used to log to the console when one of the string members of one of the other classes is initialized.  Now consider creating an instance of the derived class:</p>
<pre name="code" class="c-sharp">
public class MainClass
{
	public static void Main()
	{
		Console.WriteLine("Starting...");
		DerivedType d = new DerivedType();
		Console.WriteLine("Done.");
		Console.ReadLine();
	}
}
</pre>
<p>What will be the output to the console as the DerivedType class is instanciated?</p>
<p><span id="more-45"></span></p>
<pre>
Starting...
Derived static string
Derived static constructor
Derived string
Base static string
Base static constructor
Base string
Base instance constructor
Derived DoSomething
Derived instance constructor
Done.
</pre>
<p> What&#8217;s important to know here is the order in which things happen:
<ol>
<li>Static members of a class are initialized (first time a class is accessed)</li>
<li>Static constructor of a class is called (first time a class is accessed)</li>
<li>Instance members initialized</li>
<li>Instance constructor called</li>
</ol>
<p>What may be surprising in the above output is that the members of the derived class (both static and instance) are initialized before anything in the base class.  This is because the base class isn&#8217;t accessed until the derived constructor starts to run, which happens late in the above sequence of events.</p>
]]></content:encoded>
			<wfw:commentRss>http://softwareblog.morlok.net/2008/11/03/net-order-of-initialization/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

