Get public members with the specified name in CSharp

Description

The following code shows how to get public members with the specified name.

Example


using System;/*from   w  ww  .  java  2  s .com*/
using System.Security;
using System.Reflection;

public class MyMemberSample 
{
    public static void Main()
    {

        String myString = "GetMember_String";

        Type myType = myString.GetType();
        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.");    
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type