Registering instance of COM Object

Sep 3, 2014 at 10:23 AM
Not sure whether this is an issue or i'm just using it wrong. I'm trying to register an instance of my COM Object using IUnityContainer method RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance).
After we updated to version 3.5.1404 (from 3.0.1304.1) it started to throw exception.
{"The type System.__ComObject cannot be assigned to variables of type Inventor.Application.\r\nParameter name: instance"}
After we dug into the code, we found that the method Guard.InstanceIsAssignable is throwing this exception. Because TypeInfo of the type is:
{Name = "Application" FullName = "Inventor.Application"}
and the TypeInfo of the object type is:
{Name = "__ComObject" FullName = "System.__ComObject"}
therefore Inventor.Application is not assignable from __System.ComObject.

The same method in older version worked fine because it was checking whether object was an instance of specified type.

My questions is: is it an issue or there is other way to register COM objects?
Sep 5, 2014 at 12:57 PM
The change between 3.0 and 3.5 in Guard.InstanceIsAssignable was from:
            if (!assignmentTargetType.GetTypeInfo().IsAssignableFrom(assignmentInstance.GetType().GetTypeInfo()))
            if (!assignmentTargetType.IsInstanceOfType(assignmentInstance))            
           if (!assignmentTargetType.GetTypeInfo().IsAssignableFrom(assignmentInstance.GetType().GetTypeInfo()))
The change was made to use the new reflection API, to rationalize the code base and better position Unity for multi-platform (PCL) support. The behavior is by design so for COM object support continue using Unity 3.0 or modify the source code to suit your needs.

Randy Levy
Enterprise Library support engineer
Support How-to