我如何动态地添加字段在C#中的类 [英] How can I dynamically add a field to a class in C#
本文介绍了我如何动态地添加字段在C#中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么办法来添加字段
(或字段信息
,也许这是一样的)一类的运行时?
Is there any way to add Field
(or FieldInfo
, maybe this is the same) to a class at runtime?
推荐答案
您不能改变在运行时的类定义。但是,您可以创建一个从原来的类继承(如果它不是密封
),并声明该领域的新类。您可以通过使用发光适当的IL code要这样做 System.Reflection.Emit
。
You can't alter a class definition at runtime. However, you can create a new class that inherits from the original class (if it's not sealed
) and declares the field. You can do this by emitting the appropriate IL code using System.Reflection.Emit
.
这篇关于我如何动态地添加字段在C#中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文