【发布时间】:2013-01-21 09:36:40
【问题描述】:
我有一堂课:
class MyClass
{
}
...
MyClass c = new MyClass();
是否可以在运行时向此类添加属性/字段?
(我不知道它们在编译时的类型或名称是什么,而且它们没有我可以使用的通用接口。)
伪例子:
Add property named "Prop1" [type System.Int32]
Add property named "Prop900" [type System.String]
我已经读过this question,但它使用接口
提前致谢。
【问题讨论】:
-
然后到处通过反射访问它们?
Dictionary<string, object>怎么样? -
@HenkHolterman 是的 Henk 稍后我使用
MyClass.GetType().GetProperties()访问它们 -
您需要它用于什么用例?
ICustomeTypeDescriptor呢? -
@RoyiNamir:反射与 IL 修改无关——这就是您想要实现的目标。
-
为什么要通过直接跳入可能最糟糕的用途来“学习”反射?
标签: c# .net reflection .net-4.0 properties