Solved

looking up objects inheriting a specified interface in .net

Posted on 2004-08-08
1
191 Views
Last Modified: 2010-04-15
is there a way of looking up all objects inheriting a specified interface in the .net class library?
0
Comment
Question by:sethUSer420
1 Comment
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
ID: 11748517
sure is !

you would need to run through a set of classes ...

when you get the type for the class on of the methods is GetInterfaces http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtypeclassgetinterfacestopic.asp

using System;
using System.Web;
using System.Web.UI;

namespace Samples
{
    public class MyTemplate : Control, INamingContainer
    {
        private String _message = null;
        public String Message
        {
            get
            {
                return _message;
            }
            set
            {
                _message = value;
            }
        }    
    }
    public class MyInterfacesSample
    {
        public static void Main()
        {
            try
            {
                Type[] myObjectArray= typeof(MyTemplate).GetInterfaces();
                Console.WriteLine("The interfaces inherited by the MyTemplate class are:\n");
                for (int index = 0; index < myObjectArray.Length; index++)
                {    
                    Console.WriteLine(myObjectArray[index]);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An exception occurred.");
                Console.WriteLine("Message: " + e.Message);
            }
        }
    }
}

is an example of how to use it, you would simply compare your interface to the interfaces of the class to see if it contains it.

so you would need to run through the assemblies and test each type to see if it contained the interface.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now