Why doesn't the implicit widening cast to Document work?

Why doesn't the implicit widening cast to Document work?

Anonymous
Not applicable
446 Views
2 Replies
Message 1 of 3

Why doesn't the implicit widening cast to Document work?

Anonymous
Not applicable

Hey all,

I've been working in the Inventor API (in C#) for some time and something that continues to bug me is that the widening cast from PartDocument, AssemblyDocument, etc to Document must be explicit. Everything that I have read leads me to believe that PartDocument is an interface that inherits from Document, so why do I get compile-time errors if I attempt:

Document doc = somePartDocument;

?

 

It sure would make writing more generic methods easier if the method signature could specify Document and I didn't have to explicitly cast every passed variable, or if actual generics could recognize that <T> where T : Document *includes* PartDocument, AssemblyDocument, etc.

 

Anyone else experience this, know why or happens or know a workaround?

0 Likes
447 Views
2 Replies
Replies (2)
Message 2 of 3

nmunro
Collaborator
Collaborator

I stems from the fact that they did not derive the specific document interfaces from the Document interface. If you look at the definition of say the PartDocument interface, it is a stand alone interface that repeats everything(?) included in the document interface plus part specific information. Since it has been this way from day one I doubt that it will ever be modified.

 

namespace Inventor
{
[DefaultMember("Type")]
[Guid("29F0D463-C114-11D2-B77F-0060B0F159EF")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[TypeLibType(TypeLibTypeFlags.FDispatchable)]
public interface PartDocument
{

...

}

        


https://c3mcad.com

0 Likes
Message 3 of 3

Anonymous
Not applicable

I did notice that. I don't know enough about how VS loads COM interfaces to know if that interface stub would even show that it was inherited.

 

Next question is why is that reimplemented instead of inherited, and then shouldn't the explicit cast fail as well?

0 Likes