【发布时间】:2017-03-01 17:06:33
【问题描述】:
我有一个包含 500 个属性的对象,我需要使用字符串动态调用该对象。如何使用 c#?
public class MyObjects
{
public int RGP_Id { get; set; }
public DateTime RGP_DateTime { get; set; }
public int RGP_MCC_Numero_Serie_MS { get; set; }
public int RGP_IDREG_1 { get; set; }
public int RGP_IDREG_2 { get; set; }
public int RGP_IDREG_3 { get; set; }
public int RGP_IDREG_4 { get; set; }
public int RGP_IDREG_5 { get; set; }
public int RGP_IDREG_6 { get; set; }
public int RGP_IDREG_7 { get; set; }
public int RGP_IDREG_8 { get; set; }
public int RGP_IDREG_9 { get; set; }
public int RGP_IDREG_10 { get; set; }
.......
public int RGP_IDREG_500 { get; set; }
}
我需要调用对象的属性并使用字符串返回它的值。
目标示例:
var x = MyObjects.GetPropertyValue("RGP_IDREG_10");
有可能吗?
【问题讨论】:
-
展示的目标对我来说并不是很清楚。你想要一个名为
Equals()的static方法,它需要一个string,具体做什么?在这个操作之后x会是什么? -
我需要通过字符串调用对象的属性并返回其值...
-
这不是重复的@David,这是针对 xamarin 表单的....GetProperty() not exist in xamarin forms c#
-
有趣。您能否详细说明您是如何尝试使用该解决方案的以及失败的原因?
-
我正在尝试使用 GetType () 但没有可以通过字符串检索值的方法,反射我无法使用它,因为 .net xaamrin 表单不完全支持此功能。
标签: c# object reflection xamarin.forms equals