Using COM-style programming interfaces
Assume that you do not use the COM library directly. Do not create your own wrapper. It is more likely that a COM-style programming interface is used. This is due to the fact that many developers now publish applications. Provides the preferred interop assembly (Primary interop Assemblies). This is a pre-created COM wrapper, so we don't have to consider using TlbImp.exe to create the wrapper ourselves.
Attention
Many other content about the preferred interop assembly. Can be found on MSDN. Http://msdn.microsoft.com/en-us/library/aax7sdch.aspx.
Although the preferred interop assembly is essentially a. NET assembly, there are usually some special places to pay attention to. Example:
Some arrays and collections often start at 1 instead of 0.
There are often some methods that will have a large number of optional parameters;
Many properties and methods return the object type, and the resulting object must be transformed into a true type;
COM classes include unmanaged resources. Need to process (dispose). However, these classes do not implement the standard. NET IDisposable interface, so. is not available in F # 's use bindings; We can use F # 's object expressions to implement IDisposable very easily.
The key difference between F # and COM for interacting with C # is that we must always create an instance of the object. Rather than an interface. This sounds 趚 a bit strange, but in a COM library, each object usually has an interface, and there is a class that implements the interface. In C #, it is assumed that using keyword new to attempt to create an instance of a COM interface, the COM posablesable compiler will itself direct to (redirect) invoke the corresponding class, which is not the case in F #.
Interacting with Microsoft Office may be the most common reason to use a COM style library.
The following checklist is for reading information from an EXCEL spreadsheet.
Open System
Open Microsoft.Office.Interop.Excel
Let main () =
//Initalize an Excel application
Let app = new ApplicationClass ()
//Load a Excel work book
Let WorkBook =app. Workbooks.Open (@ "Book1.xls", ReadOnly =true)
//Ensure work book is closed corectly
Use Bookcloser ={ new IDisposable with
Member X.dispose () = Workbook.close ()}
//Open the first worksheet
let worksheet =workbook.worksheets. [1]:?> _worksheet
//Get the A1 ceel and all surround cells
Let A1cell =worksheet. Range ("A1")
Let Allcells =a1cell.currentregion
//load all cells into a list of lists
Let matrix =
[ for Row in Allcells.rows
Let row = row:?> Range
[ for cell in row. Columns
Let cell = Cell:?
> Range
Cell. Value2]]
//Close the workbook
Workbook.close ()
//Print The Matrix
PRINTFN "%A" matrix
do Main ()
Notice how this demo sample deals with the special places we mentioned earlier. We implemented the IDisposable and bound it to bookcloser, ensuring that the workbook can be closed even in case of an error. The Open method has 15 parameters. But. We have only used two:. Open (@ "Book1.xls", ReadOnly = True). The index of the first worksheet is 1:workbook.worksheets. [1] Finally, each line must be cast (upcast) to be able to use: let row = Row:?> Range.