pastapockets
pastapockets

Reputation: 1108

C# Assign a variable that has a certain name

I want to slap a simple class together to pick out QueryString variables from an HTTP request that gets sent to my application. The variables are not always in the same order, but they're always there. I want to assign the variables in my class the values of the corresponding variables from the request, but I have no idea how to do this.

Code snippets:

MyHTTPRequest ar = new MyHTTPRequest("GET /submit.php?variableOne=value&variableTwo=someothervalue HTTP/1.1"); // Leaving out the rest for now


class MyHTTPRequest
{
    public string variableOne;
    public string variableTwo;
    public string variableThree;

    private string[] properties = { "variableOne", "variableTwo", "variableThree" }; // I know they're not actually properties

    public MyHTTPRequest(string request)
    {
        string[] variablePars = request.Substring(16, request.Length - 24).Split('&'); // variablePars now contains "variableOne=value" & "variableTwo=someothervalue"

        foreach (string variablePar in variablePars)
        {
            if (properties.Contains(variablePar.Split('=')[0])) // variableOne, variableTwo, variableThree
            {
                // Assign the correct variable the value
                <???> = variablePar.Split('=')[1]; // I don't know how to pull this one off. variablePar.Split('=')[0] should contain the name of the variable.
            }
        }
    }

}

Any input?

I'm sure a similar question already exists, but I did not know what to titel or tag this with.

Upvotes: 1

Views: 354

Answers (4)

Tim Lloyd
Tim Lloyd

Reputation: 38434

You could use reflection to do this as follows:

PropertyInfo property = YourDtoClass.GetType().GetProperty("ThePropertyName");

if (property != null)
{
    property.SetValue(theTargetObject, theValue);
}

Here we first get the property of the class where your properties are defined (via reflection and the property name). If a property is found with the desired name, we then set the property value on the target object.

Or using fields instead of properties:

FieldInfo field = YourDtoClass.GetType().GetField("theFieldName");

if (field != null)
{
    field.SetValue(theTargetObject, theValue);
}

Update

This technique is only really safe (from a security perspective as others commented) if the target object that you are setting values on is purely a DTO, where all fields\properties are intended to be populated by query string values. This was the original viewpoint of my answer. If the target object contains fields that should not be set from query string values, then do not use this technique, as fields\properties that are not intended to be set from query string values, could be.

If your target object is a DTO, then the above is fine. I am assuming that this is the case.

Upvotes: 3

Jord&#227;o
Jord&#227;o

Reputation: 56457

Why not turn it around?

class MyHTTPRequest {
  public string variableOne { get { return _values["variableOne"]; } }
  public string variableTwo { get { return _values["variableTwo"]; } }
  public string variableThree { get { return _values["variableThree"]; } }
  NameValueCollection _values;
  public MyHTTPRequest(string queryStringText) { 
    _values = HttpUtility.ParseQueryString(queryStringText); 
  }
} 

Upvotes: 3

btlog
btlog

Reputation: 4780

Use the System.Web.HttpUtilityClass.ParseQueryString. It returns a NameValueCollection just like you get in ASP.NET with Request.QueryString.

// assuming you can parse your string to get your string to this format.
string queryStringText = "variableOne=value&variableTwo=someothervalue";

NameValueCollection queryString = 
     HttpUtility.ParseQueryString(queryStringText);

string variable1 = queryString["variableOne"];
string variable2 = queryString["variableTwo"];

Upvotes: 4

Related Questions