# C# | Type.GetMember() Method

Type.GetMember() Method is used to get the specified members of the current Type. There are 3 methods in the overload list of this method as follows:

• GetMember(String) Method
• GetMember(String, BindingFlags) Method
• GetMember(String, MemberTypes, BindingFlags) Method

#### GetMember(String) Method

This method searches for the public members with the specified name.

Syntax: public System.Reflection.MemberInfo[] GetMember (string name);
Here, it takes the string containing the name of the public members to get.

Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name, if found otherwise, an empty array.

Exception: This method throws ArgumentNullException if name is null.

Below programs illustrate the use of the above-discussed method:

Example 1:

 `// C# program to demonstrate the ` `// Type.GetMember(String) 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``"Name"``); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student  ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` `} `

Output:

```Members of current type is as Follow:
System.String Name
```

Example 2: For ArgumentNullException

 `// C# program to demonstrate the ` `// Type.GetMember(String) 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``null``); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student  ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` `} `

Output:

```name is null.
Exception Thrown: System.ArgumentNullException
```

#### GetMember(String, BindingFlags) Method

This method is used to search for the specified members, using the specified binding constraints.

Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.BindingFlags bindingAttr);

Parameters:
name: It is the string containing the name of the members to get.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted. or, Zero, to return an empty array.

Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.

Exception: This method throws ArgumentNullException if name is null.

Below programs illustrate the use of the above-discussed method:

Example 1:

 `// C# program to demonstrate the ` `// Type.GetMember(String, BindingFlags)  ` `// 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``"Name"``, ` `                      ``BindingFlags.Public | BindingFlags.Instance); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` `} `

Output:

```Members of current type is as Follow:
System.String Name
```

Example 2: For ArgumentNullException

 `// C# program to demonstrate the ` `// Type.GetMember(String, BindingFlags) 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``null``, ` `                    ``BindingFlags.Public | BindingFlags.Instance); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` `} `

Output:

```name is null.
Exception Thrown: System.ArgumentNullException
```

#### GetMember(String, MemberTypes, BindingFlags) Method

This method is used to search for the specified members of the specified member type, using the specified binding constraints.

Syntax: public virtual System.Reflection.MemberInfo[] GetMember (string name, System.Reflection.MemberTypes type, System.Reflection.BindingFlags bindingAttr);

Parameters:
name: It is the string containing the name of the members to get.
type: It is the value to search for.
bindingAttr: It is a bitmask comprised of one or more BindingFlags that specify how the search is conducted or, Zero, to return an empty array.

Return Value: This method returns an array of MemberInfo objects representing the public members with the specified name if found otherwise, an empty array.

Exception: This method throw ArgumentNullException if the name is null.

Below programs illustrate the use of the above-discussed method:

Example 1:

 `// C# program to demonstrate the ` `// Type.GetMember(String, MemberTypes, ` `// BindingFlags) 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``"returnNull"``, MemberTypes.Method, ` `                                      ``BindingFlags.Public | BindingFlags.Instance); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student  ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` ` `  `    ``public` `void` `returnNull() {} ` `} `

Output:

```Members of current type is as Follow:
Void returnNull()
```

Example 2: For ArgumentNullException

 `// C# program to demonstrate the ` `// Type.GetMember(String, MemberTypes, ` `// BindingFlags) 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``(Student); ` ` `  `        ``// try-catch block for handling Exception ` `        ``try` `{ ` ` `  `            ``MemberInfo[] info = objType.GetMember(``null``, MemberTypes.Method, ` `                              ``BindingFlags.Public | BindingFlags.Instance); ` ` `  `            ``// Display the Result ` `            ``Console.WriteLine(``"Members 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); ` `        ``} ` `    ``} ` `} ` ` `  `// Defining class Student ` `public` `class` `Student ` `{ ` `    ``public` `string` `Name = ``"Rahul"``; ` `    ``public` `string` `Dept = ``"Electrical"``; ` `    ``public` `int` `Roll = 10; ` `    ``public` `static` `int` `id = 02; ` ` `  `    ``public` `void` `returnNull() {} ` `} `

Output:

```name is null.
Exception Thrown: System.ArgumentNullException
```

Reference:

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.50768375,"math_prob":0.48322934,"size":9613,"snap":"2019-51-2020-05","text_gpt3_token_len":2343,"char_repetition_ratio":0.14965136,"word_repetition_ratio":0.7730148,"special_character_ratio":0.25579944,"punctuation_ratio":0.20672783,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98127896,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-10T13:54:16Z\",\"WARC-Record-ID\":\"<urn:uuid:3e480d45-aa14-45c8-9380-93e405d1ea8e>\",\"Content-Length\":\"186311\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3c8a3697-dfe4-4166-8063-7b9b1d83794f>\",\"WARC-Concurrent-To\":\"<urn:uuid:1a67a911-85a3-4e6c-85ee-4eeb2c90371a>\",\"WARC-IP-Address\":\"23.62.60.179\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/c-sharp-type-getmember-method/\",\"WARC-Payload-Digest\":\"sha1:LNZVL376767EZO7OQILAY7LJHLNRDG2S\",\"WARC-Block-Digest\":\"sha1:KCJMEQNCRNXB22AJYK5Y5YO5GECQTBYS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540527620.19_warc_CC-MAIN-20191210123634-20191210151634-00534.warc.gz\"}"}