我有一个方法,它在类库中有对象返回类型,我正在使用javascript反序列化器并返回对象数据。并创建了此类的DLL并在另一个项目中调用此类。我正在获取该类中的数据,但混淆了如何在标签控件中显示对象类型数据。
这是类库方法:
public class HttpRequestResponse
{
private WebRequest request;
public object Fetchresult()
{
request = WebRequest.Create("URL");
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
object obj = js.Deserialize<dynamic>(responseFromServer);
return obj;
}
}
这是调用类:
HttpRequestResponse cls = new HttpRequestResponse();
object obj= cls.Fetchresult();
Label3.Text = obj["name"];
我在obj
中获得了值,但是如何在标签控件中显示此值。
感谢
答案 0 :(得分:0)
这是解决方案
dynamic obj = cls.Fetchresult(i);
答案 1 :(得分:0)
也许,你可以这样。所以,你总是有一个你想要的类型的对象。
public class HttpRequestResponse
{
private WebRequest request;
public T Fetchresult<T>()
{
request = WebRequest.Create("URL");
request.Method = "GET";
request.ContentType = "application/json; charset=utf-8";
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
var dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
JavaScriptSerializer js = new JavaScriptSerializer();
var obj = js.Deserialize<T>(responseFromServer);
return obj;
}
}
可以使用它:
List<int> list = HttpRequestResponse.Fetchresult<List<int>>();
或者
ClassName obj = HttpRequestResponse.Fetchresult<ClassName>();