共用方式為


CachedDataItem.DataType 屬性 (2007 系統)

更新:2007 年 11 月

取得或設定這個型別為快取資料物件的組件限定名稱。

命名空間:  Microsoft.VisualStudio.Tools.Applications
組件:  Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0 (在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 中)

語法

Public Property DataType As String

Dim instance As CachedDataItem
Dim value As String

value = instance.DataType

instance.DataType = value
public string DataType { get; set; }

屬性值

型別:System.String

型別為快取資料物件的組件限定名稱。

備註

DataType 屬性會傳回型別為快取資料物件的組件限定名稱,這個名稱在建立自訂型別 (位於資料快取中) 的新執行個體時會很有用。例如,您可以藉由將組件限定型別名稱傳遞給 GetType 方法的方式,取得自訂資料型別的 Type。然後,便可以藉由將這個 Type 當做參數,傳遞給 Activator.CreateInstance 方法的方式,建立這個自訂型別的執行個體。

如需由 DataType 屬性傳回之組件限定型別字串格式的詳細資訊,請參閱 Type.AssemblyQualifiedName 屬性。

範例

下列程式碼範例會使用 SerializeDataInstance 方法修改 Excel 活頁簿工作表中快取的字串值。程式碼會使用 DataType 屬性,驗證快取資料物件是否為字串,然後再嘗試修改它。

這個範例要求在您的程式碼檔案頂端要有 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll 組件的參考,以及 Microsoft.VisualStudio.Tools.ApplicationsMicrosoft.VisualStudio.Tools.Applications.Runtime 命名空間的 Imports (適用於 Visual Basic) 或 using (適用於 C#) 陳述式。這個範例也會假設指定之活頁簿所含的自訂,具有 ExcelWorkbook1 命名空間中的 Sheet1 類別,以及具有名為 CachedString 的 Sheet1 類別中的快取字串。

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
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();
    }
}

使用權限

請參閱

參考

CachedDataItem 類別

CachedDataItem 成員

Microsoft.VisualStudio.Tools.Applications 命名空間