Click here to Skip to main content
Click here to Skip to main content

TypeLoader for .NET

, 3 Jan 2002
Rate this:
Please Sign up or sign in to vote.
Class and splash screen to load custom types at runtime.

Sample Image - typeloader.gif

NOTE: In order to run the demo, you must have the PDC 2001 release of Visual Studio. NET otherwise you won't have the correct system dlls. You can still download the source and build it.

Introduction

Table of Contents

Introduction

After writing my first article on loading types at runtime in mid-December I’ve since started a re-write of my type interface (after seeing a fatal flaw in my design).  In doing so I also realized that the code to load my new types looked nearly identical to what I used before; in fact, the code for most people’s use would probably look the same.

So I broke it out into its own class, and created a fancy splash screen to go with it.

Compiling the source code

When you extract the source you will have a setup.bat in the root directory, run this to setup the strong name key files (snk).  You may have to use Visual Studio's provided command line shortcut to ensure that your path is setup correctly.  In order for the project to work you must sign your assemblies (though you probably don't have to sign the main executable I do because it doesn't take much work to do, and gives added peace of mind to your users).

Once you compile all three projects place the resulting DLLs in the directory with the main executable, you should only need to copy the dll from the StandardTypes project. You can now run the program, and you should see the splash screen come up, load a type, then disappear.

TypeLoader

The type loader will be doing the grunt work for you, all you have to do is tie into it via the TypeLoading event, and optionally the TypeLoaded event.

Types property

public TypeCollection Types { get; set; }

This is the collection of types that have been loaded via the TypeLoading event.  It is a strongly typed Collection of Type objects (generated via Microsoft's Collection Generator).

TypeLoading event

public event TypeLoadingEventHandler TypeLoading;
public delegate void TypeLoadingEventHandler(object sender, <BR>  TypeLoadingEventArgs e);

This event will be fired for every type that the class encounters while it is loading.  The TypeLoading property contains the Type object in question.  Set the Cancel property to true to not load the type, set it to false to load the type.

TypeLoaded event

public event TypeLoadedEventHandler TypeLoaded;
public delegate void TypeLoadedEventHandler(object sender, <BR>  TypeLoadedEventArgs e);

This event will be fired for every type that was successfully loaded by the system.  That is, it'll be fired for every type where the TypeLoading event's Cancel property was false when it returned.  The TypeLoaded property contains the Type object that was loaded.  This event is provided only as a service and is not required to be listened to.

LoadTypesFromDirectory method

public void LoadTypesFromDirectory(string path);
public void LoadTypesFromDirectory(string path, string searchPattern);
public void LoadTypesFromDirectory(string path, <BR>  string searchPattern, bool recurseSubdirectories);

If not specified searchPattern will default to "*.dll" and recurseSubdirectories will be false.

This method is the whole purpose of the class to begin with.  It will search a directory for types, and notify the caller when it finds some via the TypeLoading event.

The path should be suitable for the System.IO.Directory class, and the searchPattern should work with the System.IO.Directory.GetFiles method.

Each of the overloads in the end calls down to a protected method, LoadTypesFromDirectoryRecursive.

None of these methods clears the collection of types.

Clear method

void Clear();

This method removes all Types from the TypeCollection.

TypeImplementsInterface static method

public static bool TypeImplementsInterface(Type type, Type interfaceType);

Returns true if the given type implements the interface name passed.  This currently goes through the list of all types implemented and compares the full name of both types.  Since its possible for two different assemblies to have the same namespace, its recommended that you prefix all namespaces with your company name.

The Splash Screen

The splash screen has 2 customizable parts to it

  • Bitmap
  • Type Listing

The bitmap is a 352x72 pixel image displayed on the top portion of the window.

The Type Listing displays all the types that have been successfully loaded by the program.  In this case the listing shows a bitmap and a name gotten from the type.

How to use it

public void ShowSplashScreen()
{
	SplashScreen splash = new SplashScreen();

	TypeCollection typesLoaded = splash.DoSplash();
	// Use the typeCollection in your main program
}

Simple eh?  Not quite that simple though, you have to modify the source to the splash screen, just a bit so that it looks for the types you want to use in your program.  The method you need to change is loader_TypeLoading.  It currently shows an example of how to look for an interface being implemented by a class.

How it works

All the work is done in the events for the TypeLoader object.

In the TypeLoading event it does a compare on the type to see if it implements the IType interface from the Types namespace.  The compare is done using TypeLoader's only static method, TypeImplementsInterface.

In the TypeLoaded event it gets the picture from the IType, adds it to the imagelist; then adds it to the ListView.  Pretty simple, and gives the effect I was looking for.

Summary

If you look through the code you'll see that my root namespace is Takklesoft.CollectorsDatabase.  No I didn't forget to change it, this is code I've ripped from my current project.  So if I ever get it released you'll know how I did the splash screen.

As always any questions or comments, leave them below or e-mail me.

The code I provide here is free for anyone to use, I don't require anything in return, but a mention in some documents or an about box would be nice Smile | :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

James T. Johnson
Software Developer (Senior) InfoPlanIT, LLC
United States United States
James has been programming in C/C++ since 1998, and grew fond of databases in 1999. His latest interest has been in C# and .NET where he has been having fun writing code starting when .NET v1.0 was in its first beta.
 
He is currently a senior developer and consultant for InfoPlanIT, a small international consulting company that focuses on custom solutions and business intelligence applications.
 
He was previously employed by ComponentOne where he was a Product Manager for the ActiveReports, Data Dynamics Reports, and ActiveAnalysis products.
 
Code contained in articles where he is the sole author is licensed via the new BSD license.
Follow on   LinkedIn

Comments and Discussions

 
GeneralCode doesnt work PinmemberAnonymous1-May-02 15:12 
GeneralRe: Code doesnt work PinmemberJonny Newman28-May-02 8:56 
GeneralRe: Code doesnt work PinsubeditorJames T. Johnson28-May-02 9:45 
GeneralRe: Code doesnt work PinsubeditorJames T. Johnson28-May-02 9:55 
GeneralRe: Code doesnt work PinmemberRumen Yankov19-Mar-03 22:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 4 Jan 2002
Article Copyright 2002 by James T. Johnson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid