使用CustomImplementationFactory创建Memory Stream的自定义实现
Contents
[
Hide
]
可能的使用场景
Aspose.Cells提供了一个名为CellsHelper.CustomImplementationFactory的API,该API使用户能够提供自定义实现,例如使用可回收内存实现而不是默认的Memory Stream。
使用CustomImplementationFactory创建Memory Stream的自定义实现
以下示例代码说明了如何在程序中使用CellsHelper.CustomImplementationFactory。有时候在系统中有足够的内存,但内存不是连续的。Memory Stream对象使用连续内存,但您可以提供这样一种Memory Stream的实现,其方式是使用非连续内存。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Implement CustomImplementationFactory - CELLSNET-45461 | |
public class MM : CustomImplementationFactory | |
{ | |
RecyclableMemoryStreamManager manager = new RecyclableMemoryStreamManager(); | |
public override MemoryStream CreateMemoryStream() | |
{ | |
return manager.GetStream("MM"); | |
} | |
public override MemoryStream CreateMemoryStream(int capacity) | |
{ | |
return manager.GetStream("MM", capacity); | |
} | |
} | |
//---------------------------------------- | |
//---------------------------------------- | |
//Assign implementation instance of CustomImplementationFactory | |
CellsHelper.CustomImplementationFactory = new MM(); | |
//Light cells data handler implementation | |
LightCellsDataHandlerVisitCells v = new LightCellsDataHandlerVisitCells(); | |
//Create workbook object | |
Workbook wb = new Workbook(sourceDir, new LoadOptions() { MemorySetting = MemorySetting.MemoryPreference, LightCellsDataHandler = v }); | |
//Print miscellaneous data | |
Console.WriteLine("Total sheets: " + wb.Worksheets.Count + ", cells: " + v.CellCount + ", strings: " + v.StringCount + ", formulas: " + v.FormulaCount); |