我如何使用Debug.Write动态数据? [英] How can I use Debug.Write with dynamic data?

查看:647
本文介绍了我如何使用Debug.Write动态数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的Adobe InDesign中的一些脚本。他们的COM实现真正的专为VB,所以它不是严格的有关报告的数据类型,偶尔有必要使用动态。

I am doing some scripting of Adobe InDesign. Their COM implementation is really designed for VB, so it's not rigorous about reporting data types, occasionally necessitating the use of dynamics.

我要调试的代码块看起来是这样的:

I'm trying to debug a chunk of code that looks like this:

foreach (dynamic pi in current.PageItems)
{
    if (pi is TextFrame)
    {
        var frame = pi as TextFrame;
        var str = frame.Contents.ToString();
        Debug.WriteLine(str);
    }
}

这给了我这样一个RuntimeBinderException:

This gives me a RuntimeBinderException like this:

信息:无法动态调用方法'的WriteLine'
,因为它有一个条件属性。

Additional information: Cannot dynamically invoke method 'WriteLine' because it has a Conditional attribute

我得到的问题是,随着条件的属性,需要处理动态解析为在运行时可能会得到编出来的类型代码的版本,但我显式转换我想调试一个字符串什么,所以我不明白为什么这仍然发生。我怎样才能解决这个问题呢?

I get that the problem is that with Conditional attributes, the version of the code that needs to handle the type the dynamic resolves to at runtime might have gotten compiled out, but I'm explicitly converting what I want to debug to a string, so I don't see why this is still happening. How can I work around this problem?

推荐答案

你被你的使用被咬VAR 这里,是我的猜测。

You're being bitten by your use of var here, is my guess.

我假设目录是<$ C 。$ C>动态

考虑这个例子:

dynamic d = null;
var s = d.ToString();



取值动态不是字符串

您会希望对象转换为对象之前调用的ToString ,或铸的ToString 到的结果字符串。问题的关键是,在某些时候,什么地方,你需要一个投走出动态周期。

You'll want to cast the object to object before calling ToString, or cast the result of ToString to a string. The point is that at some point, somewhere, you need a cast to get out of the dynamic cycle.

这是我想解决这个问题:

This is how I'd solve it:

string str = ((object)frame.Contents).ToString();
Debug.WriteLine(str);

string str = frame.Contents.ToString() as string;
Debug.WriteLine(str);

这篇关于我如何使用Debug.Write动态数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆