Link to home
Start Free TrialLog in
Avatar of MikeP090797
MikeP090797

asked on

CreateObject in C#

The title says it all: I need a functionality simular to CreateObject in C#. I need to be able to create an instance of a COM object, without first referencing it at design time, and then use it's properties (which are also unknown, since I do late binding, or whatever it's name in C#)

I'm sure there is a simple solution, but I'm new to C# (I come from a VB background)
Please help
Thanks
SOLUTION
Avatar of _TAD_
_TAD_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MikeP090797
MikeP090797

ASKER

The problem with this is that I don't know the type
Can you give me the C# for the following code:


    'Start excel, create new document, and set reference to the first sheet
            Dim m_Application as object = CreateObject("Excel.Application")
            Dim m_Workbook as object = Application.Workbooks.Add
            Dim m_Sheet as object = Workbook.Worksheets(1)
            m_Sheet.Range("A1") = "Hello"



Right click on the References in the Solution Explorer in your solution. Select COM objects and find Microsoft.Excel. Add a reference to it by double clicking it and then selecting OK. Then you can use classes like Excel.ApplicationClass for example.

The beginning of your code is:
Excel.ApplicationClass app = new Excel.ApplicationClass();
Excel.WorkbookClass workbook = (Excel.WorkbookClass)app.Workbooks.Add("");
object o = workbook.Sheets[1];
// Works up to here
Excel.WorksheetClass sheet = (Excel.WorksheetClass)o; // -> this is not a valid cast, but probably you can figure it out easily from here on
sheet.Cells[0, 0] = "Hello";


If you want to use excel, then the code provided by avenger should be sufficient.

If you were speaking more generically, then the code I posted above should put you on the right path.


As for not knowing what type of object it is, that is what the following line is for:

Foreach(Type t in obj.GetTypes())
{

}







I want to use excel, but without setting the reference first...
In VB.NET, I can use CreateObject, assign the result to an object, and then call any method using that object variable, even without the compiler knowing what it is (since CreateObject accepts a string). That process is called in VB late binding, and something simular won't even compile in C#. What you have described is called early binding...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


What Avenger said is mostly true, but not exactly.

Late-Binding in C#

using System.Runtime.InteropServices;

Type myObjType;
object myObjValue;

myObjType = Type.GetTypeFromProgID("Excel.Application");
myObjValue = Activator.CreateInstance(myObjType);

object myWorkBook;

myObjType.InvokeMember("WorkBook.Add", BindingFlags.InvokeMethod,null,myObjValue,"MyWrkBook");



The problem with this method is that you can't use intellisense and you don't have a reference to the COM object's type library.  Not to mention it is slow.


I have only used something like this when trying to implement Plugins where I don't know anything about the COM object..... but then again, even at that I can cast it as a particular type using interfaces (provided that an interface is specified as a requirement for the plug-in).


In short, I think there must be a better way to accomplish what you are trying to do, but if you must use late-binding... good luck to you!

btw... why *must* it be late binding?  Wouldn't early binding be a lot more benificial to you (intellisense, speed, accuracy, etc)?
The reason I wanted to use late binding for this one was because I already have an existing framework in VB.NET (.DLL) that handles some of the operations in excel. I have a class that provides various methods of formatting and managment of excel objects, and also returns references to excel's internal objects, such as worksheet, so that the application can use them. I didn't want to add the references to the framework itself since all my applications use it and some don't require/have excel available to them.
Anyway, I guess Ill add the reference to the C# app and do the casting into the matching objects...


Sounds interesting, I hope your shortcut works!  Good luck.
10x _TAD_, always like to learn something new...