评估一个字符串在C#中的属性 [英] evaluate a string as a property in C#

查看:74
本文介绍了评估一个字符串在C#中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在一个字符串的属性...说对象有一个属性酒吧,所以获得酒吧属性我会叫..

 控制台的价值.WRITE(foo.Bar); 

现在说我有存储在一个字符串变量...

 字符串属性=酒吧

富富=新美孚();



我将如何得到的值 foo.Bar 使用属性



如何,我用在PHP中做

  $属性=酒吧; 

$富=新的Foo();

$呼应&foo- GT; {$财产};


解决方案

 富富=新的Foo(); 
VAR barValue = foo.GetType()的getProperty(酒吧)。的GetValue(富,NULL)


I have a property stored in a string... say Object Foo has a property Bar, so to get the value of the Bar property I would call..

Console.Write(foo.Bar);

Now say that I have "Bar" stored in a string variable...

string property = "Bar"

Foo foo = new Foo();

how would I get the value of foo.Bar using property?

How I'm use to doing it in PHP

$property = "Bar";

$foo = new Foo();

echo $foo->{$property};

解决方案

Foo foo = new Foo();
var barValue = foo.GetType().GetProperty("Bar").GetValue(foo, null)

这篇关于评估一个字符串在C#中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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