当我为类的字段分配值时,添加一些与特定字段关联的元数据?

发布于 2024-10-30 13:22:56 字数 611 浏览 1 评论 0原文

我有一堂课,我正在反思地做一些有趣的事情。

现在,当我使用反射为类的字段分配值时,我需要添加一些与特定字段关联的元数据(我不知道该类是什么)。

我希望在客户不知道我的实现的情况下执行此操作(必须自己做任何特殊的事情)。

在类中拥有指定的“元”字段或对象是可行的,但感觉不太优雅,因为它要求子类做一些“兼容”的事情。我想动态地将这些元信息附加到现有的类,但仍然允许将其视为与应用程序的其余部分相同的类。我希望稍后能够恢复此元信息。

  • 类通过反射传入
  • 值并通过反射分配(映射) 附加元信息
  • 返回的类

*此过程对于正常操作和类的对象类型应该没有副作用。对于一般应用程序,前后类应该相同。

  • 应用程序对类进行“正常”工作(分配值、获取值、验证信息等)。
  • 稍后将类传回,
  • 使用值和元信息来执行某些操作

分解为最简单的术语,我是基本上寻找一种方法来“搭载”任何任意类实例上的额外信息,而无需特殊的编译时修改。

我意识到这是一个带有一些奇怪约束的奇怪问题,但是可以做到吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-11-06 13:22:56

如果您需要每种类型而不是实例的额外状态,则这适用

。您最好的选择是对字段使用自定义属性。

1) 因此,创建属性:

[AttributeUsage(AttributeTargets.Field)]
public class MyCustomAttribute : Attribute
{

}

2) 装饰字段:

class MyClass
{
    [MyCustomAttribute]
    private int _myField;
    ....

}

3) 然后在您的反思中:

if(fieldInfo.GetCustomAttributes(typeof(MyCustomAttribute), true).Length>0)
{
   ...
}

如果它必须位于实例中

如果数据要成为实例的一部分,则

  • 任一实例都需要考虑其存储
  • 状态需求像字典一样存储在单独的类中

第二种方法是第一个想到的并且简单易行的方法。首先,可以将

  • 状态定义为可以保存信息的单独属性。这是您建议的,但您不满意。
  • 从提供额外功能的基类继承
  • 创建通用类型,例如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:

[AttributeUsage(AttributeTargets.Field)]
public class MyCustomAttribute : Attribute
{

}

2) Decorate the field:

class MyClass
{
    [MyCustomAttribute]
    private int _myField;
    ....

}

3) And then in your reflection:

if(fieldInfo.GetCustomAttributes(typeof(MyCustomAttribute), true).Length>0)
{
   ...
}

If it has got to be in the instance

If data is to be part of the instance then

  • either instance needs to allow for its storage
  • state needs to be stored on a separate class like a dictionary

Second approach is the one first coming to mind and straightforward to do. On the first note, one can

  • Define state as a separate property that can hold info. This is the one you have suggested and you are not happy with.
  • Inherit from a base class which provides the extra functionality
  • Create a generic type, e.g. Metadata<T> which will provide such functionality to all types

I 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.

时光沙漏 2024-11-06 13:22:56

我倾向于创建一个字典,其中对象实例作为键,元数据作为值。您可能需要小心确保相等性是通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文