Here is my question: Request.QueryString on asp.net is typed as
NameValueCollection. But there is strange behavoir-- I can simply use
Request.QueryString , or public function ToString() to return a
well-formatted string, ie. param1=1¶m2=2..., but when use the same code
on other NameValueCollection object, it only return the class name.
I'm wondering 1. how .NET framework achieve this? by adding logic to check
if it's QueryString object ? 2. Is there a simple way,other than looping thru
kind of labor work, to convert a NameValueCollection object to a
well-formatted string ?
Thanks.
Cowboy (Gregory A. Beamer) - MVP - 31 Aug 2005 14:39 GMT
The QueryString is cast out as an HttpValueCollection (from the Get on
HttpRequest):
if (this._queryString == null)
{
this._queryString = new HttpValueCollection();
if (this._wr != null)
{
this.FillInQueryStringCollection();
}
this._queryString.MakeReadOnly();
}
ToString() is overloaded in the HttpValueCollection object to unwind the
name value collection:
internal virtual string ToString(bool urlencoded)
{
StringBuilder builder1 = new StringBuilder();
int num1 = this.Count;
for (int num2 = 0; num2 < num1; num2++)
{
string text3;
string text1 = this.GetKey(num2);
if (urlencoded)
{
text1 = HttpUtility.UrlEncodeUnicode(text1);
}
string text2 = ((text1 != null) && (text1.Length > 0)) ? (text1
+ "=") : "";
ArrayList list1 = (ArrayList) base.BaseGet(num2);
int num3 = (list1 != null) ? list1.Count : 0;
if (num2 > 0)
{
builder1.Append('&');
}
if (num3 == 1)
{
builder1.Append(text2);
text3 = (string) list1[0];
if (urlencoded)
{
text3 = HttpUtility.UrlEncodeUnicode(text3);
}
builder1.Append(text3);
}
else if (num3 == 0)
{
builder1.Append(text2);
}
else
{
for (int num4 = 0; num4 < num3; num4++)
{
if (num4 > 0)
{
builder1.Append('&');
}
builder1.Append(text2);
text3 = (string) list1[num4];
if (urlencoded)
{
text3 = HttpUtility.UrlEncodeUnicode(text3);
}
builder1.Append(text3);
}
}
}
return builder1.ToString();
}
If you want other Name/Value collections to be the same way, you can inherit
and create your own overload (the code above should be a good start).
NOTE: Used Lutz Roeder's Reflector with the File Dissasembler for the code.

Signature
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
***************************
Think Outside the Box!
***************************
> Here is my question: Request.QueryString on asp.net is typed as
> NameValueCollection. But there is strange behavoir-- I can simply use
[quoted text clipped - 8 lines]
>
> Thanks.