Export (0) Print
Expand All

Type.GetMember Method (String)

.NET Framework 1.1

Searches for the members with the specified name.

[Visual Basic]
Overloads Public Function GetMember( _
   ByVal name As String _
) As MemberInfo()
[C#]
public MemberInfo[] GetMember(
 string name
);
[C++]
public: MemberInfo* GetMember(
 String* name
) [];
[JScript]
public function GetMember(
   name : String
) : MemberInfo[];

Parameters

name
The String containing the name of the public members to get.

Return Value

An array of MemberInfo objects representing the public members with the specified name, if found; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentNullException name is a null reference (Nothing in Visual Basic).

Remarks

The search for name is case-sensitive.

Members include properties, methods, fields, events, and so on.

This method searches the current classes and its base classes. Only public methods can be found using this method. If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing in Visual Basic).

Class initializers are available through GetMember, GetMembers, FindMembers, and GetConstructors.

The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

Member Type Static Non-Static
Constructor No No
Field No Yes. A field is always hide-by-name-and-signature.
Event Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
Method No Yes. A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
Nested Type No No
Property Not applicable The common type system rule is that the inheritance is the same as that of the methods that implement the property. Reflection treats properties as hide-by-name-and-signature. See note 2 below.
  1. Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. This is a binary comparison.
  2. For reflection, properties and events are hide-by-name-and-signature. If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.
  3. Custom attributes are not part of the common type system.

Example

[Visual Basic, C#, C++] The following example displays all the members of the String class that start with the letter C.

[Visual Basic] 

Imports System
Imports System.Security
Imports System.Reflection
Imports Microsoft.VisualBasic

Public Class MyMemberSample

    Public Shared Sub Main()
        Dim [myClass] As New MyMemberSample()
        Try
            [myClass].GetMemberInfo()
            [myClass].GetPublicStaticMemberInfo()
            [myClass].GetPublicInstanceMethodMemberInfo()
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException occurred.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        Catch e As NotSupportedException
            Console.WriteLine("NotSupportedException occurred.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        Catch e As SecurityException
            Console.WriteLine("SecurityException occurred.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        Catch e As Exception
            Console.WriteLine("Exception occurred.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        End Try
    End Sub 'Main


    Public Sub GetMemberInfo()
        Dim myString As [String] = "GetMember_String"
        Dim myType As Type = myString.GetType()
        ' Get the members for myString starting with the letter C.
        Dim myMembers As MemberInfo() = myType.GetMember("C*")
        If myMembers.Length > 0 Then
            Console.WriteLine(ControlChars.Cr + "The member(s) starting with the letter C for type {0}:", myType)
            Dim index As Integer
            For index = 0 To myMembers.Length - 1
                Console.WriteLine("Member {0}: {1}", index + 1, myMembers(index).ToString())
            Next index
        Else
            Console.WriteLine("No members match the search criteria.")
        End If
    End Sub 'GetMemberInfo

[C#] 

using System;
using System.Security;
using System.Reflection;

public class MyMemberSample 
{
    public static void Main()
    {
        MyMemberSample myClass = new MyMemberSample();
        try
        {
            myClass.GetMemberInfo();
            myClass.GetPublicStaticMemberInfo();    
            myClass.GetPublicInstanceMethodMemberInfo();    
        }      
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException occurred.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
        catch(NotSupportedException e)
        {
            Console.WriteLine("NotSupportedException occurred.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException occurred.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception occurred.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
    }

    public void GetMemberInfo()
    {
        String myString = "GetMember_String";

        Type myType = myString.GetType();
        // Get the members for myString starting with the letter C.
        MemberInfo[] myMembers = myType.GetMember("C*");
        if(myMembers.Length > 0)
        {
            Console.WriteLine("\nThe member(s) starting with the letter C for type {0}:", myType);
            for(int index=0; index < myMembers.Length; index++)
                Console.WriteLine("Member {0}: {1}", index + 1, myMembers[index].ToString());
        }
        else
            Console.WriteLine("No members match the search criteria.");    
    }

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Security;
using namespace System::Reflection;

// forward declarations:
void GetMemberInfo();
void GetPublicStaticMemberInfo();
void GetPublicInstanceMethodMemberInfo();

int main() {
   try {
      GetMemberInfo();
      GetPublicStaticMemberInfo();
      GetPublicInstanceMethodMemberInfo();
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"ArgumentNullException occurred.");
      Console::WriteLine(S"Source: {0}", e->Source);
      Console::WriteLine(S"Message: {0}", e->Message);
   } catch (NotSupportedException* e) {
      Console::WriteLine(S"NotSupportedException occurred.");
      Console::WriteLine(S"Source: {0}", e->Source);
      Console::WriteLine(S"Message: {0}", e->Message);
   } catch (SecurityException* e) {
      Console::WriteLine(S"SecurityException occurred.");
      Console::WriteLine(S"Source: {0}", e->Source);
      Console::WriteLine(S"Message: {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception occurred.");
      Console::WriteLine(S"Source: {0}", e->Source);
      Console::WriteLine(S"Message: {0}", e->Message);
   }
}

void GetMemberInfo() {
   String*  myString = S"GetMember_String";

   Type*  myType = myString->GetType();
   // Get the members for myString starting with the letter C.
   MemberInfo*  myMembers[] = myType->GetMember(S"C*");
   if (myMembers->Length > 0) {
      Console::WriteLine(S"\nThe member(s) starting with the letter C for type {0}:", myType);
      for (int index=0; index < myMembers->Length; index++)
         Console::WriteLine(S"Member {0}: {1}",__box( index + 1), myMembers->Item[index]);
   } else
      Console::WriteLine(S"No members match the search criteria.");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetMember Overload List | MemberInfo | String | DefaultBinder | ReflectionPermission | GetMembers | GetDefaultMembers | FindMembers

Show:
© 2014 Microsoft