1

当我尝试以下结果时,结果出乎意料。任何想法将不胜感激!

    Class DataObjectSub
        Inherits DataObject

    End Class

    Sub Test()

        Dim myObject = New DataObjectSub

        myObject.SetData("Test")
        Clipboard.SetDataObject(myObject)

        Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject
        Console.Write(myRetrievedObject.GetType.ToString)

        Dim myRetrievedObject2=TryCast(Clipboard.GetDataObject,DataObjectSub)

    End Sub

myRetrievedObject2 什么都不是

Write 语句的输出是:“System.Windows.Forms.DataObject”。不应该是“DataObjectSub”吗?我错过了一些明显的东西吗?

按要求编辑:

在拖放操作的情况下,DataObject(实现 iDataObject)再次被使用,可以这样做:

Sub TestDragDrop

Dim myObject as iDataObject=new DataObjectSub

someControl.DoDragDrop(myObject, myAllowedEffects)

End Sub

稍后...在 DragOver 事件处理程序中:

Sub anotherControl_DragOver(sender as object, e as DragEventArgs)

Dim myRetrievedObject1 as DataObjectSub = TryCast(e.Data, DataObjectSub)
Dim myRetrievedObject2 as IDataObject = e.Data


End Sub

运行良好,myRetrievedObject1 和 myRetrievedObject2 都不是 Nothing。两个检索到的对象的类型都是 DataObjectSub。

感谢您的任何想法和耐心!:)

4

1 回答 1

1

这不是意外的行为。Clipboard.GetDataObject只需将数据存储在Clipboard(由MSDN引用),其类型为DataObject(由您的代码引用)。当您检查其类型时,输入变量是DataObject还是任何其他类型(由 支持GetDataObject)都无关紧要。请记住,这GetDataObject是一个方法,其返回值不需要遵循默认的分配规则(例如,调用它不会引起与将变量分配给String变量相同的效果Object)。

这段代码:

Dim myString As String = "Test"
Clipboard.SetDataObject(myString)
Dim myRetrievedObject As IDataObject = Clipboard.GetDataObject
Console.Write(myRetrievedObject.GetType.ToString)

输出与您的代码完全相同的结果。myRetrievedObject.GetType.ToString只检查 的类型GetDataObject,它总是DataObject.

更新

在我们讨论之后,我想澄清一下Clipboard.GetDataObject返回一个IDataObject变量(的接口DataObject,不是不同的类型)。理解这一切的示例代码:

Dim myRetrievedObject1 As IDataObject = Clipboard.GetDataObject
Dim myRetrievedObject2 As DataObject = New DataObject(Clipboard.GetDataObject)
Dim myRetrievedObject3 As DataObject = DirectCast(Clipboard.GetDataObject, DataObject)
Dim test4 As DataObject = New DataObject

If (TypeOf myRetrievedObject1 Is DataObject AndAlso TypeOf myRetrievedObject2 Is DataObject AndAlso TypeOf myRetrievedObject3 Is DataObject AndAlso TypeOf test4 Is DataObject) Then
    'Condition is met
End If
If (TypeOf myRetrievedObject1 Is IDataObject AndAlso TypeOf myRetrievedObject2 Is IDataObject AndAlso TypeOf myRetrievedObject3 Is IDataObject AndAlso TypeOf test4 Is IDataObject) Then
    'Condition is met
End If

如您所见(满足这两个条件,意味着所有变量同时具有类型DataObjectIDataObject,关系DataObject/IDataObject不像是两种不同类型之间的关系。实际上,如 所示myRetrievedObject3,强制转换只是形式上的要求(我将DataObject变量强制转换为DataObject!)。DataObject/IDataObject基本上是同一枚硬币的两个面(称为DataObject。这意味着以下条件为真:

Dim Type1 As String = myRetrievedObject1.GetType().ToString()
Dim Type2 As String = myRetrievedObject2.GetType().ToString()
Dim Type3 As String = myRetrievedObject3.GetType().ToString()
Dim Type4 As String = test4.GetType().ToString()

If (Type1 = Type2 AndAlso Type1 = Type3 AndAlso Type1 = Type4 AndAlso Type1 = GetType(DataObject).ToString) Then
    'Condition is met
End If

也就是说,DataObjectIDataObject变量的类型名称是DataObject(或者,更准确地说:)System.Windows.Forms.DataObject

于 2013-12-04T15:18:36.527 回答