<?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>Stephen Lacy &#187; C#</title>
	<atom:link href="http://www.lacy.ie/tag/c/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lacy.ie</link>
	<description>Impressions as an ASP.Net Developer</description>
	<lastBuildDate>Mon, 05 Jul 2010 09:26:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Redirecting using strong types in an asp.net web application project</title>
		<link>http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project</link>
		<comments>http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project#comments</comments>
		<pubDate>Wed, 13 Jan 2010 18:45:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Resharper]]></category>
		<category><![CDATA[Web Application Projects]]></category>
		<category><![CDATA[Web Forms]]></category>

		<guid isPermaLink="false">http://www.lacy.ie/?p=249</guid>
		<description><![CDATA[Update, I recommend also reading the following post after you read this one if you plan on using this within .Net 4: http://www.lacy.ie/updating-redirection-using-strong-types-to-net-4 It recently began to annoy me that every time I want to redirect to a page I have to use a string. Not a big deal I know but when you have broken [...]


Related posts:<ol><li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/jquery-grid-plugin-jqgrid-with-asp-net-web-forms' rel='bookmark' title='Permanent Link: jQuery Grid Plugin (JqGrid) with ASP.Net Web Forms'>jQuery Grid Plugin (JqGrid) with ASP.Net Web Forms</a> <small>Get JqGrid here: http://www.trirand.com/blog/ So far I&#8217;ve seen two implementations...</small></li>
<li><a href='http://www.lacy.ie/handling-relative-paths-and-debug-mode-with-jquery-in-asp-net-web-forms' rel='bookmark' title='Permanent Link: Handling Relative Paths and debug mode with Jquery in ASP.Net Web Forms'>Handling Relative Paths and debug mode with Jquery in ASP.Net Web Forms</a> <small>I had a problem where I wanted to use the...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Update, I recommend also reading the following post after you read this one if you plan on using this within .Net 4: <a href="http://www.lacy.ie/updating-redirection-using-strong-types-to-net-4">http://www.lacy.ie/updating-redirection-using-strong-types-to-net-4</a></p>
<p>It recently began to annoy me that every time I want to redirect to a page I have to use a string. Not a big deal I know but when you have broken links it can reflect badly on you as a developer, so I began to look for some solution that would allow for the following.</p>
<ul>
<li>Refactoring: I need to be able to move a page around without having to find every string reference to it</li>
<li>No Reflection: It&#8217;s slow I don&#8217;t want to have to use it when redirects happen often.</li>
<li>Parameters: I want to be able to pass parameters to a function and make that become part of the get request, I want the logic for that to exist on the page being redirected to as that is where the parameters are being used, this will also help with refactoring, the plain ole redirect logic however would just exist on a base class, don&#8217;t want to have to rewrite for every page.</li>
</ul>
<p>I knew the ideal situation would be if I could somehow use the fact that web application projects use the folder hierarchy for its namespace convention and end up being able to redirect to the page ~/Users/UserDetails.aspx?UserId=14 with the following command Users.UserDetails.Redirect(14);</p>
<p>Here is how I did it:<span id="more-249"></span></p>
<p>I have a web application project, let&#8217;s call it MyProject.Web</p>
<p>I have two pages called UserDetails.aspx and UserList.aspx in the folder Users.</p>
<p>They both inherit from the following base class:</p>
<div id="_mcePaste">
<blockquote>
<div id="_mcePaste">namespace MyProject.UI</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">/// &lt;summary&gt;</div>
<div id="_mcePaste">/// Summary description for BasePage</div>
<div id="_mcePaste">/// &lt;/summary&gt;</div>
<div id="_mcePaste">public abstract class BasePage&lt;T&gt;:Page where T : Page</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">public static String GetUrl()</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">return &#8220;~/&#8221; + typeof(T).ToString().Replace(&#8220;MyProject.Web.&#8221;, &#8220;&#8221;).Replace(&#8216;.&#8217;, &#8216;/&#8217;) + &#8220;.aspx&#8221;;</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">public static String GetUrl(String parameters)</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">return GetUrl() + &#8220;?&#8221; + parameters;</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">public static void Redirect()</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">HttpContext.Current.Response.Redirect(GetUrl());</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">public static void Redirect(String parameters)</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">HttpContext.Current.Response.Redirect(GetUrl(parameters));</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">public static void Redirect(Boolean endResponse)</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">HttpContext.Current.Response.Redirect(GetUrl(),endResponse);</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">public static void Redirect(String parameters,Boolean endResponse)</div>
<div id="_mcePaste">{</div>
<div id="_mcePaste">HttpContext.Current.Response.Redirect(GetUrl(parameters), endResponse);</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">}</div>
</blockquote>
</div>
<p>They follow this pattern:</p>
<blockquote><p>namespace MyProject.Web.Users</p>
<p>{</p>
<p><span style="white-space: pre;"> </span>public partial class UserDetails: BasePage&lt;UserDetails&gt;</p>
<p><span style="white-space: pre;"> </span>{</p>
<p><span style="white-space: pre;"> </span>public static void Redirect(int userId)</p>
<p><span style="white-space: pre;"> </span>{</p>
<p><span style="white-space: pre;"> </span>Redirect(&#8220;UserId=&#8221; + userId.ToString());</p>
<p><span style="white-space: pre;"> </span>}</p>
<p><span style="white-space: pre;"> </span>}</p>
<p>}</p></blockquote>
<p>Now because UserDetails and UserList are in the same folder I can simply call UserDetails.Redirect(selectedUserId) on the UserList and UserList.Redirect() on the UserDetails.<br />
The only real disadvantage is that it could confuse people who discover a Redirect method on their current page that they can&#8217;t use to make it to another page.<br />
However it should be abundantly clear to everyone what you are doing when they see it already written.<br />
Also every time you move a class, remove or add a parameter, it will break on compile time not at run time.</p>
<p>Comments and feedback welcome.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fredirecting-using-strong-types-in-an-asp-net-web-application-project&amp;linkname=Redirecting%20using%20strong%20types%20in%20an%20asp.net%20web%20application%20project"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/jquery-grid-plugin-jqgrid-with-asp-net-web-forms' rel='bookmark' title='Permanent Link: jQuery Grid Plugin (JqGrid) with ASP.Net Web Forms'>jQuery Grid Plugin (JqGrid) with ASP.Net Web Forms</a> <small>Get JqGrid here: http://www.trirand.com/blog/ So far I&#8217;ve seen two implementations...</small></li>
<li><a href='http://www.lacy.ie/handling-relative-paths-and-debug-mode-with-jquery-in-asp-net-web-forms' rel='bookmark' title='Permanent Link: Handling Relative Paths and debug mode with Jquery in ASP.Net Web Forms'>Handling Relative Paths and debug mode with Jquery in ASP.Net Web Forms</a> <small>I had a problem where I wanted to use the...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Mock the HttpContext in asp.net</title>
		<link>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net</link>
		<comments>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net#comments</comments>
		<pubDate>Tue, 13 Oct 2009 10:18:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[httpcontext]]></category>
		<category><![CDATA[mocking]]></category>

		<guid isPermaLink="false">http://www.lacy.ie/?p=232</guid>
		<description><![CDATA[I find mocking the http context rather difficult, you can&#8217;t inherit from it and making a wrapper class is a lot of work. Thankfully someone came up with a solution, unfortunately the link they included with the code is dead and I can&#8217;t seem to remember where I got the code, if anyone finds out [...]


Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I find mocking the http context rather difficult, you can&#8217;t inherit from it and making a wrapper class is a lot of work. Thankfully someone came up with a solution, unfortunately the link they included with the code is dead and I can&#8217;t seem to remember where I got the code, if anyone finds out where it comes from please comment and I&#8217;ll add it to the post.</p>
<p><span id="more-232"></span></p>
<pre>using System;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Web;
using System.Web.Hosting;
using System.Web.SessionState;

public sealed class MockHttpContext
{
    // NOTE: This code is based on the following article:
    // http://righteousindignation.gotdns.org/blog/archive/2004/04/13/149.aspx
    private const string ContextKeyAspSession = "AspSession";
    private const string ThreadDataKeyAppPath = ".appPath";
    private const string ThreadDataKeyAppPathValue = "c:\\inetpub\\wwwroot\\webapp\\";
    private const string ThreadDataKeyAppVPath = ".appVPath";
    private const string ThreadDataKeyAppVPathValue = "/webapp";
    private const string WorkerRequestPage = "default.aspx";

    private HttpContext context = null;

    private MockHttpContext()
        : base()
    {
    }

    public MockHttpContext(bool isSecure)
        : this()
    {
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppPath, MockHttpContext.ThreadDataKeyAppPathValue);
        Thread.GetDomain().SetData(
            MockHttpContext.ThreadDataKeyAppVPath, MockHttpContext.ThreadDataKeyAppVPathValue);
        SimpleWorkerRequest request = new WorkerRequest(MockHttpContext.WorkerRequestPage,
            string.Empty, new StringWriter(), isSecure);
        this.context = new HttpContext(request);

        HttpSessionStateContainer container = new HttpSessionStateContainer(
            Guid.NewGuid().ToString("N"), new SessionStateItemCollection(), new HttpStaticObjectsCollection(),
            5, true, HttpCookieMode.AutoDetect, SessionStateMode.InProc, false);

        HttpSessionState state = Activator.CreateInstance(
             typeof(HttpSessionState),
             BindingFlags.Public | BindingFlags.NonPublic |
             BindingFlags.Instance | BindingFlags.CreateInstance,
             null,
             new object[] { container }, CultureInfo.CurrentCulture) as HttpSessionState;
        this.context.Items[ContextKeyAspSession] = state;
    }

    public HttpContext Context
    {
        get
        {
            return this.context;
        }
    }

    private class WorkerRequest : SimpleWorkerRequest
    {
        private bool isSecure = false;

        public WorkerRequest(string page, string query, TextWriter output, bool isSecure)
            : base(page, query, output)
        {
            this.isSecure = isSecure;
        }

        public override bool IsSecure()
        {
            return this.isSecure;
        }
    }
}</pre>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fhow-to-mock-the-httpcontext-in-asp-net&amp;linkname=How%20to%20Mock%20the%20HttpContext%20in%20asp.net"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Call MethodInfo.Invoke with an out parameter</title>
		<link>http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter</link>
		<comments>http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter#comments</comments>
		<pubDate>Fri, 21 Aug 2009 19:19:21 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[System.Reflection]]></category>

		<guid isPermaLink="false">http://lacy.ie/?p=168</guid>
		<description><![CDATA[Taken from http://www.galcho.com/Blog/P&#8230; Here is how to call a method with an out parameter using MethodInfo.Invoke, in this case the out parameter is the last one (int). int parNum = 0; string parText = &#8220;99&#8243;; object[] methodParms = new object[] { parText, parNum }; MethodInfo methInfo = parNum.GetType().GetMethod(&#8220;TryParse&#8221;, new Type[] { typeof(string), typeof(int).MakeByRefType() }); methInfo.Invoke(null, [...]


No related posts.

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Taken from <a href="http://www.galcho.com/Blog/PermaLink.aspx?guid=2447b6ad-83e2-45ec-8257-672c42bba78b">http://www.galcho.com/Blog/P&#8230;</a></p>
<p>Here is how to call a method with an out parameter using MethodInfo.Invoke, in this case the out parameter is the last one (int).</p>
<p>int parNum = 0;<br />
string parText = &#8220;99&#8243;;<br />
object[] methodParms = new object[] { parText, parNum };</p>
<p>MethodInfo methInfo = parNum.GetType().GetMethod(&#8220;TryParse&#8221;, new Type[] { typeof(string),<strong> </strong><strong>typeof(int).MakeByRefType()</strong> });<br />
methInfo.Invoke(null, methodParms);<br />
<strong> parNum = (int)methodParms[1];</strong><br />
Console.WriteLine(&#8220;Parsed number:{0}&#8221;, parNum);</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fcall-methodinfo-invoke-with-an-out-parameter&amp;linkname=Call%20MethodInfo.Invoke%20with%20an%20out%20parameter"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ResolveUrl in your Business Logic</title>
		<link>http://www.lacy.ie/resolveurl-in-your-business-logic</link>
		<comments>http://www.lacy.ie/resolveurl-in-your-business-logic#comments</comments>
		<pubDate>Sun, 15 Mar 2009 02:04:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[N-Tier]]></category>

		<guid isPermaLink="false">http://lacy.ie/?p=118</guid>
		<description><![CDATA[I&#8217;ve recently started moving a large amount of code on the project I&#8217;m working on from the presentation layer into a business logic layer. One of the problems I found was trying to link to other pages on the site without know where the application would be located or if it would be http or [...]


Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
<li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter' rel='bookmark' title='Permanent Link: Call MethodInfo.Invoke with an out parameter'>Call MethodInfo.Invoke with an out parameter</a> <small>Taken from http://www.galcho.com/Blog/P&#8230; Here is how to call a method...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently started moving a large amount of code on the project I&#8217;m working on from the presentation layer into a business logic layer. One of the problems I found was trying to link to other pages on the site without know where the application would be located or if it would be http or https.</p>
<p>I picked up a bit of code from http://www.west-wind.com/Weblog/posts/154812.aspx to handle it, but had to make some changes to make it work in a more generic fashion. My change is small and in bold.</p>
<p><span id="more-118"></span></p>
<blockquote><p>/// &lt;summary&gt;</p>
<p>/// Returns a site relative HTTP path from a partial path starting out with a ~.</p>
<p>/// Same syntax that ASP.Net internally supports but this method can be used</p>
<p>/// outside of the Page framework.</p>
<p>///</p>
<p>/// Works like Control.ResolveUrl including support for ~ syntax</p>
<p>/// but returns an absolute URL.</p>
<p>/// &lt;/summary&gt;</p>
<p>/// &lt;param name=&#8221;originalUrl&#8221;&gt;Any Url including those starting with ~&lt;/param&gt;</p>
<p>/// &lt;returns&gt;relative url&lt;/returns&gt;</p>
<p>public static string ResolveUrl(string originalUrl)<br />
{</p>
<p>if (originalUrl == null)</p>
<p>return null;</p>
<p>// *** Absolute path &#8211; just return</p>
<p>if (originalUrl.IndexOf(&#8220;://&#8221;) != -1)</p>
<p>return originalUrl;</p>
<p>// *** Fix up image path for ~ root app dir directory</p>
<p>if (originalUrl.StartsWith(&#8220;~&#8221;))<br />
{</p>
<p>string newUrl = &#8220;&#8221;;</p>
<p>if (HttpContext.Current != null)</p>
<p>newUrl = <strong>HttpContext.Current.Request.Url.Scheme + &#8220;://&#8221; + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + originalUrl.Substring(1).Replace(&#8220;//&#8221;, &#8220;/&#8221;);</strong></p>
<p>else</p>
<p>// *** Not context: assume current directory is the base directory</p>
<p>throw new ArgumentException(&#8220;Invalid URL: Relative URL not allowed.&#8221;);</p>
<p>// *** Just to be sure fix up any double slashes</p>
<p>return newUrl;</p>
<p>}</p>
<p>return originalUrl;</p>
<p>}</p></blockquote>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fresolveurl-in-your-business-logic&amp;linkname=ResolveUrl%20in%20your%20Business%20Logic"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
<li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter' rel='bookmark' title='Permanent Link: Call MethodInfo.Invoke with an out parameter'>Call MethodInfo.Invoke with an out parameter</a> <small>Taken from http://www.galcho.com/Blog/P&#8230; Here is how to call a method...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/resolveurl-in-your-business-logic/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Method Overloading in C#</title>
		<link>http://www.lacy.ie/generic-method-overloading-in-c</link>
		<comments>http://www.lacy.ie/generic-method-overloading-in-c#comments</comments>
		<pubDate>Tue, 17 Feb 2009 12:54:46 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ASP.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Overloading]]></category>
		<category><![CDATA[System.Reflection]]></category>

		<guid isPermaLink="false">http://lacy.ie/?p=94</guid>
		<description><![CDATA[Say you have an outer method: public OuterMethod(object x) { InnerMethod(x); } and an inner method with these overloads public InnerMethod(object x) { //Generic Actions } public InnerMethod(Person x) { //Person Specific Actions } public InnerMethod(Dog x) { // Dog Specific Actions } How do you make it so that when the outer method calls [...]


Related posts:<ol><li><a href='http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter' rel='bookmark' title='Permanent Link: Call MethodInfo.Invoke with an out parameter'>Call MethodInfo.Invoke with an out parameter</a> <small>Taken from http://www.galcho.com/Blog/P&#8230; Here is how to call a method...</small></li>
<li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>Say you have an outer method:<br />
<code>public OuterMethod(object x)<br />
{<br />
InnerMethod(x);<br />
}</code><br />
and an inner method with these overloads<br />
<code><br />
public InnerMethod(object x)<br />
{<br />
//Generic Actions<br />
}<br />
public InnerMethod(Person x)<br />
{<br />
//Person Specific Actions<br />
}<br />
public InnerMethod(Dog x)<br />
{<br />
// Dog Specific Actions<br />
}<br />
</code><br />
How do you make it so that when the outer method calls the inner method it completes the specific actions as well as the generic ones that allows you to add an overload without editing any other function (useful when working on a team). Well here is the answer&#8230;<span id="more-94"></span></p>
<p>System.Reflections allows you to search for the relevant method by descibing what it looks like<br />
<code><br />
using System.Reflection;<br />
...<br />
public InnerMethod(Object x)<br />
{<br />
MethodInfo methodInfo = entity.GetType().GetMethod( "InnerMethod", new Type[] { x.GetType() });<br />
if(methodInfo != null)<br />
{<br />
methodInfo.invoke(this,Invoke(null, new object[] { x }) ;<br />
}<br />
// Generic Actions<br />
}<br />
</code></p>
<p>Now every time you add or remove a method, you only have to add or remove it in one place.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.lacy.ie%2Fgeneric-method-overloading-in-c&amp;linkname=Generic%20Method%20Overloading%20in%20C%23"><img src="http://www.lacy.ie/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>

<p>Related posts:<ol><li><a href='http://www.lacy.ie/call-methodinfo-invoke-with-an-out-parameter' rel='bookmark' title='Permanent Link: Call MethodInfo.Invoke with an out parameter'>Call MethodInfo.Invoke with an out parameter</a> <small>Taken from http://www.galcho.com/Blog/P&#8230; Here is how to call a method...</small></li>
<li><a href='http://www.lacy.ie/how-to-mock-the-httpcontext-in-asp-net' rel='bookmark' title='Permanent Link: How to Mock the HttpContext in asp.net'>How to Mock the HttpContext in asp.net</a> <small>I find mocking the http context rather difficult, you can&#8217;t...</small></li>
<li><a href='http://www.lacy.ie/redirecting-using-strong-types-in-an-asp-net-web-application-project' rel='bookmark' title='Permanent Link: Redirecting using strong types in an asp.net web application project'>Redirecting using strong types in an asp.net web application project</a> <small>Update, I recommend also reading the following post after you...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.lacy.ie/generic-method-overloading-in-c/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->