C
C#17mo ago
SWEETPONY

✅ How to change of property without setter?

I have ObjectCollection with different objects: TableCell, TextObject etc. What I want: find TableCell, TextObject adn change their font family name. I did following:
var reportObjects = _report.AllObjects;
for (var i = 0; i < reportObjects.Count; i++)
{
if (reportObjects[i] is TableCell)
((reportObjects[i] as TableCell)!).Font = new Font("Calibri", 20);
if (reportObjects[i] is TextObject)
((reportObjects[i] as TextObject)!).Font = new Font("Calibri", 20);
}
var reportObjects = _report.AllObjects;
for (var i = 0; i < reportObjects.Count; i++)
{
if (reportObjects[i] is TableCell)
((reportObjects[i] as TableCell)!).Font = new Font("Calibri", 20);
if (reportObjects[i] is TextObject)
((reportObjects[i] as TextObject)!).Font = new Font("Calibri", 20);
}
my code change full font now but I don't need this because I don't wanna change original font size! I can do smth like this.. ((reportObjects[i] as TableCell)! ).Font.Name = .. but Name is doesn't have any setter. So what to do? I wanna change only font name
26 Replies
Pobiega
Pobiega17mo ago
Font.Name isn't something you can change, you need to change the entire font instance even if managed to change it, it wouldn't result in a new font - just that you changed the displayname of the current font
SWEETPONY
SWEETPONY17mo ago
saltypatron.eth
saltypatron.eth17mo ago
Does reflection work if theres no setter?
Pobiega
Pobiega17mo ago
Doesn't matter, wont change the font
saltypatron.eth
saltypatron.eth17mo ago
I think you're looking for how to retrieve the current font size and return that, regardless of the font name Was just curious 😛
Pobiega
Pobiega17mo ago
he wants to change the font
saltypatron.eth
saltypatron.eth17mo ago
Right. If the name changes and he uses the previous font size, he solves his problem he's instantiating a new font class which will look for any installed font matching that name
Pobiega
Pobiega17mo ago
No, Font doesnt magically change if you use reflection to edit the name field new Font("Your wanted font", oldFont.Size); should do what you want
saltypatron.eth
saltypatron.eth17mo ago
Sorry, different contexts
amio
amio17mo ago
Also if something is protected or private, there is a reason. Don't abuse reflection to tamper with things in ways that are explicitly discouraged
saltypatron.eth
saltypatron.eth17mo ago
i swapped over to trying to help and the reflection would be a side discussion
SWEETPONY
SWEETPONY17mo ago
I tried this: var fontField = typeof(FontFamily).GetField( "<Name>k__BackingField", BindingFlags.Instance | BindingFlags.Public); but fontField is always null ah yes thx for helping
for (var i = 0; i < reportObjects.Count; i++)
{
if (reportObjects[i] is TableCell)
{
var size = (reportObjects[i] as TableCell )!.Font.Size;
( reportObjects[ i ] as TableCell )!.Font = new Font("Calibri", size);
}
if (reportObjects[i] is TextObject)
{
var size = (reportObjects[i] as TextObject)!.Font.Size;
(reportObjects[i] as TextObject)!.Font = new Font("Calibri", size);
}
}
for (var i = 0; i < reportObjects.Count; i++)
{
if (reportObjects[i] is TableCell)
{
var size = (reportObjects[i] as TableCell )!.Font.Size;
( reportObjects[ i ] as TableCell )!.Font = new Font("Calibri", size);
}
if (reportObjects[i] is TextObject)
{
var size = (reportObjects[i] as TextObject)!.Font.Size;
(reportObjects[i] as TextObject)!.Font = new Font("Calibri", size);
}
}

it works!
saltypatron.eth
saltypatron.eth17mo ago
as long as reportObjects[i] is never null otherwise i think it'll be zero
Pobiega
Pobiega17mo ago
you could get around that by checking for null, instead of using ! to force the compiler to ignore it
saltypatron.eth
saltypatron.eth17mo ago
One other thing id say would be to maybe try using a foreach. If you need the index, you could try something to the effect of reportObjects.Select((s,i) => new KeyValuePair<int,obj>(i,s)) an anon object works as well... just couldn' remember the syntax
SWEETPONY
SWEETPONY17mo ago
ok, thanks, I wil try it it will be TableCell or TextObject
Pobiega
Pobiega17mo ago
btw, don't forget that you can assign in the pattern match if (reportObjects[i] is TableCell tableCell)
saltypatron.eth
saltypatron.eth17mo ago
Probably my favorite new(ish) addition to C# out bool something
Pobiega
Pobiega17mo ago
pattern matching is amazing.
saltypatron.eth
saltypatron.eth17mo ago
i meant the assignment of the variable in the output i absolutely hated having to declare the variable prior to because it really messed up loops and stuff and made you rethink things
Pobiega
Pobiega17mo ago
ah yeah out var is great
SWEETPONY
SWEETPONY17mo ago
smth like that?
if(reportObjects[i] is TableCell tableCell)
{
var size = tableCell.Font.Size;
tableCell.Font = new Font("Calibri", size);
}
if(reportObjects[i] is TableCell tableCell)
{
var size = tableCell.Font.Size;
tableCell.Font = new Font("Calibri", size);
}
looks good I think
Pobiega
Pobiega17mo ago
give it a try
SWEETPONY
SWEETPONY17mo ago
it works thanks
Pobiega
Pobiega17mo ago
$close
MODiX
MODiX17mo ago
Use the /close command to mark a forum thread as answered