Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / May 2004

Tip: Looking for answers? Try searching our database.

Request all implementors of an interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Walter L. Williams - 18 May 2004 22:21 GMT
I'm not sure this is possible, but is there an elegant way to ask for a list
of objects who implement an interface?

Basically what I want to do is create a top level shell that can query dlls
in its directory for a certain interface.  It would be kind of like a
plug-in.  I would like a call that returns an array of interface pointers,
so that I could just drop in a new dll and the next time the call is made,
it is included.  The shell should not have to change to accomodate new
plug-ins.

I have the following interface:

public interface IFileParser
{
String GetFileFilter ();
Boolean ParseFile (String strFile, AddItemDelegate delegateAddItem);
}

Elsewhere, I want to call something like this:

IFileParser[] arrayFileParsers = GetIFileParserImplementors();

Does the framework provide anything for this or what approaches would be
advisable?
====================================================
Walter Williams
Software Engineer
Sawtooth Software, Inc.
http://www.sawtoothsoftware.com
----------------------------------------------------
"Do, or do not. There is no try."
Jon Skeet [C# MVP] - 19 May 2004 06:22 GMT
> I'm not sure this is possible, but is there an elegant way to ask for a list
> of objects who implement an interface?
[quoted text clipped - 5 lines]
> it is included.  The shell should not have to change to accomodate new
> plug-ins.

Use one of the forms of Assembly.Load* to load the DLL, and then use
Assembly.GetTypes to get all the types within it. From there, use

if (typeof(IFileParser).IsAssignableFrom(typeToTest))

to check whether a type implements IFileParser.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Walter L. Williams - 19 May 2004 16:29 GMT
It worked!  Thanks a lot.

String[] arrayDlls = Directory.GetFiles(Application.StartupPath, "*.dll");
ArrayList listParsers = new ArrayList();
for (Int32 i = 0; i < arrayDlls.Length; ++i)
{
Assembly oAssembly = Assembly.LoadFrom(arrayDlls[i]);
Type[] arrayTypes = oAssembly.GetTypes();
for (Int32 j = 0; j < arrayTypes.Length; ++j)
{
if ((typeof(IFileParser) != arrayTypes[j]) &&
typeof(IFileParser).IsAssignableFrom(arrayTypes[j]))
{
IFileParser oParser = (IFileParser)
System.Activator.CreateInstance(arrayTypes[j]);
listParsers.Add(oParser);
}
}
}
this.arrayParsers = new IFileParser[listParsers.Count];
for (Int32 i = 0; i < this.arrayParsers.Length; ++i)
this.arrayParsers[i] = (IFileParser) listParsers[i];

====================================================
Walter Williams
Software Engineer
Sawtooth Software, Inc.
http://www.sawtoothsoftware.com
----------------------------------------------------
"Do, or do not. There is no try."

> > I'm not sure this is possible, but is there an elegant way to ask for a list
> > of objects who implement an interface?
[quoted text clipped - 12 lines]
>
> to check whether a type implements IFileParser.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.