PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Enumerating ParagraphFormat Borders
https://forum.pdfsharp.net/viewtopic.php?f=3&t=4479
Page 1 of 1

Author:  thoblerone [ Tue Sep 26, 2023 10:07 am ]
Post subject:  Enumerating ParagraphFormat Borders

Hi there,

writing some code to test my own stuff I stumbled about the MigraDoc.DocumentObjectModel.Borders object.

This short test method will throw a System.InvalidCastException:

Code:
[TestMethod]
public void TestForMigraDocBugReport()
{
    var paragraphFormat = new ParagraphFormat();

    foreach (var border in paragraphFormat.Borders)
    {
        Trace.WriteLine(border.ToString()); // or whatever
    }
}


It will fail because the Borders.BorderEnumerator is using a Dictionary<string, Border> and when finally enumerating about its contents, it casts the enumerator.Current entry to a DictionaryEntry object, what will throw the InvalidCastException. This is simply because the .net Dictionary<TKey, TValue> class is implementing the IEnumerable<KeyValuePair<TKey,TValue>> interface rather than IEnumerable<DictionaryEntry<TKey,TValue>>.

Suggested code change
Code:
public Border Current
{
   get
   {
      IEnumerator enumerator = _ht.GetEnumerator();
      enumerator.Reset();
      for (int i = 0; i < _index + 1; i++)
      {
         enumerator.MoveNext();
      }

      /* remove return ((DictionaryEntry)enumerator.Current).Value as Border; */
      /* instead : */
      return ((KeyValuePair<string, Border>)enumerator.Current).Value as Border;
   }
}

Author:  Thomas Hoevel [ Tue Sep 26, 2023 10:42 am ]
Post subject:  Re: Enumerating ParagraphFormat Borders

Thanks for the feedback.
The class is commented out with the current version of PDFsharp:
https://github.com/empira/PDFsharp/blob ... rs.cs#L423

Does the enumerator make sense? Can you give me a good reason for bringing it back?

Author:  thoblerone [ Mon Oct 09, 2023 8:06 am ]
Post subject:  Re: Enumerating ParagraphFormat Borders

Thanks for your reply.

No, I'm not making real use of this enumerator, in fact I stumbled across it.

I have a couple of unit test that use reflection to check if objects are fully initialized and/or have proper copy constructors that use reflection and dive into enumerations. Thus I can perfectly live without the enumerator.

Any chance to forecast when the change will come a to the PDFsharp-MigraDoc-wpf Nuget package?

Author:  TH-Soft [ Mon Oct 09, 2023 10:04 am ]
Post subject:  Re: Enumerating ParagraphFormat Borders

thoblerone wrote:
Any chance to forecast when the change will come a to the PDFsharp-MigraDoc-wpf Nuget package?
It's already there:
https://www.nuget.org/packages/PDFsharp ... -preview-3

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/