如何序列化对象放入查询字符串格式? [英] How do I serialize an object into query-string format?
问题描述
我如何将对象序列化到查询字符串格式?我似乎无法找到谷歌的答案。谢谢你。
下面是我的对象序列化将作为一个例子。
公共类EditListItemActionModel
{
公众诠释?标识{搞定;组; }
公众诠释国家{搞定;组; }
公共字符串$ P $ {PFIX获得;组; }
公共字符串指数{搞定;组; }
公众诠释? PARENTID {搞定;组; }
}
我99%肯定没有内置此实用程序方法。这不是一个很常见的任务,因为Web服务器通常不与URLEn codeD键/值串作出响应的
你如何看待混合反射和LINQ?这工作:
无功富=新EditListItemActionModel(){
ID = 1,
状态= 26,
preFIX =F,
指数=OO
PARENTID = NULL
};VAR场所,中,p = foo.GetType()的GetProperties()
其中,p.GetValue(富,NULL)!= NULL
选择p.Name +=+ HttpUtility.UrlEn code(p.GetValue(富,空)的ToString());//的queryString将被设定为ID = 1和;状态= 26&放大器; preFIX = F&放大器;指数= OO
串的queryString =的string.join(与&,properties.ToArray());
更新:
要编写返回任何1深对象的查询字符串重新presentation的方法,你可以这样做:
公共字符串GetQueryString(obj对象){
VAR场所,中,p = obj.GetType()的GetProperties()
其中,p.GetValue(OBJ,NULL)!= NULL
选择p.Name +=+ HttpUtility.UrlEn code(p.GetValue(OBJ,空)的ToString()); 返回的string.join(与&,properties.ToArray());
}//使用方法:
字符串的queryString = GetQueryString(富);
您也可以把它的扩展方法没有太多的额外工作。
公共静态类ExtensionMethods {
公共静态字符串GetQueryString(此对象OBJ){
VAR场所,中,p = obj.GetType()的GetProperties()
其中,p.GetValue(OBJ,NULL)!= NULL
选择p.Name +=+ HttpUtility.UrlEn code(p.GetValue(OBJ,空)的ToString()); 返回的string.join(与&,properties.ToArray());
}
}//使用方法:
字符串的queryString = foo.GetQueryString();
How do I serialize an object into query-string format? I can't seem to find an answer on google. Thanks.
Here is the object I will serialize as an example.
public class EditListItemActionModel
{
public int? Id { get; set; }
public int State { get; set; }
public string Prefix { get; set; }
public string Index { get; set; }
public int? ParentID { get; set; }
}
I'm 99% sure there's no built-in utility method for this. It's not a very common task, since a web server doesn't typically respond with a URLEncoded key/value string.
How do you feel about mixing reflection and LINQ? This works:
var foo = new EditListItemActionModel() {
Id = 1,
State = 26,
Prefix = "f",
Index = "oo",
ParentID = null
};
var properties = from p in foo.GetType().GetProperties()
where p.GetValue(foo, null) != null
select p.Name + "=" + HttpUtility.UrlEncode(p.GetValue(foo, null).ToString());
// queryString will be set to "Id=1&State=26&Prefix=f&Index=oo"
string queryString = String.Join("&", properties.ToArray());
Update:
To write a method that returns the QueryString representation of any 1-deep object, you could do this:
public string GetQueryString(object obj) {
var properties = from p in obj.GetType().GetProperties()
where p.GetValue(obj, null) != null
select p.Name + "=" + HttpUtility.UrlEncode(p.GetValue(obj, null).ToString());
return String.Join("&", properties.ToArray());
}
// Usage:
string queryString = GetQueryString(foo);
You could also make it an extension method without much additional work
public static class ExtensionMethods {
public static string GetQueryString(this object obj) {
var properties = from p in obj.GetType().GetProperties()
where p.GetValue(obj, null) != null
select p.Name + "=" + HttpUtility.UrlEncode(p.GetValue(obj, null).ToString());
return String.Join("&", properties.ToArray());
}
}
// Usage:
string queryString = foo.GetQueryString();
这篇关于如何序列化对象放入查询字符串格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!