c#将类转换为查询字符串
我正在尝试将应用程序中的一些类/对象转换为查询字符串,例如:c#将类转换为查询字符串,c#,C#,我正在尝试将应用程序中的一些类/对象转换为查询字符串,例如: public class LoginRequest : BaseRequest { public string username { get; set; } public string password { get; set; } public otherclass d { get; set; } } public class otherclass { public string a { get; s
public class LoginRequest : BaseRequest
{
public string username { get; set; }
public string password { get; set; }
public otherclass d { get; set; }
}
public class otherclass
{
public string a { get; set; }
public string b { get; set; }
}
届时将成为:
username=test&password=p&a=123&b=123
我通过以下功能实现了这一点
private string ObjectToQueryString<T>(T obj) where T: class {
StringBuilder sb = new StringBuilder();
Type t = obj.GetType();
var properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
if (p.CanRead)
{
var indexes = p.GetIndexParameters();
if (indexes.Count() > 0)
{
var pp = p.GetValue(obj, new object[] { 1 });
sb.Append(ObjectToQueryString(pp));
}
else
{
//I dont think this is a good way to do it
if (p.PropertyType.FullName != p.GetValue(obj, null).ToString())
{
sb.Append(String.Format("{0}={1}&", p.Name, HttpUtility.UrlEncode(p.GetValue(obj, null).ToString())));
}
else
{
sb.Append(ObjectToQueryString(p.GetValue(obj, null)));
}
}
}
}
return sb.ToString().TrimEnd('&');
}
私有字符串ObjectToQueryString(T obj),其中T:class{
StringBuilder sb=新的StringBuilder();
类型t=obj.GetType();
var properties=t.GetProperties();
foreach(PropertyInfo p in properties)
{
如果(p.CanRead)
{
var index=p.GetIndexParameters();
如果(index.Count()>0)
{
var pp=p.GetValue(obj,新对象[]{1});
sb.Append(ObjectToQueryString(pp));
}
其他的
{
//我认为这不是一个好办法
if(p.PropertyType.FullName!=p.GetValue(obj,null).ToString())
{
sb.Append(String.Format(“{0}={1}&”,p.Name,HttpUtility.UrlEncode(p.GetValue(obj,null.ToString())));
}
其他的
{
sb.Append(ObjectToQueryString(p.GetValue(obj,null));
}
}
}
}
使某人返回字符串('&');
}
但是如果我将一个列表传递到函数中,它还将包括计数和容量属性以及其他我不想要的东西。说它是一个列表
List<otherclass>()
List()
干杯对我来说似乎很简单,检查该类是IEnumerable还是IEnumerator,如果是,则枚举它(而不是反映该特定类)。如果您能解释一下您希望我们如何处理这些结果,那会有所帮助
//username=bob&password=123&a=Cheese&b=Chocolate&a=Cat&b=Dog
public class LoginRequest
{
public string username { get; set; }
public string password { get; set; }
public List<OtherClass> d { get; set; }
}
public class OtherClass
{
public string a { get; set; }
public string b { get; set; }
}
static void Main(string[] args)
{
var request = new LoginRequest
{
username = "bob",
password = "123",
d = new List<OtherClass> { new OtherClass { a = "Cheese", b = "Chocolate" } ,
new OtherClass { a = "Cat", b = "Dog" } }
};
Console.WriteLine(ObjectToQueryString(request));
Console.ReadLine();
}
private static string ObjectToQueryString<T>(T obj) where T : class
{
StringBuilder sb = new StringBuilder();
IEnumerable data = obj as IEnumerable ?? new[] { obj };
foreach (var datum in data)
{
Type t = datum.GetType();
var properties = t.GetProperties();
foreach (PropertyInfo p in properties)
{
if (p.CanRead)
{
var indexes = p.GetIndexParameters();
if (indexes.Count() > 0)
{
var pp = p.GetValue(datum, new object[] { 1 });
sb.Append(ObjectToQueryString(pp));
}
else if (typeof(IEnumerable).IsAssignableFrom(p.PropertyType) && p.PropertyType != typeof(string))
{
sb.Append(ObjectToQueryString(p.GetValue(datum)));
}
else
{
//I dont think this is a good way to do it
if (p.PropertyType.FullName != p.GetValue(datum, null).ToString())
{
//sb.Append(String.Format("{0}={1}&", p.Name, HttpUtility.UrlEncode(p.GetValue(datum, null).ToString())));
sb.Append(String.Format("{0}={1}&", p.Name, p.GetValue(datum, null).ToString()));
}
else
{
sb.Append(ObjectToQueryString(p.GetValue(datum, null)));
}
}
}
}
}
return sb.ToString().TrimEnd('&');
}
//username=bob&password=123&a=Cheese&b=Chocolate&a=Cat&b=Dog
公共类登录请求
{
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公共列表d{get;set;}
}
公共类其他类
{
公共字符串a{get;set;}
公共字符串b{get;set;}
}
静态void Main(字符串[]参数)
{
var请求=新登录请求
{
username=“bob”,
password=“123”,
d=新列表{new OtherClass{a=“Cheese”,b=“Chocolate”},
新的OtherClass{a=“Cat”,b=“Dog”}
};
WriteLine(ObjectToQueryString(request));
Console.ReadLine();
}
私有静态字符串ObjectToQueryString(T obj),其中T:class
{
StringBuilder sb=新的StringBuilder();
IEnumerable data=obj作为IEnumerable??新[]{obj};
foreach(数据中的var数据)
{
类型t=datum.GetType();
var properties=t.GetProperties();
foreach(PropertyInfo p in properties)
{
如果(p.CanRead)
{
var index=p.GetIndexParameters();
如果(index.Count()>0)
{
var pp=p.GetValue(数据,新对象[]{1});
sb.Append(ObjectToQueryString(pp));
}
else if(typeof(IEnumerable).IsAssignableFrom(p.PropertyType)&&p.PropertyType!=typeof(string))
{
sb.Append(ObjectToQueryString(p.GetValue(datum));
}
其他的
{
//我认为这不是一个好办法
if(p.PropertyType.FullName!=p.GetValue(数据,null).ToString()
{
//sb.Append(String.Format(“{0}={1}&”,p.Name,HttpUtility.UrlEncode(p.GetValue(datum,null.ToString())));
sb.Append(String.Format(“{0}={1}&”,p.Name,p.GetValue(datum,null.ToString());
}
其他的
{
sb.Append(ObjectToQueryString(p.GetValue(datum,null));
}
}
}
}
}
使某人返回字符串('&');
}
我不明白,你为什么要这么复杂
public class LoginRequest : BaseRequest
{
public string username { get; set; }
public string password { get; set; }
public otherclass d { get; set; }
public String getQueryString(){
return "username="+this.username+"&password="+this.password+"&a="+this.d.a+"&b="+this.d.b;
}
}
public class otherclass
{
public string a { get; set; }
public string b { get; set; }
}
。。。或者您的问题中遗漏了什么?使用GetProperties()的BindingFlags参数
将其结果限制为由您的类型声明的属性。这可能是通过POST正确序列化(例如XML)而不是作为querystring的一部分发送的最好方法。抱歉,应该添加。然后对字符串进行散列,并将其用作headerWould中的散列。此外,还建议将对象序列化为字符串,但不建议使用JSON。Json.Net是一个安全的开始。然后url编码json并附加它。就像你的坟墓;-)这只是一个次要问题,但您知道通过查询字符串传递密码是非常糟糕的,对吗?我的意思是,我们谈论的是《星球大战第一集》,很糟糕。我不会在查询字符串上以明文形式传递密码。我不想让世界爆炸。这只是一个示例类:)
GetProperties(BindingFlags.DeclaredOnly | ...)