在为类的字段分配值时添加与特定字段关联的一些元数据? [英] Add some meta data associated to a particular field when I am assigning a value to the field of a class?

查看:111
本文介绍了在为类的字段分配值时添加与特定字段关联的一些元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堂课,我正在做一些漂亮的事。

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.


  • 类被传入

  • 通过反射附加了
    元信息来分配(映射)值

  • 返回的类

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

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


  • 应用程序对类进行正常工作(分配值,获取值,验证信息等)。

  • 稍后再传回类

  • 使用值和元信息一起做某事

简而言之,我基本上是在寻找一种在任何不带任何类实例的情况下 Pi带额外信息的方式,特殊的编译时修改。

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

I realize this is an odd problem with some odd constraints, but can it be done?

推荐答案

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



您最好的选择是

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)创建属性:

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

}

2)装饰字段:

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

}

3)然后在您的反思中:

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


  • 两个实例都需要允许其存储

  • 状态需要存储在单独的类中,例如字典

第二种方法是第一个想到的方法,很简单。首先,可以

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


  • 将状态定义为可以保存信息的单独属性

  • 从提供额外功能的基类继承

  • 创建通用类型,例如 Metadata< T> 它将为所有类型提供此类功能

  • 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

与第三个类似,它可以封装以反映类型 T 并创建必要的占位符以存储额外状态。这样做的主要问题是您不能将类型作为参数传递给方法。 似乎第二种解决方案最实用。

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.

这篇关于在为类的字段分配值时添加与特定字段关联的一些元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆