首页 新闻 赞助 找找看

如何计算对象的个数

0
[已解决问题] 解决于 2010-12-04 20:41

public string OpMsg

{

    public string name;

     public string context;

}

 

如上 有2个对象, 写个什么函数可以将 2 计算出来?

prcog的主页 prcog | 初学一级 | 园豆:5
提问于:2010-11-03 23:35
< >
分享
最佳答案
0

int count = typeof(OpMsg).GetFields().Count();

 

carlWolf | 菜鸟二级 |园豆:295 | 2010-11-03 23:44
其他回答(4)
0

int count =  typeof(OpMsg).GetFields().Count();

萧鼎 | 园豆:106 (初学一级) | 2010-11-04 17:06
0

不懂你到底要计算什么。

forhells | 园豆:724 (小虾三级) | 2010-11-06 20:35
0

public string OpMsg

{

    public string name;

     public string context;

}

这是什么?类吗?

CsharpHome | 园豆:205 (菜鸟二级) | 2010-11-06 20:46
0

顶起  carlWolf,他的方法是正确的。

假设你的类OpMsg的命名空间是WindowsFormsApplication1,则

以下方法可以实现你要的功能:

(1)

int count = typeof(Class1).GetFields().Count();

 

(2)

int count = Type.GetType("WindowsFormsApplication1.Class1").GetFields().Count();

 

 当然,只能获取公共字段

 

角逐魏蜀吴 | 园豆:533 (小虾三级) | 2010-11-17 11:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册