From: Fazil Peermohammed on
I'm writing a generic application that can read any .NET object which is a
wrapper on COM object (such as MS Exel 2007). I create the class in C# like
this:

ApplicationClass app = new ApplicationClass(); // the Excel application.

System.Reflection.PropertyInfo w = app.GetType().GetProperty("Workbooks");

Object Objbooks= w.GetValue(app, null);



Objbooks is now a System.__ComObject. My objective is to convert this to a
System.Object that represents Microsoft.Office.Interop.Excel.Workbooks. To
do this in C#, I would do



Microsoft.Office.Interop.Excel.Workbooks WorkBooks =
(Microsoft.Office.Interop.Excel.Workbooks)obj;



WorkBooks has all the properties and methods of Workbooks. How do I write
this casting algorithm dynamically so that I dont have to hard code any
type?



I've found a way to find the Type that represents
Microsoft.Office.Interop.Excel.Workbooks, but I just can't figure out how to
cast Objbooks to a System.Object of
Microsoft.Office.Interop.Excel.Workbooks.

I tried the generic method and reflection approach gievn in few sites, but I
still get a System.__ComObject without the properties of Workbooks.



Thanks

Fazil