Solved

looking up objects inheriting a specified interface in .net

Posted on 2004-08-08
1
193 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

16 Experts available now in Live!

Get 1:1 Help Now