我已经开始编写一个调试器可视化库,但是它没有按预期工作。
当我将鼠标悬停在我为其创建可视化工具的变量上时,应该有一个放大镜,但事实并非如此。显然,视觉工作室并没有告诉我出了什么问题。
以下代码位于可视化库(面向 4.0 的类库项目)中。
对话框调试器:
[assembly: DebuggerVisualizer(
typeof(ColorRGBA8Visualizer),
typeof(Serializer<ColorRGBA8>),
Target = typeof(ColorRGBA8),
Description = "ColorRGBA8 Visualizer")]
public class ColorRGBA8Visualizer
: DialogDebuggerVisualizer
{
public static void TestShowVisualizer(object objectToVisualize)
{
var visualizerHost = new VisualizerDevelopmentHost(objectToVisualize,
typeof(ColorRGBA8Visualizer),
typeof(Serializer<ColorRGBA8>));
visualizerHost.ShowVisualizer();
}
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
{
var color = Serializer<ColorRGBA8>.GetValue(objectProvider);
using (var form = new Form())
{
form.Text = "Color Visualizer";
form.BackColor = Color.FromArgb(255, color.R, color.G, color.B);
form.Size = new Size(100, 10);
windowService.ShowDialog(form);
}
}
}
自定义序列化器,因为无论如何我都需要它来处理更复杂的对象:
public class Serializer<T>
: VisualizerObjectSource
{
public static T GetValue(IVisualizerObjectProvider provider)
{
var serializer = new DataContractSerializer(typeof (T));
var transport = (MemoryStream) provider.GetObject();
var value = serializer.ReadObject(transport);
return (T) value;
}
public override void GetData(object target, Stream outgoingData)
{
var value = (T) target;
var transport = new MemoryStream();
var serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(transport, value);
transport.Position = 0;
base.GetData(transport, outgoingData);
}
}
我的测试控制台应用程序:
internal class Program
{
#region Static Methods
private static void Main(string[] args)
{
var value = new ColorRGBA8{R=255};
ColorRGBA8Visualizer.TestShowVisualizer(value);
}
#endregion
}
对话框显示得很好,但整个该死的点是当鼠标悬停在该点的变量“值”上时,显示对话框的按钮出现。
是的,我的可视化库通过构建库输出到“C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\Debugger\Visualizers”:所有必需的引用也被复制。
谁能告诉我我错过了什么?
编辑
这是JaredPar建议的解决方案。出于某种未知原因,Visual Studio 拒绝使用通用序列化器,即使它们被指定为封闭类型,例如 Serializer。
public class ColorRGBA8Serializer
: VisualizerObjectSource
{
public static ColorRGBA8 GetValue(IVisualizerObjectProvider provider)
{
var serializer = new DataContractSerializer(typeof(ColorRGBA8));
var transport = (MemoryStream)provider.GetObject();
var value = serializer.ReadObject(transport);
return (ColorRGBA8)value;
}
public override void GetData(object target, Stream outgoingData)
{
var value = (ColorRGBA8)target;
var transport = new MemoryStream();
var serializer = new DataContractSerializer(typeof(ColorRGBA8));
serializer.WriteObject(transport, value);
transport.Position = 0;
base.GetData(transport, outgoingData);
}
}