C#指定一个具有特定名称的变量

C#指定一个具有特定名称的变量,c#,variables,C#,Variables,我想结合一个简单的类,从发送到我的应用程序的HTTP请求中挑选QueryString变量。变量的顺序并不总是相同的,但它们总是存在的。我想给我的类中的变量分配请求中相应变量的值,但我不知道怎么做 代码片段: MyHTTPRequest ar = new MyHTTPRequest("GET /submit.php?variableOne=value&variableTwo=someothervalue HTTP/1.1"); // Leaving out the rest for now

我想结合一个简单的类,从发送到我的应用程序的HTTP请求中挑选QueryString变量。变量的顺序并不总是相同的,但它们总是存在的。我想给我的类中的变量分配请求中相应变量的值,但我不知道怎么做

代码片段:

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.
            }
        }
    }

}
MyHTTPRequest ar=newmyhttprequest(“GET/submit.php?variableOne=value&variableTwo=someothervalue HTTP/1.1”);//剩下的暂时不考虑
类MyHTTPRequest
{
公共字符串变量1;
公共字符串变量2;
公共字符串变量三;
私有字符串[]属性={“variableOne”、“variableTwo”、“variableTwo”};//我知道它们实际上不是属性
公共MyHTTPRequest(字符串请求)
{
string[]variablePars=request.Substring(16,request.Length-24).Split('&');//variablePars现在包含“variableOne=value”和“variableTwo=someothervalue”
foreach(variablePars中的字符串variablePar)
{
if(properties.Contains(variablePar.Split('=')[0])///variableOne、variableTwo、variableTwo
{
//为正确的变量赋值
=variablePar.Split('=')[1];//我不知道如何完成此操作。variablePar.Split('=')[0]应该包含变量的名称。
}
}
}
}
有什么意见吗


我确信已经存在类似的问题,但我不知道用什么来命名或标记它。

您可以使用反射来执行以下操作:

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

if (property != null)
{
    property.SetValue(theTargetObject, theValue);
}
在这里,我们首先得到定义属性的类的属性(通过反射和属性名)。如果找到具有所需名称的属性,则在目标对象上设置属性值

或使用字段而不是属性:

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

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

只有当您正在设置值的目标对象纯粹是DTO,其中所有字段\属性都将由查询字符串值填充时,此技术才是真正安全的(从其他人评论的安全角度来看)。这是我回答问题的最初观点。如果目标对象包含不应根据查询字符串值设置的字段,则不要使用此技术,因为可能会删除不应根据查询字符串值设置的字段\属性


如果您的目标对象是DTO,那么上面的操作就可以了。我假设情况就是这样。

您可以使用反射按如下方式执行此操作:

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

if (property != null)
{
    property.SetValue(theTargetObject, theValue);
}
在这里,我们首先得到定义属性的类的属性(通过反射和属性名)。如果找到具有所需名称的属性,则在目标对象上设置属性值

或使用字段而不是属性:

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

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

只有当您正在设置值的目标对象纯粹是DTO,其中所有字段\属性都将由查询字符串值填充时,此技术才是真正安全的(从其他人评论的安全角度来看)。这是我回答问题的最初观点。如果目标对象包含不应根据查询字符串值设置的字段,则不要使用此技术,因为可能会删除不应根据查询字符串值设置的字段\属性


如果您的目标对象是DTO,那么上面的操作就可以了。我假设是这样。

使用System.Web.HttpUtilityClass.ParseQueryString。它返回一个NameValueCollection,就像在ASP.NET中使用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"];

使用System.Web.HttpUtilityClass.ParseQueryString。它返回一个NameValueCollection,就像在ASP.NET中使用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"];

为什么不扭转局面呢

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); 
  }
} 

为什么不扭转局面呢

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); 
  }
} 


我使用TcpClient来获取请求,我真的很想用我的方式解决这个问题。:-)我得到的印象是,您正在询问服务器如何从请求中提取querystring值。我看到其他人也这样做了(用asp.net标记了它)。这是一个C#应用程序,从本地机器上的另一个程序获取数据包。英语不是我的母语,这个问题可能太模糊了。为什么不扔掉私有字段,用字典替换它们呢?我使用TcpClient获取请求,我真的很想用我的方式解决这个问题。:-)我得到的印象是,您正在询问服务器如何从请求中提取querystring值。我看到其他人也这样做了(用asp.net标记了它)。这是一个C#应用程序,从本地机器上的另一个程序获取数据包。英语不是我的母语,这个问题可能太模糊了。为什么不扔掉私有字段,用字典代替它们呢?你可能想研究如何使用反射。你可能想研究如何使用反射。对,除了这一点,传递一个没有相应属性的参数的那一刻就中断了。您可以在设置之前通过检查来解决此问题,但我认为更通用的解决方案是删除字段并使用集合。@Steven搞错了-这就是“if(property!=null)”的作用。这可能会成为一场安全噩梦。查询字符串不应具有直接设置类参数的效果。@drharris我认为您的注释应属于问题的注释,而不是我的答案的注释。不,我具体说的是使用反射直接设置参数。这个问题本身是有效的,其他答案提供了比反射好得多的解决方案。对,除了传递一个没有相应属性的参数这一刻中断。您可以在设置之前通过检查来解决此问题,但我认为更通用的解决方案是删除字段并使用集合。@Steven搞错了-这就是“if(property!=null)”的作用。这可能会成为一个安全问题