在C#中以新程序版本读取旧剪贴板数据 [英] Reading old clipboard data in new program version in C#
问题描述
我们有一个旧程序版本,在该版本中我们将可序列化的类放入剪贴板。对于这个示例,我们可以这样说:
We have an old program version where we put a serializable class into the clipboard. For this example lets say something like this:
namespace ClipboardLibrary
{
[Serializable]
public class ClipboardData
{
public string Content { get; set; }
}
}
现在在新版本中为此保留它示例简单的类看起来类似,只是名称空间已更改,如下所示:
Now in the new version to keep it for this example simple the class looks the same except the namespace was changed like so:
namespace ClipboardLibrary.OtherNamespace
{
[Serializable]
public class ClipboardData
{
public string Content { get; set; }
}
}
当我从旧版本复制时-其中只需调用 SetData(someKey,剪贴板数据)
并尝试通过 Clipboard.GetData(someKey)$ c来反序列化数据即可完成此操作$ c>我得到一个序列化异常:
When I copy now from the old version - where it is done by simply call SetData(someKey, clipboardData)
and try to get the deserialize the data via Clipboard.GetData(someKey)
I get a serialization exception:
System.Runtime.Serialization.SerializationException was unhandled
HResult=-2146233076
Message=Der für die Deserialisierung benötigte Typ "ClipboardLibrary.ClipboardData" kann nicht geladen werden.
Source=mscorlib
StackTrace:
bei System.Runtime.Serialization.ObjectManager.DoFixups()
bei System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
bei System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
bei System.Windows.DataObject.OleConverter.ReadObjectFromHandle(IntPtr handle)
bei System.Windows.DataObject.OleConverter.GetDataFromHGLOBAL(String format, IntPtr hglobal)
bei System.Windows.DataObject.OleConverter.GetDataFromOleHGLOBAL(String format, DVASPECT aspect, Int32 index)
bei System.Windows.DataObject.OleConverter.GetDataFromBoundOleDataObject(String format, DVASPECT aspect, Int32 index)
bei System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert, DVASPECT aspect, Int32 index)
bei System.Windows.DataObject.OleConverter.GetData(String format, Boolean autoConvert)
bei System.Windows.DataObject.GetData(String format, Boolean autoConvert)
bei System.Windows.Clipboard.GetDataInternal(String format)
bei System.Windows.Clipboard.GetData(String format)
bei CopyPaste.MainWindow.Button_Click_1(Object sender, RoutedEventArgs e) in c:\Users\twilker\Documents\Visual Studio 2013\Projects\CopyPaste\CopyPaste\MainWindow.xaml.cs:Zeile 42.
...
抱歉,这里的德语文本。它说的是找不到类型 ClipboardLibrary.ClipboardData
,这很明显。
I am sorry of the German text here. What it says is that the type ClipboardLibrary.ClipboardData
could not be found, which makes obvious sense.
有可能以某种方式检索数据吗?
Is there a possibility to retrieve the data anyway somehow?
推荐答案
您可以在新的程序集中引用旧程序集并获得 ClipboardLibrary.ClipboardData
Type
通过反序列化,没有例外。并在新的程序集转换器中实现,从 ClipboardLibrary.ClipboardData
到 ClipboardLibrary.OtherNamespace.ClipboardData
。
You can reference old assembly in the new one and obtain ClipboardLibrary.ClipboardData
Type
via deserialization without exceptions. And implement in the new assembly convertor from ClipboardLibrary.ClipboardData
to ClipboardLibrary.OtherNamespace.ClipboardData
.
这篇关于在C#中以新程序版本读取旧剪贴板数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!