0

I'm working with a COM object and I need to be able to search for properties and methods at runtime via Reflection and invoke them based on an input string that roughly matches the COM object's structure.

I'm currently using a recursive algorithm to walk the COM object's properties at runtime using obj.GetType().InvokeMember() but this only works if I'm supplied the exact property and method names, which is not always the case. Examples:

"ALPha.BETa.GAMma?" -> var gamma = comObj.ALPha.BETa.GAMma;
"ALP.BET.GAM?" -> var gamma = comObj.ALPha.BETa.GAMma;
"ALP.BET.GAM 200" -> comObj.ALPha.BETa.GAMma = 200;
"ALPha.DELta" -> comObj.ALPha.DELta();

(if anyone's familiar with SCPI, this is what I'm actually working with -- trying to dynamically convert SCPI strings to calls to a COM object's API at runtime)


Stray observations:

  • typeof(IComObject).GetProperties() only returns properties that have been previously evaluated at runtime which is useless for me.
  • Intellisense is fully available for all properties and methods on the COM object.
  • The debugger shows comObj's type as System.__ComObject at runtime.
  • I've tried running Tlbimp.exe on the COM type library exe to produce an interop assembly but I see the same results as before when using GetProperties().
  • There's at least 200 properties and methods on the COM object so there's no way I can manually build up the mappings.
4
  • 1
    Reflection is a feature of .NET, not of COM. That's where it ends. This product has seriously unusable naming conventions, POWer.LEVel.IMMediate.AMPLitude is just bizarre. But there's probably some rhyme to that madness, upper-case letters one by one until it stops complaining. Commented Jul 6, 2016 at 0:04
  • How does Visual Studio know about the COM object's members at compile time in order to populate Intellisense? Shouldn't there be some way for me to replicate this sort of behaviour in order to enumerate and search for members at runtime using Reflection? Commented Jul 6, 2016 at 17:01
  • please elaborate on the statement "I've tried running Tlbimp.exe on the COM type library exe to produce an interop assembly but I see the same results as before when using GetProperties()", how exactly where you calling the assembly generated by tlbimp.exe? Commented Jul 6, 2016 at 18:10
  • The COM type library is distributed as part of an exe. I used Tlbimp.exe on that exe to create an interop assembly. Inside Visual Studio I added a reference to this new interop assembly instead of using the regular COM type library reference. From what I understand, VS automatically creates an assembly from the COM type library and outputs it in the /obj/Debug folder so that would explain why I see no difference between the two since they're the same? Commented Jul 6, 2016 at 20:24

1 Answer 1

1

To expand on @HansPassant comment, COM just doesn't do reflection. Late-binding COM (via IDispatch) is done via exact string match. Probably your best bet is to decode the typelib yourself (I'd start with this tool) and build a manual mapping from strings to methods and properties. (Yes, you're manually reimplementing reflection.)

Sign up to request clarification or add additional context in comments.

1 Comment

Not the answer I was hoping for since it adds a lot of extra work on my side, but thanks for the info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.