【问题标题】:How to set a base class internal field如何设置基类内部字段
【发布时间】:2009-06-28 01:58:35
【问题描述】:

我正在扩展一个类 (DirectoryServices.AccountManagement.Principal),我需要为基类中的一个字段分配一个值,但它标记为内部的。我可以使用反射来设置值吗?那要怎么做呢?

我发现了这个:

base.GetType().GetProperty("unpersisted").SetValue(??, false);

但我不太清楚如何给它基本的 Principal 对象

非常感谢任何指导。

【问题讨论】:

    标签: .net reflection field internal account-management


    【解决方案1】:

    使用 this 表示 ??

    【讨论】:

    • 对不起,我收回你的正确,我只需要使用 GetField 代替。谢谢!
    【解决方案2】:

    在派生类上获取属性并使用所有参数:

    GetType(Derived).GetProperty("BaseProperty", BindingFlags.Instance Or BindingFlags.NonPublic, Nothing, GetType(PropertyType), New Type() {}, Nothing).SetValue(DerivedInstance, NewValue, Nothing)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-28
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多