Inaccessible .NET types?

joerocket23

Weaksauce
Joined
Sep 15, 2007
Messages
88
So I found a .NET solution on a forum that I want to use, but it is way over my head. When this guy posted this, he said that "It required instances of
types like "HttpServerVarsCollectionEntry", which can't aren't publicly
accessible".

Whenever I try to get this to work, it throws a missing method exception for the HttpServerVarsCollectionEntry call...just like the guy mentioned on the forum. I have spent hours trying to understnad why it is happeneing and how to get around it, but havent found much. Any ideas?

Here is the code I have been trying, and the original forum post here.

Code:
protected override void OnLoad(System.EventArgs e)
{
Response.Write(Request.ServerVariables["SERVER_SOFTWARE"] + "<br />");
SetServerVariable("SERVER_SOFTWARE", "Hax0r 1.1");
Response.Write(Request.ServerVariables["SERVER_SOFTWARE"] + "<br />");
}

protected void SetServerVariable(string key, string newValue)
{
Type tRequest = typeof(HttpRequest);
Type tVar;
Type tServerVars = Request.ServerVariables.GetType();

// Do this call to make sure everything is filled in
string temp = Request.ServerVariables[key];

if (temp != null)
{
// Create new HttpServerVarsCollection
Object[] input = new Object[1];
input[0] = Request;
NameValueCollection vars =
(NameValueCollection)tServerVars.InvokeMember(null,
BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.CreateInstance, null, null,
input);

// Do a BaseGet to get the HttpServerVarsCollectionEntry type
Object[] inputBaseGet = new Object[1];
inputBaseGet[0] = 1;
tVar = tServerVars.InvokeMember("BaseGet", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, Request.ServerVariables, inputBaseGet).GetType();

// Create new HttpServerVarsCollectionEntry object for our
"override"
Object[] inputCreate = new Object[2];
inputCreate[0] = key;
inputCreate[1] = newValue;
Object theVar = tVar.InvokeMember(null, BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance |
BindingFlags.CreateInstance, null, null, inputCreate);

// Set _readOnly to false so that we can modify the collection
Object[] inputReadOnly = new Object[1];
inputReadOnly[0] = false;
tServerVars.InvokeMember("IsReadOnly", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty,
null, Request.ServerVariables, inputReadOnly);

// Remove old object
Object[] inputBaseRemove = new Object[1];
inputBaseRemove[0] = key;
tServerVars.InvokeMember("BaseRemove", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, Request.ServerVariables, inputBaseRemove);

// Add new one
Object[] inputAdd = new Object[2];
inputAdd[0] = key;
inputAdd[1] = theVar;
tServerVars.InvokeMember("BaseAdd", BindingFlags.NonPublic |
BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,
null, Request.ServerVariables, inputAdd);
}
}

Thanks everyone!
 
looks like he's using reflection to get an instance of HttpServerVarsCollectionEntry from System.Web.HttpServerVarsCollectionEntry

you have both using statements correct?
using System.Reflection;
using System.Collections.Specialized;

I would try to mess with this, but its too early and I'm lazy
 
Yeah, using both of those using statements. I should have included that in the code snippet.

I would try to mess with this, but its too early and I'm lazy

No problem, I'll send you a case of 5 Hour Energy.
 
Back
Top