#453 – Use Reflection to Get a List of Interfaces that a Class Implements

You can query an object at runtime, using reflection, to get information about its members.  This includes the interfaces that the object’s class implements.

            Cow bessie = new Cow("Bessie", 4);

            // Use reflection to dump out information about
            //   Bessie's interfaces and the methods in
            //   each interface.
            Type t = bessie.GetType();
            foreach (Type iType in t.GetInterfaces())
            {
                Console.WriteLine("Interface [{0}]:", iType.Name);
                foreach (MemberInfo minfo in iType.GetMembers())
                {
                    Console.WriteLine("  {0}", minfo.Name);
                }
            }

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: