PC Review


Reply
Thread Tools Rate Thread

Dynamic Casting in C#

 
 
Fazil Peermohammed
Guest
Posts: n/a
 
      1st Sep 2009
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




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Casting Philipp Schumann Microsoft C# .NET 5 20th Apr 2005 09:52 PM
Dynamic casting and dynamic variables. Martin Hart - Memory Soft, S.L. Microsoft Dot NET Framework 3 6th Apr 2004 08:17 AM
Dynamic casting and dynamic variables. Martin Hart - Memory Soft, S.L. Microsoft C# .NET 2 6th Apr 2004 07:07 AM
Dynamic casting =?Utf-8?B?VmFs?= Microsoft C# .NET 4 19th Jan 2004 09:51 AM
Dynamic Casting Jeff Poste Microsoft C# .NET 3 19th Dec 2003 12:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:13 PM.