How to find TypeCatalog for particular type

Nov 25, 2010 at 7: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 4: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 7: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.

Glenn

Nov 26, 2010 at 7:31 PM

Here's a link to the api: http://msdn.microsoft.com/en-us/library/system.componentmodel.composition.reflectionmodel.reflectionmodelservices.getparttype(VS.95).aspx

Nov 27, 2010 at 7:18 AM

Thanks.

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
                     where
                     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.