Type.GetMembers()方法用于获取当前Type的成员(属性,方法,字段,事件等)。此方法的重载列表中有2种方法,如下所示:
- GetMembers()方法
- GetMembers(BindingFlags)方法
GetMembers() Method
此方法用于返回当前Type的所有公共成员。
用法: public System.Reflection.MemberInfo[] GetMembers ();
返回值:此方法返回代表当前Type的所有公共成员的MemberInfo对象的数组;如果当前Type没有公共成员,则返回MemberInfo类型的空数组。
以下示例程序旨在说明Type.GetMembers()方法的使用:
示例1:
// C# program to demonstrate the
// Type.GetMember() Method
using System;
using System.Globalization;
using System.Reflection;
// Defining Empty class
public class Empty { }
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Empty);
// try-catch block for handling Exception
try {
// Getting array of MemberInfos by
// using GetMembers() Method
MemberInfo[] info = objType.GetMembers();
// Display the Result
Console.WriteLine("Fields of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Fields of current type is as Follow: Boolean Equals(System.Object) Int32 GetHashCode() System.Type GetType() System.String ToString() Void .ctor()
示例2:
// C# program to demonstrate the
// Type.GetMember() Method
using System;
using System.Globalization;
using System.Reflection;
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(int);
// try-catch block for handling Exception
try {
// Getting array of MemberInfos by
// using GetMembers() Method
MemberInfo[] info = objType.GetMembers();
// Display the Result
Console.WriteLine("Fields of current type is as Follow: ");
for (int i = 0; i < 6; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e) {
Console.Write("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Fields of current type is as Follow: Int32 CompareTo(System.Object) Int32 CompareTo(Int32) Boolean Equals(System.Object) Boolean Equals(Int32) Int32 GetHashCode() System.String ToString()
GetMembers(BindingFlags) Method
此方法用于在派生类中重写时使用指定的绑定约束来搜索为当前Type定义的成员。
用法: public abstract System.Reflection.MemberInfo[] GetMembers (System.Reflection.BindingFlags bindingAttr);
Here, it takes a bitmask comprised of one or more BindingFlags that specify how the search is conducted or,
Zero (Default), to return an empty array.
返回值:此方法返回一个MemberInfo对象的数组,该数组表示为当前Type定义的所有与指定绑定约束匹配的成员;或者,如果没有为当前Type定义任何成员,或者如果没有定义的成员与绑定匹配,则为MemberInfo类型的空数组约束。
以下示例程序旨在说明上述方法的用法:
示例1:
// C# program to demonstrate the
// Type.GetMembers(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
public class Empty { }
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(Empty);
// try-catch block for handling Exception
try {
// Getting array of Fields by
// using GetField() Method
MemberInfo[] info = objType.GetMembers(BindingFlags.Public
| BindingFlags.Instance);
// Display the Result
Console.WriteLine("Fields of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e) {
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Fields of current type is as Follow: Boolean Equals(System.Object) Int32 GetHashCode() System.Type GetType() System.String ToString() Void .ctor()
示例2:
// C# program to demonstrate the
// Type.GetMembers(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
// Defining class Empty
public class Empty { }
class GFG {
// Main Method
public static void Main()
{
// Declaring and initializing object of Type
Type objType = typeof(int);
// try-catch block for handling Exception
try {
// Getting array of Fields by
// using GetField() Method
MemberInfo[] info = objType.GetMembers(BindingFlags.Public
| BindingFlags.Static);
// Display the Result
Console.WriteLine("Fields of current type is as Follow: ");
for (int i = 0; i < info.Length; i++)
Console.WriteLine(" {0}", info[i]);
}
// catch ArgumentNullException here
catch (ArgumentNullException e)
{
Console.WriteLine("name is null.");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
Fields of current type is as Follow: Int32 Parse(System.String) Int32 Parse(System.String, System.Globalization.NumberStyles) Int32 Parse(System.String, System.IFormatProvider) Int32 Parse(System.String, System.Globalization.NumberStyles, System.IFormatProvider) Boolean TryParse(System.String, Int32 ByRef) Boolean TryParse(System.String, System.Globalization.NumberStyles, System.IFormatProvider, Int32 ByRef) System.Int32 MaxValue System.Int32 MinValue
参考:
相关用法
- C# DateTimeOffset.Add()用法及代码示例
- C# String.Contains()用法及代码示例
- C# Math.Sin()用法及代码示例
- C# Math.Cos()用法及代码示例
- C# Dictionary.Add()用法及代码示例
- C# Math.Tan()用法及代码示例
- C# Math.Abs()方法用法及代码示例
- C# Math.Exp()用法及代码示例
- C# Math.Abs()函数用法及代码示例
注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Type.GetMembers() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。