您可以在此处找到列出类成员的示例:
查看类型信息
using System;
using System.IO;
using System.Reflection;
class Mymemberinfo
{
public static void Main()
{
Console.WriteLine ("\nReflection.MemberInfo");
// Gets the Type and MemberInfo.
Type MyType = Type.GetType("System.IO.File");
MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
// Gets and displays the DeclaringType method.
Console.WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray.Length, MyType.FullName);
Console.WriteLine("{0}.", MyType.FullName);
if (MyType.IsPublic)
{
Console.WriteLine("{0} is public.", MyType.FullName);
}
}
}
如果你已经有一个对象(比如一个控件),你可以像这样绕过上面的一些代码:
using System;
using System.IO;
using System.Reflection;
class Mymemberinfo
{
public static void Main()
{
// Whatever kind of control you are using:
Object l_control = new Object();
Console.WriteLine ("\nReflection.MemberInfo");
// Gets the Type and MemberInfo.
// ----- Call l_control.GetType();
Type MyType = l_control.GetType();
MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
// Gets and displays the DeclaringType method.
Console.WriteLine("\nThere are {0} members in {1}.",
Mymemberinfoarray.Length, MyType.FullName);
Console.WriteLine("{0}.", MyType.FullName);
if (MyType.IsPublic)
{
Console.WriteLine("{0} is public.", MyType.FullName);
}
}
}
您可以在 MSDN 网站上阅读有关反射的更多信息:
.NET Framework 中的反射
MSDN 上的文章包括如何使用每个功能的完整描述和示例。