jeudi 21 décembre 2017

Reflecting COM interop interfaces

I have a need to compare "Microsoft.Office.Interop.Publisher.Font" instances, so that I can tell if specific text range has the same text formatting as another range.

The Font interface has about 38 properties and a bunch of methods. This is what object browser shows and what I call successfully access in the code.

The first obvious one is to write a method which would compare property by property. Thankfully most of the properties are kind-of-bool values. Yet there are some advanced, like Color, which itself is ColorFormat interface with 9 properties, etc. All in all this sounds boring.

I thought, OK, why would not I serialize that into a string and compare results instead. I tried standard xml serialization and json.net, both return empty result. No errors, just empty. Ouch, COM interop interfaces cannot be serialized (well, they can with some workaround, but read on, it would not help anyway). What a pity, I do not need to deserialize the result anyway.

Then I thought, alright I can use reflection on Font interface and get all properties myself (isn't this is what serializers do anyway), and recursively build unique string comprised of values for comparison. So I went for "typeof(Font).GetMembers()". But... wait, only 22 members. How come?

Why object browser shows more members than reflection? Why reflection on COM interop interface (just this one) show only partial information? And how I can reflect missing members?

As a more generic questions, would anyone point me to an article which describes in detail what happens behind the scene when object browses queries COM interfaces for type information please?





Aucun commentaire:

Enregistrer un commentaire