none
Convert __ComObject to underlying object type RRS feed

  • Question

  • I can cast a com object with no problem, such as:

    ChartObject chart = selection as ChartObject;

    But if I try to look at the properties in selection using reflection, it fails because, while ChartObjects has a Parent property, __ComObject does not.

    Is there a way to turn this object from the __ComObject into the ChartObject type. And in this case, at runtime I can get several different object types so I can't do a cast in code, I need to use reflection.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Wednesday, October 24, 2018 9:43 PM

All replies

  • Hi David,

    >> Is there a way to turn this object from the __ComObject into the ChartObject type.

    >> And in this case, at runtime I can get several different object types so I can't do a cast in code, I need to use reflection.

    According to my research, you can try to put the __ComObject in a runtime callable wrapper, RCW.

    Please have a look at CreateWrapperOfType.

    For more information, please review the following link:

    Casting System.__ComObject to known type reflection

    Hopefully it helps you.

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.


    Thursday, October 25, 2018 9:10 AM
    Moderator
  • Hi Bruce;

    Ok, I tried that. But when I call:

    object selection = sheet.Application.ActiveWindow.Selection;
    selection = System.Runtime.InteropServices.Marshal.CreateWrapperOfType(selection, typeof(ChartObject));

    I get: {"The type must be __ComObject or be derived from __ComObject.\r\nParameter name: t"}

    What am I doing wrong?

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Thursday, October 25, 2018 3:20 PM
  • Hi David,

    Did you solve the problem?

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 30, 2018 2:28 AM
    Moderator
  • Nope. I gave up.

    I think this approach won't work with the interop marshalled objects. They can pretend to be the object that was marshalled for most uses, but I think reflection sees it as the underlying COMObject.

    thanks - dave


    What we did for the last 6 months - Made the world's coolest reporting & docgen system even more amazing

    Tuesday, October 30, 2018 12:22 PM