类中字段的属性 [英] Attributes on Field in Class

查看:52
本文介绍了类中字段的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在类中的每个字段上获取带有属性 Sync.Field 的字段列表.该字段可以/不能具有 Sync.Field

I want to get a list of fields with an Attribute Sync.Field on each of the field in the class. The field can / cannot have the attribute of Sync.Field

我一直在尝试以下方法,但无法获取每个字段的自定义属性.

I have been trying the following, but having trouble getting the custom attribute for each field.

FieldInfo[] fiClass = typClass.GetFields();

FieldInfo[] lst = fiClass
                   .Where(c => c.CustomAttribute().GetType() == typeOf(Sync.Field))
                   .ToList();

推荐答案

我有一个通用的集合类,它使用数据类来匹配 SNMP 表与数据类字段.像 JsonProperty 将反序列化的值匹配到属性.我以同样的方式定义了 SNMPPropertyAttribute.属性本身是

I have a generic collection class, which uses a data class to match an SNMP table with data class fields. Like JsonProperty matches deserialised values to properties. In the same way I define a SNMPPropertyAttribute. The attribute itself is

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
  sealed class SNMPPropertyAttribute : Attribute
  {
    public SNMPPropertyAttribute(string propertyOID) => PropertyOID = new ObjectIdentifier(propertyOID);

    public ObjectIdentifier PropertyOID { get; }
  }

当在表构造函数中时,我正在从属性中制作数据字段及其 OID 的字典:

When in the table constructor, I'm making a dictionary of data fiels and their OIDs from the attribute:

public SNMPTableEntity()
    {
      snmpPoperties = new Dictionary<ObjectIdentifier, PropertyInfo>();
      foreach (PropertyInfo myProperty in GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
      {
        CustomAttributeData snmpAttribure = myProperty.CustomAttributes.Where(x => x.AttributeType == typeof(SNMPPropertyAttribute)).FirstOrDefault();
        if (snmpAttribure != null)
          snmpPoperties.Add(new ObjectIdentifier((string)snmpAttribure.ConstructorArguments[0].Value), myProperty);
      }
    }

它看起来与您想要实现的目标相似,因此希望它有所帮助.但不同的是,我使用的是属性,而不是字段.不确定它是否有很大的不同,但是...

It looks similar to what are you trying to acheive, so hopefully it helps. But the difference, is that I'm using properties, not fields. Not sure if it makes a big difference, but...

有一个使用示例:

  public class InterfaceTableEntity : SNMPTableEntity
  {
    /// <summary>
    /// A unique value for each interface.  Its value ranges between 1 and the value of ifNumber.  The value for each interface must remain constant at least from one re-initialization of the entity's network management system to the next re- initialization.
    /// </summary>
    [SNMPProperty("1.3.6.1.2.1.2.2.1.1")]
    protected Integer32 ifIndex { get; set; }
    /// <summary>
    /// A textual string containing information about the interface.  This string should include the name of the manufacturer, the product name and the version of the hardware interface.
    /// </summary>
    [SNMPProperty("1.3.6.1.2.1.2.2.1.2")]
    protected OctetString ifDescr { get; set; }
    /// <summary>
    /// The type of interface, distinguished according to the physical/link protocol(s) immediately `below' the network layer in the protocol stack.
    /// </summary>
    [SNMPProperty("1.3.6.1.2.1.2.2.1.3")]
    protected Integer32 ifType { get; set; }
}

这篇关于类中字段的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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