Equivalent code of CreateObject in C#
If you are using .net 4 or later, and therefore can make use of dynamic
, you can do this quite simply. Here's an example that uses the Excel automation interface.
Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
dynamic ExcelInst = Activator.CreateInstance(ExcelType);
ExcelInst.Visible = true;
If you can't use dynamic then it's much more messy.
Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
object ExcelInst = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible", BindingFlags.SetProperty, null,
ExcelInst, new object[1] {true});
Trying to do very much of that will sap the lifeblood from you.
COM is so much easier if you can use early bound dispatch rather than late bound as shown above. Are you sure you can't find the right reference for the COM object?
Equivalent code of CreateObject in VB.net
VB.Net way, no late binding as you can create the objects directly from the library. Clean them up with the Marshal class since in it a COM object - in reverse order.
Dim objXLS As New Excel.Application
Dim objWorkBook As Excel.Workbook = objXLS.Workbooks.Open("Excel File Goes Here")
objXLS.Visible = False
'work with file
objWorkBook.SaveAs strCurPath & "\Temp.csv", 6
objWorkBook.Close 2
objXLS.Quit
Marshall.FinalReleaseComObject(objWorkBook)
Marshall.FinalReleaseComObject(objXLS)
How to CreateObject in C#?
It is not a constructor call. The sHost variable contains the name of a machine, the one that provides the out-of-process COM server. The equivalent functionality is provided by Type.GetTypeFromProgId(), using the overload that allows specifying the server name:
var t = Type.GetTypeFromProgID("Exfo.IcSCPIActiveX.IcSCPIAccess", sHost, true);
obj = (IcSCPIAccess)Activator.CreateInstance(t);
I named it "obj", do avoid giving variables the same name as the interface type. A gazillion things can still go wrong, having the COM server properly registered both on the client and server machine and setting the DCOM security correctly is essential to make this code work. Don't try this until you are sure that the original code works properly.
Equivalence of CreateObject(Microsoft.XMLHTTP)
Look up WebRequest.
See examples section.
How to create object by assigning other object with similar fields
You can do this using an implicit conversion operator:
class CopyClass
{
public int x;
public int y;
public CopyClass(FullClass fullObject)
{
x = fullObject.x;
y = fullObject.y;
}
public static implicit operator CopyClass(FullClass fc) => new CopyClass(fc);
}
But this somewhat obfuscates your intent.
Personally I'd go with new CopyClass(fullObject);
, as that makes clear you are creating a copy of a different type.
Related Topics
Hide Form Instead of Closing When Close Button Clicked
Serilog Mssql Sink Doesn't Write Logs to Database
Encrypting/Decrypting Large Files (.Net)
Wpf Binding - Default Value for Empty String
How to Convert String to Integer in C#
How to Intercept Console Output
C# Automatic Property Deserialization of JSON
How to Get the Subscribers of an Event
Get Instance of Excel Application with C# by Handle
How to Get the Colour of a Pixel at X,Y Using C#
How to Check If Ioexception Is Not-Enough-Disk-Space-Exception Type
Determine Number of Pages in a PDF File
..The Underlying Connection Was Closed: an Unexpected Error Occurred on a Receive
How to Put Conditional Required Attribute into Class Property to Work with Web API
Creating an Anonymous Type Dynamically
Best Practice: Convert Linq Query Result to a Datatable Without Looping