C#VS 2005:如何运行期间得到一个类的公共成员名单? [英] C# VS 2005: How to get a class's public member list during the runtime?

查看:179
本文介绍了C#VS 2005:如何运行期间得到一个类的公共成员名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让在运行时类memeber变量列表。我知道这可能使用的typeof和反思。却找不到一个例子。请别人阐明我

I'm trying to get a class memeber variable list at run time. I know this probably using typeof and reflections. but can't find an example. Please someone shed light for me.

下面是伪代码示例:

Class Test01
{ 
 public string str01;
 public string str02;
 public int myint01;
}



我想是这样的(伪代码):

I want something like this (pseudo code):

Test01 tt = new Test01();
foreach(variable v in tt.PublicVariableList)
{
   debug.print v.name;
   debug.print v.type;
}

请帮我找出如何在C#VS2005做到这一点。

Please help me figure out how to do this in C# VS2005

非常感谢

推荐答案

如果你是公众的字段后的只是使用 tt.GetType()。GetFields()

If you're after public fields just use tt.GetType().GetFields()

如果您需要其他成员使用的的GetProperties(),的的getMethods() GetEvents()等特定的人或 GetMembers()让他们所有。

If you need other members, use GetProperties(), GetMethods(), GetEvents() etc for specific ones, or GetMembers() to get them all.

每个方法都有一个重载接受的BindingFlags如果要访问非公共成员,或限制搜索静态成员(或只是实例成员)。

Each method has an overload accepting a BindingFlags if you want to access non-public members, or restrict the search to static members (or just to instance members).

这篇关于C#VS 2005:如何运行期间得到一个类的公共成员名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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