This project has moved and is read-only. For the latest updates, please go here.

How to find TypeCatalog for particular type

Nov 25, 2010 at 8:33 AM

I've created an aggregate catalog and added several TypeCatalogs, each for a separate type.

Something like:

Catalog.Catalogs.Add(new TypeCatalog(type));

At a later moment I want to check if a type catalog for 'type' exists, and if so, remove it.

How to query the aggregate catalog to check if it contains a 'type' catalog for type?


Nov 26, 2010 at 5:57 PM

You can create something called FilteredCatalog which you should be able to find if you search for it. It's a catalog which allows you to hide and show types within it depending on some predicate, that's a good place to start.

But, what you're talking about is sort of recomposition/removing parts from the container. The CompositionContainer can easily be queried for a specific part, however, removing stuff once in the container, not so much.

One way to go about this, would be to switch out the CompositionContainer using FilteredCatalogs that hide the type you whish to remove. This sounds fishy and obtrusive, are you sure you doing the right thing?

Nov 26, 2010 at 8:30 PM

To check if the type exists, you need to use an api we have called ReflectionModelServices.GetPartType. It will allow you to take a part definition (which the catalog returns) and see what the concrete type is for that part. Thus you can do a LINQ query against the AggregateCatalog.Catalogs seeing if one of the catalogs within contains a part whose type matches your type.


Nov 26, 2010 at 8:31 PM

Here's a link to the api:

Nov 27, 2010 at 8:18 AM


I'm now using the following query to get type type catalog that holds the given type:

(from tc in Catalog.Catalogs.OfType<TypeCatalog>()
                     from part in tc.Parts
                     ReflectionModelServices.GetPartType(part).Value == type
                     select tc).SingleOrDefault();

This is exactly what I needed. I'm using SingleOrDefault because before adding a type catalog I check if a catalog with the same type already exists.

I need this functionality because I want to unit test a static class that holds my AggregateCatalog. To make sure the tests do a proper cleanup such that they don't have side-effects, I must be able to unregister the types that a test has registered in the AggregateCatalog.