在C#中,有没有无论如何要确定一个类的成员在运行时? [英] in C#, Is there anyway to determine a class's members at runtime?

查看:97
本文介绍了在C#中,有没有无论如何要确定一个类的成员在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一类名为FOO,它有3个公共成员foo1,foo2的,和foo3。

Suppose I have a class named foo, and it has 3 public members foo1, foo2, and foo3.

现在假设我正在写一个函数,类Foo的一个实例作为参数,但是当我在写这个功能我不知道有什么样的公共成员。

Now suppose I'm writing a function that takes an instance of class foo as a parameter, but when I'm writing this function I have no idea what public members it has.

有没有办法让我确定在运行时,它具有公共成员foo1,foo2的,foo3的 AND ONLY 的foo1,foo2的,foo3。 IE浏览器 - 找出所有公众成员

Is there a way for me to determine at run-time that it has public members foo1, foo2, foo3 AND ONLY foo1, foo2, foo3. IE - find out what all the public members are?

和可我也决定了它们的类型?

And can I also determine their types?

推荐答案

嗯,这是反思是什么在那里:

Well, that's what Reflection is there for :

Type myObjectType = typeof(foo);

System.Reflection.FieldInfo[] fieldInfo = myObjectType.GetFields();
foreach (System.Reflection.FieldInfo info in fieldInfo)
   Console.WriteLine(info.Name); // or whatever you desire to do with it

这篇关于在C#中,有没有无论如何要确定一个类的成员在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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