2016-09-21 126 views
-3

函数内部执行时,有一个变量名为id如何获得由变量名的变量值

int id = 0; 

我加载它包含在该varable名称的类的实例里面的功能字符串

obj a { varableName = "id" } 

我想从字符串名称中获取变量并从中获取值,这怎么可能。

+2

我需要看到更多一点的代码来了解你想要做什么。你能分享你的全部功能吗?现在,它看起来像一个myA = new a(); a.variableName = ID; –

+0

您必须使用反射。 –

+0

以字符串格式执行web服务函数,例如:string.format(“customer/getcustomerbyid/{id}”,46) – Lanister

回答

1

您可以通过反射得到它:

variableName = this.GetType().GetProperty("id").GetValue(this, null); 
+0

然后它应该是this.GetType()。GetProperty(variableName).GetValue(this,null); – c0d3b34n

+0

这假设它是一个属性,OP的代码不是。 – CodeCaster