0

Is there in C# connect to COM-object and use contents of com-object such as in the Builder c++:

CreateOleObject("some.someClass");

(OLE C # seems to be not supported, except OLEDb, but it is not the current case in my opinion) I know I can add a link -> COM -> Seeking a registered com-object.

But it does not fit.

1

1 Answer 1

2

Normally, if you need to use a COM object in C#, you would add it as a Reference, and select the registered type library. That will generate an Interop Assembly, after which you can use the COM object just like any other C# class.

Alternatively, you can run the .NET utility tlbimp by hand, which has roughly the same effect but gives you slightly more control.

If you really need to create the object dynamically, without knowing anything about the type ahead of time, you can use the dynamic keyword and the Activator class to create a dynamic instance of a type. The code would look like:

var comType = Type.GetTypeFromProgID("some.someClass");
dynamic obj = Activator.CreateInstance(comType);

This will defer all type checking on obj until run-time, behaving much like VBA would.

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

Comments

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.