Wednesday, February 17, 2010

Extension Methods / Visitor Pattern

Extension method is new to C#.Its allow you to add new method to the existing class.

Description
1.Extension method allows you to add new method to the existing class without modifying the code, recompiling or modifying the original code.
2.Extension method are special kind of static method but they are called using instance method syntax. Their first parameter specify which type the method operate on, and the parameter is precede by “this” modifier.
3.Extension method are only in scope when you explicitly import the namespace into your source code with “using” directive.
For Example in string object there is no method call “Reverse()” function .But if you need to extend the string ….


Sample Code


C# Code


namespace ExtensionMethods
{
public static class ExtensionsClass
{
public static string Reverse(this String strReverse)
{
char[] charArray = new char[strReverse.Length];
int len = strReverse.Length - 1;
for (int i = 0; i <= len; i++)
{
charArray[i] = strReverse[len-i];
}
return new string(charArray);
}
}
}
C# Code


How to use?

string s = "Hello Extension Methods";
string strReverse = s.Reverse ();

Here "s" is nothing but the parameter of Reverse() method.



Conclusion
Extension method is nothing but the Visitor pattern.

Visitor Pattern:-
Visitor pattern allows us to change the class structure without changing the actual class. Its way of separating the logic and algorithm from the current data structure. Due to this you can add new logic to the current data structure without altering the structure. Second you can alter the structure without touching the logic

Thursday, February 4, 2010

Force IE7 Compatibility Mode in IE8 with IIS settings

Force IE7 Compatibility Mode in IE8 with IIS settings
There a ton of examples on the web of how you can force IE8 into IE7 compatibility mode using a meta tag in the header.
This tag needs to be first in the (before any css):


That really stinks if you need to add that to a lot of pages or sites. It’s much easier just to add the header as real HTTP Header via IIS. This can be done via IIS 6 or 7.

IIS 6
Go to the website, bring up the properties for it, and click on the HTTP Headers tab.


HTTP Headers tab of an IIS 6 Website

Then, add a new header as below:
Add custom HTTP Header

IIS 7
IIS 7 is much the same. Just go to the site and click on “HTTP Response Headers”.

IIS 7 Website Properties – IIS Section
Then, just add the header:
Add custom HTTP Header
Incidentally, this just sets a value in the web.config, as below:

< httpProtocol >
< CustomHeaders >
< add name="X-UA-Compatible" value="IE7" / >
< /CustomHeaders >
< /httpProtcol >
< /System.webServer >