我有兩個 WinForms 應用程式,并且希望能夠將物件從一個拖動到另一個。
我的資料物件代碼很簡單:
// the data object
[ComVisible(true)]
[Serializable]
public class MyData : ISerializable {
public int Value1 { get; set; }
public int Value2 { get; set; }
public MyData() { }
public MyData(int value1, int value2) {
Value1 = value1;
Value2 = value2;
}
public void GetObjectData(SerializationInfo info, StreamingContext context) {
info.AddValue(nameof(Value1), Value1);
info.AddValue(nameof(Value2), Value2);
}
}
該物件是 dll 的一部分,在我的兩個 WinForms 應用程式中都參考了它。
我正在使用以下方法初始化拖放:
// inside some control
MyData toBeTransmitted = new MyData(0, 0);
IDataObject dataObject = new DataObject(DataFormats.Serializable, toBeTransmitted);
this.DoDragDrop(dataObject, DragDropEffects.All);
并使用以下方法處理它:
// inside some drag over handler
IDataObject dataObject = dragEvent.DataObject;
if (dataObject.GetDataPresent(DataFormats.Serializable)) {
object obj = e.DataObject.GetData(DataFormats.Serializable);
}
只要我在單個應用程式中拖放資料,所有這些都可以正常作業。但是,只要我將資料從一個行程拖到另一個行程,檢索拖動的資料就會回傳一個型別的物件,System.__ComObject而不是MyData.

如何檢索包含在 中的實際資料IDataObject?
(注意:我也嘗試使用自定義格式而不是DataFormats.Serializable,那里沒有運氣。)
uj5u.com熱心網友回復:
問題:指定
時DataFormats.Serializable,DataObject 類使用 BinaryFormatter 序列化實作的類物件ISerializable(順便說一句,您應該添加一個public MyData(SerializationInfo info, StreamingContext context)建構式)。
BinaryFormatter 物件是使用具有默認 Binder 的標準形式創建的,這意味著序列化物件包含嚴格的程式集資訊。
因此,您可以毫無問題地將物件拖放到代表同一程式集實體的行程中,但如果程式集不同或它們的版本不匹配,則 BinaryFormatter 反序列化失敗,您會得到一個結果解開IComDataObject。
您可以自己編組這個 COM 物件,這意味著您必須構建一個兼容的FORMATETC結構物件 ( System.Runtime.InteropServices.ComTypes.FORMATETC),從獲取STGMEDIUM ,使用Marshal.GetObjectForIUnknown()IDataObject.GetData([FORMATETC], out [STGMEDIUM])獲取IStream物件,并傳遞指標。
然后創建一個 BinaryFormatter 指定一個限制較少/自定義的 Binder并反序列化 Stream 忽略或替換程式集名稱。[STGMEDIUM].unionmember
在你問之前,你不能[SerializationInfo].AssemblyName直接在你的 ISerializable 類中設定(即使它不是只讀的),這是行不通的。
一種可能的解決方案:
一種簡單的方法是用不同的序列化程式替換 BinaryFormatter 并創建 IDataObject 設定自定義格式(或DataFormat與生成的資料兼容的預定義格式)。
使用 XmlSerializer 作為序列化程式和 MemoryStream 的示例:
可以簡化類物件,洗掉ISerializable實作:
[Serializable]
public class MyData {
public int Value1 { get; set; }
public int Value2 { get; set; }
public MyData() { }
public MyData(int value1, int value2)
{
Value1 = value1;
Value2 = value2;
}
public override string ToString()
{
return $"Value1: {Value1}, Value2: {Value2}";
}
}
用于在側面生成 IDataObject 并在Source側面提取其內容的兩個靜態方法Target。XmlSerializer用于序列化/反序列化類物件。
private static IDataObject SetObjectData<T>(object value, string format) where T : class
{
using (var ms = new MemoryStream())
using (var sw = new StreamWriter(ms)) {
var serializer = new XmlSerializer(typeof(T), "");
serializer.Serialize(sw, value);
sw.Flush();
var data = new DataObject(format, ms.ToArray());
// Failsafe custom data type - could be a GUID, anything else, or removed entirely
data.SetData("MyApp_DataObjectType", format);
return data;
};
}
private static T GetObjectData<T>(IDataObject data, string format) where T : class
{
// Throws if the byte[] cast fails
using (var ms = new MemoryStream(data.GetData(format) as byte[])) {
var serializer = new XmlSerializer(typeof(T));
var obj = serializer.Deserialize(ms);
return (T)obj;
}
}
In the example, I'm using a Dictionary<string, Action> to call methods nased on the Type contained in the IDataObject received from the DragDrop operation.
This because I suppose you could transfer different Types. Of course you can use anything else.
You could also use a common Interface and just use this as <T>. It would simplify a lot the whole implementation (and future expansion, if generic enough methods and properties can be defined).
Dictionary<string, Action<IDataObject>> dataActions = new Dictionary<string, Action<IDataObject>>() {
["MyData"] = (data) => {
// The Action delegate deserialzies the IDataObject...
var myData = GetObjectData<MyData>(data, "MyData");
// ...and calls a method passing the class object
MessageBox.Show(myData.ToString());
},
["MyOtherData"] = (data) => {
var otherData = GetObjectData<MyOtherData>(data, "MyOtherData");
MessageBox.Show(otherData.ToString());
}
};
? On the Source side (Drag/Drop initiator):
Point mouseDownPos = Point.Empty;
private void SomeSourceControl_MouseDown(object sender, MouseEventArgs e)
{
mouseDownPos = e.Location;
}
private void SomeSourceControl_MouseMove(object sender, MouseEventArgs e)
{
MyData toBeTransmitted = new MyData(100, 100);
if (e.Button == MouseButtons.Left &&
((Math.Abs(e.X - mouseDownPos.X) > SystemInformation.DragSize.Width) ||
(Math.Abs(e.Y - mouseDownPos.Y) > SystemInformation.DragSize.Height))) {
var data = SetObjectData<MyData>(toBeTransmitted, "MyData");
DoDragDrop(data, DragDropEffects.All);
}
}
? On the Target side (Drag/Drop target)
private void SomeTargetControl_DragEnter(object sender, DragEventArgs e)
{
var formats = e.Data.GetFormats();
// Verify that a Data Type is defined in the Dictionary
if (formats.Any(f => dataActions.ContainsKey(f))) {
e.Effect = DragDropEffects.All;
}
}
private void SomeTargetControl_DragDrop(object sender, DragEventArgs e)
{
// Double check: the fail-safe Data Type is present
string dataType = (string)e.Data.GetData("MyApp_DataObjectType");
// If the Data Type is in the Dictionary, deserialize and call the Action
if (dataActions.ContainsKey(dataType)) {
dataActions[dataType](e.Data);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/443401.html
上一篇:如何獲取RAM名稱?
