Tuesday, January 24, 2012

How to Reset/Remove read only property values in C#


How to remove query string from the url.
Example: In this example Query String value is setting to the null then it is removed from the query string:
            PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            // make collection editable
            if (HttpContext.Current.Request != null)
            {
                // remove
                isreadonly.SetValue(HttpContext.Current.Request.QueryString, false,             null);
                HttpContext.Current.Request.QueryString.Set(keyname,null);
                HttpContext.Current.Request.QueryString.Remove(keyname);
            }

Note: PropertyInfo class will exist under “System.Reflection” namespace

No comments:

Post a Comment