C# - 增加关联到一个特定的领域时,我分配一个值一类的领域的一些元数据? [英] C# - add some meta data associated to a particular field when I am assigning a value to the field of a class?

查看:133
本文介绍了C# - 增加关联到一个特定的领域时,我分配一个值一类的领域的一些元数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,而我在做一些漂亮的东西与反思。

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.

  • 类被传入
  • 在价值观得到通过反射的与分配(映射) Meta信息连接
  • 在返回的类
  • 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.

  • 在应用程序的正常与班主任工作(分配值,得到的值,验证信息等。)
  • 类被传递回在以后的
  • 使用值,以及元信息做一些事情

分解到最简单的术语,我基本上在寻找一种方式来背驮式的额外信息,而无需特殊的编译时修改的任意类的实例。

我意识到这是一个奇怪的问题,一些奇怪的限制,但是能不能做到?

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

推荐答案

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

Edit : 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),因此创建属性:

1) So create the attribute:

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

}

2)装饰领域:

2) Decorate the field:

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

  • 定义状态为单独的属性,可容纳信息。这是一个你建议,你是不是高兴。
  • 继承从一个基类,它提供了额外的功能
  • 创建一个泛型类型,例如元数据< 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.

这篇关于C# - 增加关联到一个特定的领域时,我分配一个值一类的领域的一些元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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