#731 – Getting Information About the Members of a Class

You can use the Type.GetMembers method to get information about all public members in a type.  GetMembers returns an array of MemberInfo objects, each of which can be used to get details about one of the members of the type.

Here’s a sampling of some of the information that you can get:

            MemberInfo[] members = typeof(Dog).GetMembers();

            if (members != null)
                foreach (MemberInfo mi in members)
                {
                    Console.WriteLine(mi.Name);
                    Console.WriteLine(string.Format("  MemberType: {0}", mi.MemberType));

                    object[] attributes = mi.GetCustomAttributes(false);
                    if ((attributes != null) && (attributes.Length > 0))
                    {
                        Console.Write("  Attributes: ");
                        foreach (object attr in attributes)
                            Console.Write(string.Format("{0} ", attr.ToString()));
                        Console.WriteLine();
                    }

                    // Get subtype-specific information
                    switch (mi.MemberType)
                    {
                        case MemberTypes.Constructor:
                            DumpConstructorInfo((ConstructorInfo)mi);
                            break;

                        case MemberTypes.Event:
                            DumpEventInfo((EventInfo)mi);
                            break;

                        case MemberTypes.Method:
                            DumpMethodInfo((MethodInfo)mi);
                            break;
                    }

                    Console.WriteLine();
                }
        private static void DumpConstructorInfo(ConstructorInfo ci)
        {
            Console.WriteLine(string.Format("  Calling Convention: {0}", ci.CallingConvention));

            StringBuilder sbInfo = new StringBuilder("  ");
            if (ci.IsAbstract) sbInfo.Append("Abstract ");
            if (ci.IsGenericMethod) sbInfo.Append("GenericMethod ");
            if (ci.IsHideBySig) sbInfo.Append("HideBySig ");
            if (ci.IsPrivate) sbInfo.Append("Private ");
            if (ci.IsPublic) sbInfo.Append("Public ");
            if (ci.IsStatic) sbInfo.Append("Static ");
            if (ci.IsVirtual) sbInfo.Append("Virtual ");

            if (sbInfo.Length > 2)
                Console.WriteLine(sbInfo);
        }

        private static void DumpEventInfo(EventInfo ei)
        {
            Console.WriteLine(string.Format("  Event Handler Type: {0}", ei.EventHandlerType.Name));
            if (ei.IsMulticast) Console.WriteLine("  IsMulticast");
        }

        private static void DumpMethodInfo(MethodInfo mi)
        {
            Console.WriteLine(string.Format("  Calling Convention: {0}", mi.CallingConvention));

            StringBuilder sbInfo = new StringBuilder("  ");
            if (mi.IsAbstract) sbInfo.Append("Abstract ");
            if (mi.IsGenericMethod) sbInfo.Append("GenericMethod ");
            if (mi.IsHideBySig) sbInfo.Append("HideBySig ");
            if (mi.IsPrivate) sbInfo.Append("Private ");
            if (mi.IsPublic) sbInfo.Append("Public ");
            if (mi.IsStatic) sbInfo.Append("Static ");
            if (mi.IsVirtual) sbInfo.Append("Virtual ");

            ParameterInfo[] paraminfo = mi.GetParameters();
            if ((paraminfo != null) && (paraminfo.Length > 0))
            {
                Console.Write("  Parameters: ");
                foreach (ParameterInfo pi in paraminfo)
                {
                    Console.Write(string.Format("{0} ({1})  ", pi.Name, pi.ParameterType.Name));
                }
                Console.WriteLine();
            }
        }

731-001

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: