DataObject.SetData DataObject.SetData DataObject.SetData DataObject.SetData Method

定义

将对象添加到 DataObject 中。Adds an object to the DataObject.

重载

SetData(Object) SetData(Object) SetData(Object) SetData(Object)

使用对象类型作为数据格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the object type as the data format.

SetData(String, Object) SetData(String, Object) SetData(String, Object) SetData(String, Object)

使用指定格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified format.

SetData(Type, Object) SetData(Type, Object) SetData(Type, Object) SetData(Type, Object)

使用指定类型作为格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified type as the format.

SetData(String, Boolean, Object) SetData(String, Boolean, Object) SetData(String, Boolean, Object) SetData(String, Boolean, Object)

使用指定格式将指定对象添加到 DataObject 并指示这些数据是否可以转换为其他格式。Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.

SetData(Object) SetData(Object) SetData(Object) SetData(Object)

使用对象类型作为数据格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the object type as the data format.

public:
 virtual void SetData(System::Object ^ data);
public virtual void SetData (object data);
abstract member SetData : obj -> unit
override this.SetData : obj -> unit
Public Overridable Sub SetData (data As Object)

参数

data
Object Object Object Object

要存储的数据。The data to store.

实现

示例

下面的代码示例将DataObject数据存储在中。The following code example stores data in a DataObject. 首先, 创建一个DataObject新的, 并在其中存储一个组件。First, a new DataObject is created and a component is stored in it. 然后, 通过指定类来检索数据。Then, the data is retrieved by specifying the class. 结果将显示在文本框中。The result is displayed in a text box.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

private:
   void AddMyData3()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      Type^ myType = myComponent->GetType();
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is not present in the DataObject" );
      }
   }
private void AddMyData3() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    Type myType = myComponent.GetType();
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
    
Private Sub AddMyData3()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    Dim myType As Type = myComponent.GetType()
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub 'AddMyData3

注解

重要

使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证For more information, see Data Validation.

如果您不知道目标应用程序的格式, 则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method. 在检索使用此方法存储的数据时, 可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

重载通过调用Object.GetType方法data将值存储为它确定的格式。 SetData(Object)The SetData(Object) overload stores the data value in a format that it determines by calling the Object.GetType method. 如果data Serializable实现接口, 此重载还会将值存储为格式。 ISerializableIf data implements the ISerializable interface, this overload also stores the value in the Serializable format.

另请参阅

SetData(String, Object) SetData(String, Object) SetData(String, Object) SetData(String, Object)

使用指定格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified format.

public:
 virtual void SetData(System::String ^ format, System::Object ^ data);
public virtual void SetData (string format, object data);
abstract member SetData : string * obj -> unit
override this.SetData : string * obj -> unit
Public Overridable Sub SetData (format As String, data As Object)

参数

format
String String String String

与数据关联的格式。The format associated with the data. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

data
Object Object Object Object

要存储的数据。The data to store.

实现

示例

下面的代码示例将数据存储在DataObject中, 并将其格式指定为 Unicode。The following code example stores data in a DataObject, specifying its format as Unicode.

然后通过指定文本格式来检索数据, 因为默认情况下, 在最终格式兼容时转换数据。Then the data is retrieved by specifying the text format, since the default is to convert the data when the final format is compatible. 结果将显示在文本框中。The result is displayed in a text box.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

private:
   void AddMyData()
   {
      // Creates a new data object using a string and the text format.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Stores a string, specifying the Unicode format.
      myDataObject->SetData( DataFormats::UnicodeText, "Text string" );
      
      // Retrieves the data by specifying Text.
      textBox1->Text = myDataObject->GetData( DataFormats::Text )->GetType()->Name;
   }
private void AddMyData() {
    // Creates a new data object using a string and the text format.
    DataObject myDataObject = new DataObject();
 
    // Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string");
 
    // Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name;
 }
 
Private Sub AddMyData()
    ' Creates a new data object using a string and the text format.
    Dim myDataObject As New DataObject()
    
    ' Stores a string, specifying the Unicode format.
    myDataObject.SetData(DataFormats.UnicodeText, "Text string")
    
    ' Retrieves the data by specifying Text.
    textBox1.Text = myDataObject.GetData(DataFormats.Text).GetType().Name
End Sub 'AddMyData

注解

重要

使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证For more information, see Data Validation.

如果您不知道目标应用程序的格式, 则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

在检索使用此方法存储的数据时, 可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

另请参阅

SetData(Type, Object) SetData(Type, Object) SetData(Type, Object) SetData(Type, Object)

使用指定类型作为格式将指定对象添加到 DataObjectAdds the specified object to the DataObject using the specified type as the format.

public:
 virtual void SetData(Type ^ format, System::Object ^ data);
public virtual void SetData (Type format, object data);
abstract member SetData : Type * obj -> unit
override this.SetData : Type * obj -> unit
Public Overridable Sub SetData (format As Type, data As Object)

参数

format
Type Type Type Type

表示与数据关联的格式的 TypeA Type representing the format associated with the data.

data
Object Object Object Object

要存储的数据。The data to store.

实现

示例

下面的代码示例DataObject Type使用作为数据格式将数据存储在中。The following code example stores data in a DataObject using a Type as the data format. 然后通过调用GetData Type来检索数据, 以指定数据格式。The data is then retrieved by calling GetData using the Type to specify the data format. 结果将显示在文本框中。The result is displayed in a text box.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

private:
   void AddMyData2()
   {
      // Creates a component to store in the data object.
      Component^ myComponent = gcnew Component;
      
      // Gets the type of the component.
      Type^ myType = myComponent->GetType();
      
      // Creates a new data object.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds the component to the DataObject.
      myDataObject->SetData( myType, myComponent );
      
      // Prints whether data of the specified type is in the DataObject.
      if ( myDataObject->GetDataPresent( myType ) )
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
            " is present in the DataObject" );
      }
      else
      {
         textBox1->Text = String::Concat( "Data of type ", myType->Name,
           " is not present in the DataObject" );
      }
   }
private void AddMyData2() {
    // Creates a component to store in the data object.
    Component myComponent = new Component();
 
    // Gets the type of the component.
    Type myType = myComponent.GetType();
 
    // Creates a new data object.
    DataObject myDataObject = new DataObject();
 
    // Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent);
 
    // Prints whether data of the specified type is in the DataObject.
    if(myDataObject.GetDataPresent(myType))
       textBox1.Text = "Data of type " + myType.GetType().Name + 
       " is present in the DataObject";
    else
       textBox1.Text = "Data of type " + myType.GetType().Name +
       " is not present in the DataObject";
 }
 
Private Sub AddMyData2()
    ' Creates a component to store in the data object.
    Dim myComponent As New Component()
    
    ' Gets the type of the component.
    Dim myType As Type = myComponent.GetType()
    
    ' Creates a new data object.
    Dim myDataObject As New DataObject()
    
    ' Adds the component to the DataObject.
    myDataObject.SetData(myType, myComponent)
    
    ' Prints whether data of the specified type is in the DataObject.
    If myDataObject.GetDataPresent(myType) Then
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is present in the DataObject"
    Else
        textBox1.Text = "Data of type " & myType.GetType().Name & _
            " is not present in the DataObject"
    End If
End Sub 'AddMyData2

注解

重要

使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证For more information, see Data Validation.

如果您不知道目标应用程序的格式, 则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

在检索使用此方法存储的数据时, 可以将其转换为兼容的格式。Data stored using this method can be converted to a compatible format when it is retrieved.

另请参阅

SetData(String, Boolean, Object) SetData(String, Boolean, Object) SetData(String, Boolean, Object) SetData(String, Boolean, Object)

使用指定格式将指定对象添加到 DataObject 并指示这些数据是否可以转换为其他格式。Adds the specified object to the DataObject using the specified format and indicating whether the data can be converted to another format.

public:
 virtual void SetData(System::String ^ format, bool autoConvert, System::Object ^ data);
public virtual void SetData (string format, bool autoConvert, object data);
abstract member SetData : string * bool * obj -> unit
override this.SetData : string * bool * obj -> unit
Public Overridable Sub SetData (format As String, autoConvert As Boolean, data As Object)

参数

format
String String String String

与数据关联的格式。The format associated with the data. 请参见 DataFormats 以获取预定义的格式。See DataFormats for predefined formats.

autoConvert
Boolean Boolean Boolean Boolean

如果允许将数据转换成另一格式,值为 true;否则,值为 falsetrue to allow the data to be converted to another format; otherwise, false.

data
Object Object Object Object

要存储的数据。The data to store.

实现

示例

下面的代码示例将DataObject数据存储在中, 并指定只能以本机格式检索数据。The following code example stores data in a DataObject and specifies that the data can only be retrieved in its native format.

首先, 创建一个DataObject新的。First, a new DataObject is created. Unicode 格式的数据存储在DataObject中, 并autoConvert将设置为falseData in the Unicode format is stored in the DataObject, with autoConvert set to false.

然后, DataObject查询以获取可用数据格式的列表。Then, the DataObject is queried for the list of available data formats. 仅返回 Unicode 格式, 尽管 Unicode 数据可以转换为文本和其他格式。Only the Unicode format is returned, although Unicode data can be converted to text and other formats.

此代码需要已textBox1创建的。This code requires that textBox1 has been created.

private:
   void AddMyData4()
   {
      // Creates a new data object, and assigns it the component.
      DataObject^ myDataObject = gcnew DataObject;
      
      // Adds data to the DataObject, and specifies no format conversion.
      myDataObject->SetData( DataFormats::UnicodeText, false, "My Unicode data" );
      
      // Gets the data formats in the DataObject.
      array<String^>^ arrayOfFormats = myDataObject->GetFormats();
      
      // Prints the results.
      textBox1->Text = "The format(s) associated with the data are: \n";
      for ( int i = 0; i < arrayOfFormats->Length; i++ )
      {
         textBox1->Text = String::Concat( textBox1->Text, arrayOfFormats[ i ], "\n" );
      }
   }
private void AddMyData4() {
    // Creates a new data object, and assigns it the component.
    DataObject myDataObject = new DataObject();
 
    // Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, false, "My Unicode data");
 
    // Gets the data formats in the DataObject.
    String[] arrayOfFormats = myDataObject.GetFormats();
 
    // Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " + '\n';
    for(int i=0; i<arrayOfFormats.Length; i++)
       textBox1.Text += arrayOfFormats[i] + '\n';
 }
 
Private Sub AddMyData4()
    ' Creates a new data object, and assigns it the component.
    Dim myDataObject As New DataObject()
    
    ' Adds data to the DataObject, and specifies no format conversion.
    myDataObject.SetData(DataFormats.UnicodeText, False, "My Unicode data")
    
    ' Gets the data formats in the DataObject.
    Dim arrayOfFormats As String() = myDataObject.GetFormats()
    
    ' Prints the results.
    textBox1.Text = "The format(s) associated with the data are: " & ControlChars.Cr
    Dim i As Integer
    For i = 0 To arrayOfFormats.Length - 1
        textBox1.Text += arrayOfFormats(i) & ControlChars.Cr
    Next i
End Sub 'AddMyData4 

注解

重要

使用不受信任的数据调用此方法存在安全风险。Calling this method with untrusted data is a security risk. 仅使用受信任的数据调用此方法。Call this method only with trusted data. 有关详细信息,请参阅数据验证For more information, see Data Validation.

如果您不知道目标应用程序的格式, 则可以使用此方法以多种格式存储数据。If you do not know the format of the target application, you can store data in multiple formats using this method.

另请参阅

适用于