How can I use Debug.Write with dynamic data?

后端 未结 3 476
粉色の甜心
粉色の甜心 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:19

    No one has said it this way so I will.

    Change

     var str = frame.Contents.ToString();
    

    to

     string str = frame.Contents.ToString();
    
    0 讨论(0)
  • 2020-12-20 11:32

    but I'm explicitly converting what I want to debug to a string

    That's not actually true.

    var str = frame.Contents.ToString();
    

    This line is still completely dynamic.

    You need to explicitly declare it as a string.

    Alternatively, you could static-ize earlier by explicitly declaring frame as TextFrame.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题