需求
在使用构建业务类型时,对应的Model属性需要动态变更(新增)。
讨论及方案1
讨论1:
You cannot extend an existing class with new members at runtime. However, you can create a new class using System.Reflection.Emit that has the existing class as base class.
SeeTypeBuilder.DefineProperty Method (String, PropertyAttributes, Type, Type[])for a full example.
typeBuilder.SetParent(typeof(MyClass));
typeBuilder.DefineProperty("Prop1", ..., typeof(System.Int32), null

博客探讨了在运行时无法直接扩展已有类以添加新成员的问题,提出了三种解决方案。方案1利用System.Reflection.Emit创建继承自原有类的新类;方案2通过Dictionary实现动态属性;方案3通过hook对象的GetType方法实现额外处理。这些方法为动态变更Model属性提供了可能。
最低0.47元/天 解锁文章
566

被折叠的 条评论
为什么被折叠?



