将字符串转换为Object或Property [英] Convert string to Object or Property

查看:59
本文介绍了将字符串转换为Object或Property的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样做而不是下面的代码吗?

Can I do this instead of the code below ?

For(int i=0;i<=30;i++)
{
   string str = "((UNOWOS.MyObject)(item)).MH" + i + " = qitem.TotalHours"
}





使用的代码。



Code Used.

if(col.UniqueName == string.Format("{0:ddMMMyyyy}",qitem.TranDate))
{
switch (col.DisplayIndex)
{
case 5:
 ((UNOWOS.MyObject)(item)).MH0 = qitem.TotalHours;
 break;
case 6:
 ((UNOWOS.MyObject)(item)).MH1 = qitem.TotalHours;
 break;
case 7:
 ((UNOWOS.MyObject)(item)).MH2 = qitem.TotalHours;
 break;
case 8:
 ((UNOWOS.MyObject)(item)).MH3 = qitem.TotalHours;
 break;
case 9:
 ((UNOWOS.MyObject)(item)).MH4 = qitem.TotalHours;
 break;

推荐答案

也就是说,你有属性名称为MH0,MH1,MH2,......?

你听说过数组吗?然后你可以做((UNOWOS.MyObject)(item))。MH [i] = qitem.TotalHours;

否则,你可以仍然计算属性的名称,并使用反射来访问它 - 这对初学者来说并不容易,而且通常不是一个好习惯。
That is, you have properties with the names MH0, MH1, MH2, ...?
Have you ever heard of arrays? Then you could do a ((UNOWOS.MyObject)(item)).MH[i] = qitem.TotalHours;
Otherwise, you could still compute the name of the property, and use reflection to access it - that''s not easy for a beginner, and it is normally not a good practise.


这篇关于将字符串转换为Object或Property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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