gpt4 book ai didi

c# 将类转换为查询字符串

转载 作者:行者123 更新时间:2023-11-30 22:13:57 25 4
gpt4 key购买 nike

我正在尝试将我的应用程序中的一些类/对象转换为查询字符串,例如:

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('&');
}

但是如果我将一个列表传递给函数,它还会包含 Count 和 Capacity 属性以及其他我不想要的东西。说它是

的列表
List<otherclass>()

干杯

最佳答案

对我来说似乎很简单,检查该类是 IEnumerable 还是 IEnumerator,如果是,则枚举它(而不是反射(reflect)该特定类)。如果您说明您希望我们如何处理此类结果,将会有所帮助。

//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('&');
}

关于c# 将类转换为查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18753205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com