| Business Application Development with: SQL Server, C#, VB, VB.Net, ASP, ASP.Net, and XML |
|
|
News Links Schedule Site Map Contact |
|
Tips and Tricks for: C#, COMThe Problem: Creating a COM object in .Net based on its ProgIDOnce you've created a reference to a COM object on your system, how do you go about creating the object? You can't just use CreateObject or new. You have to use a few .Net methods from the System namespace. Solution: Use GetTypeFromProgID and Activator.CreateInstance.You can ask for the type using the GetTypeFromProgID method of the Type object. Then use the static method Activator.CreateInstance to create it from the Type. The COMCreateObject function is static so I usually include it in my NSLibStatic class. The sole purpose of the class is to hold static methods that I write. You can put it anywhere you like. Then put this method into one of your classes:
To call it, just supply the ProgID and the object will be instantiated and ready to be used:
I first wrote this function so that I could create an Internet Explorer object. There's a tip on that one also.
|
|
|
Copyright © 2003-2008 Novick Software, Inc. | Terms of Use | Privacy Policy | Nice Things People Say| |