让我回答我自己的问题。使用来自 DotNetInside 对 Dataflow 程序集的反编译的信息,例如,TransformBlockhere(再次感谢 @i3amon 提供指向 dotnetinside.com 的链接),以及非常好的 ExposedObject 包codeplex here(我了解到在this blog post,我做了以下事情:
TPL 数据流块都通过 DebuggerTypeProxy 属性实现调试器可视化工具,该属性应用于一个类型,在要显示原始类型时命名另一个类型以在 Visual Studio 调试器中使用(例如,watch窗口)。
每个DebuggerTypeProxy命名的类都是属性附加到的数据流块的内部类,通常命名为DebugView。该类始终是私有的和密封的。它公开了有关数据流块的许多很酷的东西,包括其真实(非副本)DataflowBlockOptions 以及 - 如果该块是源块 - 一个 ITargetBlock[],可用于从一开始就跟踪数据流网络构建后阻塞。
一旦获得DebugView 的实例,您就可以通过ExposedObject 使用dynamic 来获得该类公开的任何属性 - ExposedObject 允许您获取一个对象并使用普通方法和属性语法来访问其方法和属性。
1234563可以更改其SingleProducerConstrained 值。
但是,您不能使用dynamic 来查找或构造内部DebugView 类的实例。你需要为此反思。您首先从您的DebuggerTypeProxy 属性中删除
数据流块的类型,获取调试类的名称,假设它是一个内部类
数据流块的类型并搜索它,将其转换为封闭的泛型类型,最后
构造一个实例。
请充分注意,您正在使用来自数据流内部的未记录代码。用你自己的
判断这是否是一个好主意。在我看来,TPL Dataflow 的开发人员做了很多工作来支持在调试器中查看这些块,并且他们可能会继续这样做。细节可能会发生变化,但是,如果您对这些类型的反射和动态使用进行适当的错误检查,您将能够发现您的代码何时停止使用新版本的 TPL 数据流。
以下代码片段可能不会一起编译 - 它们只是从我的工作代码中剪切和粘贴的,来自不同的类,但它们肯定会给你一些想法。我让它工作正常。 (此外,为简洁起见,我省略了所有错误检查。)(此外,我仅使用 TPL 数据流的 4.5.20.0 版本开发/测试了此代码,因此您可能必须针对过去或未来的版本对其进行调整。)
// Set (change) the NameFormat of a dataflow block after construction
public void SetNameFormat(IDataflowBlock block, string nameFormat)
{
try
{
dynamic debugView = block.GetInternalData(Logger);
if (null != debugView)
{
var blockOptions = debugView.DataflowBlockOptions as DataflowBlockOptions;
blockOptions.NameFormat = nameFormat;
}
}
catch (Exception ex)
{
...
}
}
// Get access to the internal data of a dataflow block via its DebugTypeProxy class
public static dynamic GetInternalData(this IDataflowBlock block)
{
Type blockType = block.GetType();
try
{
// Get the DebuggerTypeProxy attribute, which names the debug class type.
DebuggerTypeProxyAttribute debuggerTypeProxyAttr =
blockType.GetCustomAttributes(true).OfType<DebuggerTypeProxyAttribute>().Single();
// Get the name of the debug class type
string debuggerTypeProxyNestedClassName =
GetNestedTypeNameFromTypeProxyName(debuggerTypeProxyAttr.ProxyTypeName);
// Get the actual Type of the nested class type (it will be open generic)
Type openDebuggerTypeProxyNestedClass = blockType.GetNestedType(
debuggerTypeProxyNestedClassName,
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic);
// Close it with the actual type arguments from the outer (dataflow block) Type.
Type debuggerTypeProxyNestedClass =
openDebuggerTypeProxyNestedClass.CloseNestedTypeOfClosedGeneric(blockType);
// Now create an instance of the debug class directed at the given dataflow block.
dynamic debugView = ExposedObject.New(debuggerTypeProxyNestedClass, block);
return debugView;
}
catch (Exception ex)
{
...
return null;
}
}
// Given a (Type of a) (open) inner class of a generic class, return the (Type
// of the) closed inner class.
public static Type CloseNestedTypeOfClosedGeneric(
this Type openNestedType,
Type closedOuterGenericType)
{
Type[] outerGenericTypeArguments = closedOuterGenericType.GetGenericArguments();
Type closedNestedType = openNestedType.MakeGenericType(outerGenericTypeArguments);
return closedNestedType;
}
// A cheesy helper to pull a type name for a nested type out of a full assembly name.
private static string GetNestedTypeNameFromTypeProxyName(string value)
{
// Expecting it to have the following form: full assembly name, e.g.,
// "System.Threading...FooBlock`1+NESTEDNAMEHERE, System..."
Match m = Regex.Match(value, @"^.*`\d+[+]([_\w-[0-9]][_\w]+),.*$", RegexOptions.IgnoreCase);
if (!m.Success)
return null;
else
return m.Groups[1].Value;
}
// Added to IgorO.ExposedObjectProject.ExposedObject class to let me construct an
// object using a constructor with an argument.
public ExposedObject {
...
public static dynamic New(Type type, object arg)
{
return new ExposedObject(Create(type, arg));
}
private static object Create(Type type, object arg)
{
// Create instance using Activator
object res = Activator.CreateInstance(type, arg);
return res;
// ... or, alternatively, this works using reflection, your choice:
Type argType = arg.GetType();
ConstructorInfo constructorInfo = GetConstructorInfo(type, argType);
return constructorInfo.Invoke(new object[] { arg });
}
...
}