2

I need to set the Value property inside the BFrame class through the Tag class.

How am I suppose to set the Value property?

Clarification:
I am not trying to set the value of the Frame property inside the Tag class but the Value property of the Frame property which is of the type BFrame.

class BFrame
{
    string Value{get; set;}
}

class Tag
{
    BFrame Frame{get;}
}

public void func(Tag tag, string newValue)
{
    PropertyInfo frameProperty = tag.GetType().GetProperty("Frame");
    var oldValue = frameProperty.GetValue(tag);
    //frameProperty.SetValue(tag, newValue); //Doesn't work. Throws exception because there is no setter
    //TODO: Set the Value property inside the BFrame class
    //Somethig like this: tag.Frame.Value = newValue;
}
Randall Flagg
  • 4,834
  • 9
  • 33
  • 45
  • have you tried working with ? ` .GetField()` ? – Vulpex May 08 '18 at 19:41
  • 2
    If the property doesn't have a setter, you *can't* set it. –  May 08 '18 at 19:41
  • @Vulpex these are properties, `GetField` isn't relevant. –  May 08 '18 at 19:41
  • @Amy not trying to argue but can you explain to me? There should still be a filed, in this case an anonymous. There may not be a direct setter method but still there should be a way to access the field where the data is stored or am I completly off here? – Vulpex May 08 '18 at 19:43
  • @Vulpex Fields and properties are different things in C#. These are properties. –  May 08 '18 at 19:44
  • 1
    Possible duplicate of [How to set value of property where there is no setter](https://stackoverflow.com/questions/20665410/how-to-set-value-of-property-where-there-is-no-setter) –  May 08 '18 at 19:48
  • @Amy How is it a duplicate? – Randall Flagg May 08 '18 at 19:51
  • @RandallFlagg well the second answer is exactly your question. ps.: Amy see what I said is written as answer there – Vulpex May 08 '18 at 19:52
  • @Amy Is correct, your property doesn't have a setter. It is marked read only. – Greg May 08 '18 at 19:52
  • @Vulpex as I stated in my question there is only a getter. I want the Value property inside the value returned from the getter. Please see the accepted answer to understand what I asked and why the link you suggested isn't relevant. – Randall Flagg May 08 '18 at 20:23
  • Why even need reflection here? Why not just set `tag.Frame.Value` directly, or is this just a dummy example? – pinkfloydx33 May 08 '18 at 23:40
  • @RandallFlagg I did not suggest the link. The second answer from the link is the suggestion I made since it is the same, the property has no setter. `var field = typeof(MyClass).GetField("k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic)` is used to get the backend field from the property. `field.SetValue(anIstanceOfMyClass, 3);` is used to set the value. It should work with the example you've provided unless it is not accurate. If the value is processed and not stored, it may not work. – Vulpex May 09 '18 at 06:18
  • @pinkfloydx33 My goal was to go over all the properties inside a class I have. I would love to hear of a different approach for achieving this. And yes, this is a dummy example :) – Randall Flagg May 09 '18 at 06:22
  • @RandallFlagg see my comment about dummy example. If the value is processed purely, there is most likely no backend field and therefore probably will not work. – Vulpex May 09 '18 at 06:24
  • @Vulpex Thanks for helping. The solution you offered with the backing field is not relevant as it doesn't solve the problem. Your solution returns the backing field but what I want to set is a property inside it. Please take a look at the accepted answer and at the line added in the question. It has nothing to do with the availability of the backing field. – Randall Flagg May 09 '18 at 06:41

1 Answers1

6

Cast return value of GetValue to BFrame

var bFrame = (BFrame) frameProperty.GetValue(tag);
bFrame.Value = newValue; 
Hesam Faridmehr
  • 1,176
  • 8
  • 20