如何从C#中的对象数据类型读取值 [英] How to read value from object data type in C#

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

问题描述





我正在使用instamojo支付网关。在具有对象类型的响应参数中。



我需要读取该值。



我的代码如下:



Hi,

i am using instamojo payment gateway.in the response parameters having object type.

i need to read that value.

my code is below:

static void GetPaymentOrderDetailsUsingTransactionId(Instamojo objClass)
       {
           try
           {
               PaymentOrderDetailsResponse objPaymentRequestDetailsResponse = objClass.getPaymentOrderDetailsByTransactionId("testj2oz244t4lp");
               ArrayList final_result = new ArrayList();             
                MessageBox.Show("Transaction Id = " + objPaymentRequestDetailsResponse.transaction_id);
           }
           catch (ArgumentNullException ex)
           {
               MessageBox.Show(ex.Message);
           }
           catch (WebException ex)
           {
               MessageBox.Show(ex.Message);
           }
           catch (Exception ex)
           {
               MessageBox.Show("Error:" + ex.Message);
           }
       }










public class PaymentOrderDetailsResponse
	{
		public PaymentOrderDetailsResponse()
		{
			status = "pending";
			currency = "INR";
		}

		public string id { get; set; }
		public string transaction_id { get; set; }
		public string status { get; set; }
		public string currency { get; set; }
		public decimal? amount { get; set; }
		public string name { get; set; }
		public string email { get; set; }
		public string phone { get; set; }
		public string description { get; set; }
		public string redirect_url { get; set; }

		public object payments { get; set; }

		public string created_at { get; set; }
		public string resource_uri { get; set; }
	}





我尝试过:



i我试图读取价值



What I have tried:

i am trying to read value

public object payments { get; set; }



它不允许我读取该值。



任何想法?


it's not allowing me to read that value.

any idea ?

推荐答案

你没有。 Object类型不会公开任何这些属性,因此没有任何内容可供阅读。看一下Object公开的内容,这里 [ ^ ]。



为什么使用object作为该属性的返回类型?使用对象的类型,然后您将可以访问该类型公开的属性和方法。



或者,如果此属性付款是您无法更改的供应商代码,您必须将返回的对象强制转换为暴露您正在寻找的属性和方法的适当类型。
You don't. The Object type doesn't expose any of these properties so there's nothing there to read. Take a look at what Object does expose, here[^].

Why are you using object as a return type of that property? Use the type the object really is instead, then you'll get access to the properties and methods exposed by that type.

Or, if this property "payments" is vendor code that you cannot change, you have to cast the object returned to it's appropriate type that exposes the properties and methods you're looking for.


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

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