我正在调用一个返回此对象的Web服务。我知道我应该在C#中使用对象反射来访问sentBatchTotal
的属性。但是,我不能为我的生活弄清楚如何到达这个属性。我在这里和MSDN上看过其他几篇文章,但它并没有陷入其中。
这是我的代码,我做错了什么?
private void button1_Click(object sender, EventArgs e)
{
prdChal.finfunctions service = new prdChal.finfunctions();
//Type thisObject = typeof()
//Type myType = myObject.GetType();
//IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
String ThisName = "";
Object StatusReturn = new Object();
StatusReturn = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
var type = StatusReturn.GetType();
var propertyName = type.Name;
//var propertyValue = propertyName.GetValue(myObject, null);error here
}
答案 0 :(得分:3)
不要先将StatusReturn变量声明为对象类型。
//Object StatusReturn = new Object();
var StatusReturn = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
if (StatusReturn.Count() > 0)
{
var fixedAsset = StatusReturn[0];
}
答案 1 :(得分:2)
dynamic d = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
string result = (string)d[0].sentBatchTotal;
答案 2 :(得分:1)
以下代码使用反射。
StatusReturn = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
var type = StatusReturn.GetType();
var pi = type.GetProperty("sentBatchTotal");
if (pi != null) {
var propertyValue = pi.GetValue(StatusReturn, null);
}
但是你不能只用webservice方法返回类型而不是对象吗?您可以直接阅读该物业。
类似的东西:
WhatEverTypeYourServiceReturns StatusReturn = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
string sentBatchTotal = StatusReturn.sentBatchTotal;