user372724
user372724

Reputation:

What is the equivalent for VB's CreateObject in c# 3.0?

I have the below code in VB

Private Sub RefreshCharts()
On Error GoTo err_getICHELPER_REFRESH
    Application.Cursor = xlWait
    Dim objOperationRefresh
    Set objOperationRefresh = CreateObject("Charting.AutomationProxy")
    Dim a As Variant
    Set a = Application
    objOperationRefresh.RefreshApplication a
    Application.StatusBar = "Refreshing .. "
    Do While Len(objOperationRefresh.RefreshStatus) = 0
        DoEvents
    Loop
    Application.StatusBar = "Refreshing Complete."
    Set objOperationRefresh = Nothing
    Application.StatusBar = ""
    Exit Sub
err_getICHELPER_REFRESH:

I have done the conversion in C# except for CreateObject("Charting.AutomationProxy"). What will be the equivalent of that in c# 3.0?

Thanks

Upvotes: 0

Views: 3186

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You could use reflection:

Type type = Type.GetTypeFromProgID("Charting.AutomationProxy");
object instance = Activator.CreateInstance(type);

Contrary to VB and VB.NET, C# doesn't support dynamic types until C# 4.0. As this is a COM object another possibility is to generate a strongly typed wrapper which will greatly simplify the usage: in the Add Reference dialog select the component from COM components tab or use the tlbimp.exe utility.

Upvotes: 3

Related Questions