C
C#2y 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
Pobiega2y 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
SWEETPONYOP2y ago
saltypatron.eth
Does reflection work if theres no setter?
Pobiega
Pobiega2y ago
Doesn't matter, wont change the font
saltypatron.eth
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
Pobiega2y ago
he wants to change the font
saltypatron.eth
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
Pobiega2y 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
Sorry, different contexts
amio
amio2y 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
i swapped over to trying to help and the reflection would be a side discussion
SWEETPONY
SWEETPONYOP2y 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
as long as reportObjects[i] is never null otherwise i think it'll be zero
Pobiega
Pobiega2y ago
you could get around that by checking for null, instead of using ! to force the compiler to ignore it
saltypatron.eth
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
SWEETPONYOP2y ago
ok, thanks, I wil try it it will be TableCell or TextObject
Pobiega
Pobiega2y ago
btw, don't forget that you can assign in the pattern match if (reportObjects[i] is TableCell tableCell)
saltypatron.eth
Probably my favorite new(ish) addition to C# out bool something
Pobiega
Pobiega2y ago
pattern matching is amazing.
saltypatron.eth
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
Pobiega2y ago
ah yeah out var is great
SWEETPONY
SWEETPONYOP2y 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
Pobiega2y ago
give it a try
SWEETPONY
SWEETPONYOP2y ago
it works thanks
Pobiega
Pobiega2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server