CachedDataItem.DataType Propriété

Définition

Obtient ou définit le nom qualifié par un assembly du type d'objet de données en mémoire cache.

public string DataType { get; set; }

Valeur de propriété

String

Nom qualifié par un assembly du type d'objet de données en mémoire cache.

Exemples

L’exemple de code suivant utilise la SerializeDataInstance méthode pour modifier la valeur d’une chaîne mise en cache dans une feuille de calcul d’un classeur Excel. Le code utilise la DataType propriété pour vérifier que l’objet de données mis en cache est une chaîne avant d’essayer de le modifier.

Cet exemple nécessite :

  • Une personnalisation au niveau du document pour Excel qui a une Sheet1 classe dans l' ExcelWorkbook1 espace de noms et une chaîne mise en cache dans la Sheet1 classe nommée CachedString .

  • Projet d’application console ou autre projet non-Office.

  • Références aux assemblys suivants :

    • Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll

    • Microsoft.VisualStudio.Tools.Applications.Runtime.dll

  • Imports(pour Visual Basic) ou using (pour C#) des instructions pour les Microsoft.VisualStudio.Tools.Applications Microsoft.VisualStudio.Tools.Applications.Runtime espaces de noms et en haut de votre fichier de code.

private void ModifyCachedString(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;

    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);

        if (runtimeVersion != 3)
        {
            MessageBox.Show("This document does not have a Visual Studio Tools for " +
                "Office customization, or it has a customization that was created with " +
                "a version of the runtime that is incompatible with this version of the " +
                "ServerDocument class.");
            return;
        }

        if (ServerDocument.IsCacheEnabled(documentPath))
        {
            serverDocument1 = new ServerDocument(documentPath);
            CachedDataHostItem hostItem1 = 
                serverDocument1.CachedData.HostItems["ExcelWorkbook1.Sheet1"];
            CachedDataItem dataItem1 = hostItem1.CachedData["CachedString"];

            if (dataItem1 != null &&
                Type.GetType(dataItem1.DataType) == typeof(string))
            {
                dataItem1.SerializeDataInstance("This is the new cached string value.");
                serverDocument1.Save();
            }
        }
        else
        {
            MessageBox.Show("The specified document does not have cached data.");
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (UnknownCustomizationFileException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document has a file " +
            "extension that is not supported by Visual Studio Tools for Office.");
    }
    finally
    {
        if (serverDocument1 != null)
            serverDocument1.Close();
    }
}
Private Sub ModifyCachedString(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing

    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion <> 3 Then
            MessageBox.Show("This document does not have a Visual Studio Tools for Office " & _
                "customization, or it has a customization that was created with a version of " & _
                "the runtime that is incompatible with this version of the ServerDocument class.")
            Return
        End If

        If ServerDocument.IsCacheEnabled(documentPath) Then
            serverDocument1 = New ServerDocument(documentPath)
            Dim hostItem1 As CachedDataHostItem = _
                serverDocument1.CachedData.HostItems("ExcelWorkbook1.Sheet1")
            Dim dataItem1 As CachedDataItem = hostItem1.CachedData("CachedString")

            If dataItem1 IsNot Nothing AndAlso _
                Type.GetType(dataItem1.DataType).Equals(GetType(String)) Then

                dataItem1.SerializeDataInstance("This is the new cached string value.")
                serverDocument1.Save()
            End If
        Else
            MessageBox.Show("The specified document does not have cached data.")
        End If

    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As UnknownCustomizationFileException
        System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
            "extension that is not supported by Visual Studio Tools for Office.")
    Finally
        If Not (serverDocument1 Is Nothing) Then
            serverDocument1.Close()
        End If
    End Try
End Sub

Remarques

La DataType propriété retourne un nom qualifié d’assembly du type de données mis en cache qui est utile pour créer une nouvelle instance d’un type personnalisé qui se trouve dans le cache de données. Par exemple, vous pouvez obtenir le Type d’un type de données personnalisé en passant le nom de type qualifié d’assembly à la GetType méthode. Vous pouvez ensuite créer une instance du type personnalisé en le transmettant Type en tant que paramètre à la System.Activator.CreateInstance méthode.

Pour plus d’informations sur le format de la chaîne de type qualifié d’assembly qui est retournée par la DataType propriété, consultez la System.Type.AssemblyQualifiedName propriété.

S’applique à