C#中的Type.GetMembers()方法
C#中的Type.GetMembers()方法用于获取当前Type的成员(属性,方法,字段,事件等)。
语法
以下是语法-
public System.Reflection.MemberInfo[] GetMembers ();public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);
示例
现在让我们看一个实现Type.GetMembers()方法的示例-
using System;using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField("SubName");
MemberInfo[] info = type.GetMembers();
Console.Write("Members = ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
Console.WriteLine("FieldInfo = {0}", fieldInfo);
}
catch (ArgumentNullException e){
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject{
public string SubName = "Science";
}
输出结果
这将产生以下输出-
Members = System.String ToString() Boolean Equals(System.Object)Int32 GetHashCode()System.Type GetType()Void .ctor()
System.String SubName
FieldInfo = System.String SubName
示例
现在让我们来看另一个实现Type.GetMembers()方法的示例-
using System;using System.Reflection;
public class Demo {
public static void Main(){
Type type = typeof(Subject);
try {
FieldInfo fieldInfo = type.GetField("SubName");
MemberInfo[] info = type.GetMembers(BindingFlags.Public | BindingFlags.Instance);
Console.Write("Members = ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
Console.WriteLine("FieldInfo = {0}", fieldInfo);
}
catch (ArgumentNullException e){
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
public class Subject{
public string SubName = "Science";
}
输出结果
这将产生以下输出-
Members = System.String ToString() Boolean Equals(System.Object)Int32 GetHashCode()System.Type GetType()Void .ctor()
以上是 C#中的Type.GetMembers()方法 的全部内容, 来源链接: utcz.com/z/322134.html