Adding a Property to an existing class

前端 未结 2 1125
予麋鹿
予麋鹿 2021-01-19 07:05

I have a private class that I\'m using to implement certain properties. As such, I don\'t have the ability to modify the actual private class, and don\'t want to use inherit

2条回答
  •  佛祖请我去吃肉
    2021-01-19 07:38

    If the purpose of the properties is for data-binding, then you can add runtime properties to types outside your control using TypeDescriptionProvider, which is a factory for an ICustomTypeDescriptor. You would then chain the original provider and create a custom PropertyDescriptor that obtained the additional data.

    This is reasonably easy for read-only extra properties that are calculated based on the existing properties (and by "reasonably easy" I mean "only slightly insane"), but for read-write properties (or properties that are independent to the existing members) it is very hard, as you need to figure out where to put those values (in a way that still gets garbage collected etc). Non-trivial.

提交回复
热议问题