C
C#2mo ago
SWEETPONY

Is it possible to update public IReadOnlyList?

There is public IReadOnlyList<ISelectionNode> Selections { get; } from public api I know it is not a good idea but I want to extend it. Is it possible?
4 Replies
Angius
Angius2mo ago
With reflections, maybe Get the list normally, turn it into a regular list, append to it Then turn it into a readonly list again and use reflections to change the value of this property to that new list
SWEETPONY
SWEETPONY2mo ago
hm I tried this:
var newSelections = new List<ISelectionNode>(node.SelectionSet!.Selections)
{
new FieldNode("test")
}.AsReadOnly();

var selectionSetProperty = node.GetType().GetProperty("SelectionSet");
var selectionSet = selectionSetProperty?.GetValue(node);
var selectionsProperty = selectionSet?.GetType().GetProperty("Selections");

if(selectionsProperty != null)
{
selectionsProperty.SetValue(selectionSet, newSelections);
}
var newSelections = new List<ISelectionNode>(node.SelectionSet!.Selections)
{
new FieldNode("test")
}.AsReadOnly();

var selectionSetProperty = node.GetType().GetProperty("SelectionSet");
var selectionSet = selectionSetProperty?.GetValue(node);
var selectionsProperty = selectionSet?.GetType().GetProperty("Selections");

if(selectionsProperty != null)
{
selectionsProperty.SetValue(selectionSet, newSelections);
}
but got an exception "Property set method not found." in the last line
Angius
Angius2mo ago
You need to save the data in the backing field directly selectionProperty.GetBackingField().SetValue(selectionSet, newSelections)