Compartilhar via


Cache data

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can cache data objects in a document-level customization so that the data can be accessed offline, or without opening Microsoft Office Word or Microsoft Office Excel. To cache an object, the object must have a data type that meets certain requirements. Many common data types in the .NET Framework meet these requirements, including String, DataSet, and DataTable.

Applies to: The information in this topic applies to document-level projects for Excel and Word. For more information, see Features available by Office application and project type.

There are two ways to add an object to the data cache:

Requirements for data objects to be cached

To cache a data object in your solution, the object must meet these requirements:

  • Be a read/write public field or property of a host item, such as the ThisDocument or ThisWorkbook classes.

  • Not be an indexer or other parameterized property.

    In addition, the data object must be serializable by the XmlSerializer class, which means the type of the object must have these characteristics:

  • Be a public type.

  • Have a public constructor with no parameters.

  • Not execute code that requires additional security privileges.

  • Expose only read/write public properties (other properties will be ignored).

  • Not expose multi-dimensional arrays (nested arrays are accepted).

  • Not return interfaces from properties and fields.

  • Not implement IDictionary if a collection.

    When you cache a data object, the Visual Studio Tools for Office runtime serializes the object into an XML string that is stored in a custom XML part in the document. For more information, see Custom XML parts overview.

Cached data size limits

There are some limits to the total amount of data you can add to the data cache in a document, and to the size of any individual object in the data cache. If you exceed these limits, the application might close unexpectedly when the data is saved to the data cache.

To avoid these limits, follow these guidelines:

  • Do not add any object larger than 10 MB to the data cache.

  • Do not add more than 100 MB of total data to the data cache in a single document.

    These are approximate values. The exact limits depend on several factors, including the available RAM and the number of running processes.

Control the behavior of cached objects

To gain more control over the behavior of a cached object, you can implement the ICachedType interface on the type of the cached object. For example, you can implement this interface if you want to control how the user is notified when the object has been changed. For code examples that demonstrate how to implement ICachedType, see the ControlCollection class in the Excel Dynamic Controls Sample and Word Dynamic Controls Sample in Office development samples and walkthroughs.

Persist changes to cached data in password-protected documents

If you cache data objects in a document that is protected with a password, changes to the cached data are not saved. You can save changes to the cached data by overriding two methods. Override these methods to temporarily remove the protection when the document is saved, and then reapply the protection after the save operation is complete.

For more information, see How to: Cache data in a password-protected document.

Prevent data loss when adding null values to the data cache

When you add objects to the data cache, all of the cached objects must be initialized to a non-null value before the document is saved and closed. If any cached object has a null value when the document is saved and closed, the Visual Studio Tools for Office runtime will automatically remove all of the cached objects from the data cache.

If you add an object with a null value to the data cache by using the CachedAttribute attribute at design time, you can use the ServerDocument class to initialize the cached data objects before the document is opened. This is useful if you want to initialize the cached data on a server without Word or Excel installed, before the document is opened by an end user. For more information, see Access data in documents on the server.

See also