如何从对象类型C#中获取元素 [英] How to get elements from an object type C#

查看:73
本文介绍了如何从对象类型C#中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个对象:

I have this object:

object response = client.ConsultPerson("43812941");



响应是一个带有这些的对象元素:

<小>

回复

{

[1] codError = 1;

[2] msjError =Ok;

[3] name =Jhon;

[4] LastName =Smith;

}



如何获取对象类型中包含的元素值?。 />


响应的本地类型是对象类型。


The response is an object with these elements:

response
{
[1] codError=1;
[2] msjError="Ok";
[3] name="Jhon";
[4] LastName="Smith";
}

How can I get the values of elements contained in an object type?.

THE NATIVE TYPE OF THE RESPONSE IS AN OBJECT TYPE.

推荐答案

你需要对象转换回其本机类型,或者对象的本机类型继承的接口类型,因此其成员将公开。 br />


如果你知道......可以搞清楚......那是什么类型:



NativeType响应= client.Co nsultPerson(43812941);







You need to cast the object back into its "native Type," or the Type of the Interface the object's "native Type" inherits from, so its members will be exposed.

If you know ... can figure out ... what that Type is:

NativeType response = client.ConsultPerson("43812941");

or

NativeType response = client.ConsultPerson("43812941") as NativeType;<br />
<br />
if(response == null) // either object is null, or error in cast





如果你不知道要把它投射到什么类型:在Visual Studio中:



1.在你引用的代码行之后设置一个断点。



2.当你运行代码时,点击断点:



a。打开'立即窗口并输入:?回复或输入:? response.GetType()

b。查看'立即窗口中的结果:尝试从你看到的内容中找出原生类型。



If you don't know what Type to cast it to: In Visual Studio:

1. set a break-point after the line of code you cite.

2. when you run the code, and hit the break-point:

a. open the 'Immediate window and type: ? response or type: ? response.GetType()
b. look at the result(s) in the 'Immediate window: try and figure out the native Type from what you see.


这篇关于如何从对象类型C#中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆