gpt4 book ai didi

C# 在运行时访问对象属性

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:23 24 4
gpt4 key购买 nike

enter image description here

我正在调用返回此对象的网络服务。我知道我应该在 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
}

最佳答案

下面的代码使用了反射。

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-method 返回类型而不是对象吗?您可以直接阅读该属性。

类似于:

WhatEverTypeYourServiceReturns StatusReturn = service.UpdateGrantBillStatus(fundBox.Text, toBox.Text, fromBox.Text);
string sentBatchTotal = StatusReturn.sentBatchTotal;

关于C# 在运行时访问对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367459/

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