越来越不知道他们的名字对象的属性值? [英] getting object's properties values without knowing their name?

查看:135
本文介绍了越来越不知道他们的名字对象的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  A级
{
公共字符串{搞定;组; }
--and所以on-- //让我们有30-50级变量//
}

我知道它不好。但我如何才能通过只是循环和不知道他们的名字只是对象或类的instanse得到所有变量的值?



 的for(int我;我小于30; i ++在)
{
变量[i] =对象? ;
}


解决方案

使用反射。

  typeof运算(A).GetFields()
typeof运算(A).GetProperties()


class A
{
    public string a { get; set; } 
    --and so on--   //lets we have 30-50 class variables//
}

i know its bad. but how can i get the values of all variables by just looping and without knowing their name just with object or instanse of class?

like

for(int i;i<30;i++)
{
    variable[i] = object.? ;
}

解决方案

Use reflection.

typeof(A).GetFields()
typeof(A).GetProperties()

这篇关于越来越不知道他们的名字对象的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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