Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix casting of list of textruns #333

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ImageSharp.Drawing/Processing/RichTextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public RichTextOptions(RichTextOptions options)
/// </summary>
public new IReadOnlyList<RichTextRun> TextRuns
{
get => (IReadOnlyList<RichTextRun>)base.TextRuns;
get => base.TextRuns.Cast<RichTextRun>().ToList();
Copy link
Member

@antonfirsov antonfirsov Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This creates a copy and in my understanding Cast should also fail since the default instance is has the property assigned to TextRun[] not RichTextRun[].

Would direct casting not work if we explicitly set an empty array as the default value?

That would be a better solution IMO, but it would allow bringing the object into an invalid state by downcasting RichTextOptions to TextOptions. It is an unusual but valid code.

RichTextOptions a = new(font);

Foo(a);
void Foo(TextOptions b) =>  b.TextRuns = new TextRun[0];

IReadOnlyList<RichTextRun> runs = a.TextRuns; // exception

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd probably fix that with some sort of generic pattern in the future but for now our options are limited.

set => base.TextRuns = value;
}

Expand Down
Loading