I think that if an object implements IDisposable , it is a good thing to get rid of it when you no longer need it.
DataObject provides the basic implementation of the IDataObject interface, so why not get it out of it:
public sealed class HtmlDataObject : DataObject, IDisposable { protected MemoryStream HtmlMemoryStream { get; set; } public HtmlDataObject(MemoryStream memoryStream, string fallBackText) { HtmlMemoryStream = memoryStream; SetText(fallBackText); SetData(DataFormats.Html, false, HtmlMemoryStream ); } public void Dispose() { HtmlMemoryStream .Dispose(); } }
Thus, your method can be changed:
public HtmlDataObject GetClipboardData() { return new HtmlDataObject(this.GenerateHtml(), this.fallbackText.ToString()); }
And you can put it in using statement or Dispose() when you finish using it.
Final thought:. You do not have to worry about the clipboard data, because the DataObject will be destroyed when you exit the application and your clipboard will lose what you put inside it. http://msdn.microsoft.com/en-us/library/office/gg278673.aspx
If you want the stream to remain after deleting it and / or when exiting the application, you should use Clipboard.SetDataObject with copy parameter = true
giammin
source share