PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 2:39 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Tue Sep 26, 2023 10:07 am 
Offline

Joined: Tue Sep 26, 2023 9:19 am
Posts: 2
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;
   }
}


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 26, 2023 10:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 09, 2023 8:06 am 
Offline

Joined: Tue Sep 26, 2023 9:19 am
Posts: 2
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?


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 09, 2023 10:04 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 48 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group