代码之家  ›  专栏  ›  技术社区  ›  Randall Flagg

访问propertyinfo中的属性

  •  2
  • Randall Flagg  · 技术社区  · 6 年前

    我需要通过Tag类在BFrame类内设置Value属性。

    我该如何设置 Value 所有物

    澄清:
    我不想设置 Frame 中的属性 Tag 类,但 价值 的属性 框架 类型为的属性 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;
    }
    
    1 回复  |  直到 6 年前
        1
  •  6
  •   Hesam Faridmehr    6 年前

    铸造返回值 GetValue BFrame

    var bFrame = (BFrame) frameProperty.GetValue(tag);
    bFrame.Value = newValue;