Tuesday, March 2, 2010
Reflection performance
http://msdn.microsoft.com/en-us/magazine/cc163759.aspx
http://www.drdobbs.com/windows/184416570
Basically, to speed up the reflection, you should do this:
1. Cache the object instance that you have created and reuse it later.
2. Using Interface or delegate to invoke the method instead of MethodInfo.Invoke.
3. Create the assembly dynamically using Reflection.Emit.
Monday, January 18, 2010
global.asax - rewriting the URL
http://localhost/myWebsite/beers
Instead, there are a few ways to achieve it. The easiest way is to use the "urlMappings" in the web.config file. But, this is good for the website that does not update frequently.
  <system.web>
    <urlMappings>
      <add url="~/beers" mappedUrl="~/article_list.aspx?id=1111"/>
    </urlMappings>
  </system.web>
If you have a website that requires to publish the contents that stores in the database, then, you might wonder how to achieve the same result. So, the URL mapping feature can't meet the requirement.
Two ways to achieve this:
(1) you may create a class that inherits from IHttpModule OR
(2) add global.asax to the website and then override BeginRequest method.
Sample code
protected void Application_BeginRequest(object sender, EventArgs e)
{
try
{
// get the requested URL.
string s = this.Request.RawUrl.ToString();
// get the current website url including the virtual directory.
string current_website = (String)Application["current_website"];
if (String.IsNullOrEmpty(current_website))
{
current_website = VirtualPathUtility.ToAbsolute("~/");
Application["current_website"] = current_website;
}
// remove the virtual directory. So, only the requested file name will be in the string.
string s2 = s.Replace(current_website, "");
// if the string contains "." (ie, file extension dot)
// or "/" (ie, the folder separator), exit the checking.
if ((s2.Contains("/"))
|| (s2.Contains("."))
)
{
return;
}
else
{
// Now, we need to search for the string in the database that matches the requested url.
// If found the record, we will rewrite the url. The new URL will not reflect in the client browser.
if (s2.CompareTo("beers") == 0)
{
string url = "~/article.aspx?id=1111";
((HttpApplication)sender).Context.RewritePath(url);
}
}
}
catch (Exception ex)
{
// exception handler goes here.
}
}
Wednesday, November 4, 2009
How to find out the current path/virtual directory of a website?
- VirtualPathUtility.IsAbsolute() will returns false. Meaning, once the URL contains the "~" symbol, it becomes relative. So, VirtualPathUtility.IsAppRelative() will return true.
- For ASP.NET to translate this URL into a runtime URL, we have to call VirtualPathUtility.ToAbsolute(). The result will be "/images/ok.jpg" and "/news/images/ok.jpg" for the above mentioned situation respectively.
- To compute the physical path, you have to call AppDomain.CurrentDomain.BaseDirectory + relative path (you have to remove the "~" symbol and replace "/" with "\").
Thursday, January 22, 2009
Activator.CreateInstance
To load the assembly from GAC:
System.Reflection.AssemblyName asmName = new System.Reflection.AssemblyName(name);
System.Reflection.Assembly asm = System.Reflection.Assembly.Load(asmName);
object obj = asm.CreateInstance(typeName);
References:
(1)
http://blogs.msdn.com/haibo_luo/archive/2005/11/17/494009.aspx
(2)
Q&A of Activator.CreateInstance
http://bytes.com/groups/net-c/586685-activator-createinstance-question
(3)
http://mironabramson.com/blog/post/2008/08/Fast-version-of-the-ActivatorCreateInstance-method-using-IL.aspx
(4)
http://blog.lozanotek.com/archive/2006/03/22/8311.aspx
Tuesday, January 20, 2009
How to upload file using WebClient
System.Net.WebClient cli = new System.Net.WebClient();
cli.UploadFile(url,
"POST",
file_name);
Reference:
http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(VS.71).aspx
http://www.developerfusion.com/forum/thread/38843/
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115
Wednesday, January 14, 2009
Handling Exception
http://www.yoda.arachsys.com/csharp/exceptions.html
When to throw exception?
http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx
Singleton
http://www.yoda.arachsys.com/csharp/singleton.html
Sample code extracted from the above URL :
public sealed class Singleton
{
static Singleton instance=null;
static readonly object lock_obj = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
lock (lock_obj)
{
if (instance==null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}