当我为类的字段分配值时,添加一些与特定字段关联的元数据?
我有一堂课,我正在反思地做一些有趣的事情。
现在,当我使用反射为类的字段分配值时,我需要添加一些与特定字段关联的元数据(我不知道该类是什么)。
我希望在客户不知道我的实现的情况下执行此操作(必须自己做任何特殊的事情)。
在类中拥有指定的“元”字段或对象是可行的,但感觉不太优雅,因为它要求子类做一些“兼容”的事情。我想动态地将这些元信息附加到现有的类,但仍然允许将其视为与应用程序的其余部分相同的类。我希望稍后能够恢复此元信息。
- 类通过反射传入
- 值并通过反射分配(映射) 附加元信息
- 返回的类
*此过程对于正常操作和类的对象类型应该没有副作用。对于一般应用程序,前后类应该相同。
- 应用程序对类进行“正常”工作(分配值、获取值、验证信息等)。
- 稍后将类传回,
- 使用值和元信息来执行某些操作
分解为最简单的术语,我是基本上寻找一种方法来“搭载”任何任意类实例上的额外信息,而无需特殊的编译时修改。
我意识到这是一个带有一些奇怪约束的奇怪问题,但是可以做到吗?
I have a class, and I am doing some nifty things with reflection.
Now I have a need to add some meta data associated to a particular field when I am assigning a value to the field of a class using reflection (I don't know what the class will be).
I would like to do this without the clients to my implementation knowing (having to do anything special of their own).
Having a designated "meta" field or object within the class would work, but doesn't feel very elegant since it requires the child class to do something to be "compatible". I would like to dynamically attach this meta information to the existing class, but still allow it to be treated like the same class be the rest of the application. I would like to be able to recover this meta information later.
- Class gets passed in
- Values get assigned (mapped) via Reflection with
Meta information attached - Class returned
*This process should have no side effects in regard normal operations and the object type of the class. To the general application the class should be the same before and after.
- Application does "normal" work with the class (assign values, get values, validate information ,etc.)
- Class gets passed back in later
- Use the values along with meta information to do something
Broken down to the simplest terms, I am basically looking for a way to "Piggy-back" extra information on any arbitrary class instance without special compile-time modification.
I realize this is an odd problem with some odd constraints, but can it be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要每种类型而不是实例的额外状态,则这适用
。您最好的选择是对字段使用自定义属性。
1) 因此,创建属性:
2) 装饰字段:
3) 然后在您的反思中:
如果它必须位于实例中
如果数据要成为实例的一部分,则
第二种方法是第一个想到的并且简单易行的方法。首先,可以将
Metadata
,它将为所有类型提供此类功能倾向于喜欢第三个,它可以封装反映类型
T
并创建必要的占位符来存储额外的状态。主要问题是您无法将类型作为参数传递给方法。 看来第二种解决方案是最实用的。This applies if you need the extra state per type and not instance
Your best bet is to use a custom attribute against the field.
1) So create the attribute:
2) Decorate the field:
3) And then in your reflection:
If it has got to be in the instance
If data is to be part of the instance then
Second approach is the one first coming to mind and straightforward to do. On the first note, one can
Metadata<T>
which will provide such functionality to all typesI tend to like the third which can encapsulate reflecting the type
T
and creating necessary placeholders for storing extra state. Main problem with this is you cannot pass the type to methods as a parameter. Which seems that the second solution is the most practical.我倾向于创建一个字典,其中对象实例作为键,元数据作为值。您可能需要小心确保相等性是通过 ReferenceEquals() 而不是 Equals() 确定的。您可能还需要一个包含对象和相关 PropertyInfo 的复合键。
如果元数据需要跟随对象进入元数据字典不可用的某个上下文,则此方法也不起作用。
I'd be inclined to create a dictionary with the object instances as the keys and the metadata as the values. You'd probably need to be careful to ensure that equality is determined with ReferenceEquals() rather than Equals(). You might also need a compound key comprising the object and the relevant PropertyInfo.
This approach also wouldn't work if the metadata needs to follow the object into some context where the metadata dictionary is not available.