Injecting COM objects throws an exception


Hi Guys,
ObjectBuilding my way through the visual studio IDE I ran into a problem when injecting Com-objects (such as the realization EnvDTE.DTE).
problem occurs when creating an object that gets an COM-object injected in its constructor.
Problem boils down to the Guard.TypeIsAssignableFromType throwing an exception if the found type is not assignablable from the constructor-paramters type.
if (!assignee.IsAssignableFrom(providedType))
throw new IncompatibleTypesException(....);
My workaround (the quickest, problably not the best -but here to provide insight on the issue)
if (!providedType.IsCOMObject)
if (!assignee.IsAssignableFrom(providedType))
throw new IncompatibleTypesException(....);


dcazzulino wrote Sep 22, 2006 at 10:15 PM

Yup, good catch. I had some more complex logic performing a query interface if the type is a COM object... will see if I can bring that in (it was part of the older ComponentModel-based injection)

wrote Nov 8, 2006 at 3:45 AM

wrote Nov 11, 2006 at 7:57 PM

wrote Nov 17, 2006 at 3:41 PM

wrote Dec 23, 2006 at 1:45 PM

wrote Jun 8, 2007 at 2:14 AM

yurik wrote Feb 13, 2008 at 4:33 PM

I ran into a similar problem, but with remoting objects. My solution was to add
&& providedType != typeof(MarshalByRefObject)
to the Guard.TypeIsAssignableFromType(), which solved it for any objects created through remoting.

wrote Feb 14, 2013 at 12:58 AM