How can I use Debug.Write with dynamic data?

后端 未结 3 563
粉色の甜心
粉色の甜心 2020-12-20 10:53

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

3条回答
  •  感动是毒
    2020-12-20 11:40

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

    I'm assuming that Contents is dynamic.

    Consider this example:

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

    s is dynamic not string.

    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);
    

    or

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

提交回复
热议问题