如何获取具有指定名称的DataMemberAttribute的财产?财产、名称、DataMemberAttribute

2023-09-04 13:29:00 作者:闹够了没

我怎样才能得到反思,有一个给定名称的数据成员的属性(假设每个数据成员都有一个唯一的名字)?例如,在C与具有名为P1的数据成员的属性如下$ C $是 PropertyOne

  [DataContract(NAME =MyContract)
公共类MyContract
{
    [数据成员(名称为P1)
    公共字符串PropertyOne {获得;组; }

    [数据成员(名称为P2)
    公共字符串PropertyTwo {获得;组; }

    [数据成员(名称为P3)
    公共字符串PropertyThree {获得;组; }
}
 

目前,我有:

 字符串dataMemberName = ...;

变种dataMemberProperties = typeof运算(T)的.GetProperties()如(p值=> p.GetCustomAttributes(typeof运算(DataMemberAttribute),假)。任何())。

VAR propInfo = dataMemberProperties.Where(P =>((DataMemberAttribute)p.GetCustomAttributes(typeof运算(DataMemberAttribute),假的)。首先())名称== dataMemberName).FirstOrDefault();
 

这个作品,但感觉像它可以改进。我特别不喜欢 GetCustomAttributes()被调用了两次。

怎样才可以重新编写好?理想的情况下,这将是巨大的,如果我可以做一个简单的一行代码。

解决方案

  //使用System.Linq的;
//使用的System.Reflection;
//使用System.Runtime.Serialization;
obj.GetType()
   .GetProperties(...)
   。凡(P => Attribute.IsDefined(P的typeof(DataMemberAttribute)))
   。单(P =>((DataMemberAttribute)Attribute.GetCustomAttribute(
                    ,P的typeof(DataMemberAttribute)))名称==富);
 
excel中如何为单元格 指定 名称,并指定 最左列

注:

Attribute.IsDefined 是用来检查一个自定义属性的presence无需下载数据。因此,它是比 Attribute.GetCustomAttribute 更有效和用于跳过在第一步骤属性

其中,运营商,我们剩下的属性有一个的 DataMemberAttribute :缺少这种属性属性已被过滤掉,并且它不能被应用超过一次。因此,我们可以使用 Attribute.GetCustomAttribute 而不是 Attribute.GetCustomAttributes

How can I reflectively get the property that has the DataMember with a given name (let's assume every DataMember has a unique name)? For example, in the following code the property with the DataMember that has name "p1" is PropertyOne:

[DataContract(Name = "MyContract")]
public class MyContract
{
    [DataMember(Name = "p1")]
    public string PropertyOne { get; set; }

    [DataMember(Name = "p2")]
    public string PropertyTwo { get; set; }

    [DataMember(Name = "p3")]
    public string PropertyThree { get; set; }
}

Currently, I have:

string dataMemberName = ...;

var dataMemberProperties = typeof(T).GetProperties().Where(p => p.GetCustomAttributes(typeof(DataMemberAttribute), false).Any());

var propInfo = dataMemberProperties.Where(p => ((DataMemberAttribute)p.GetCustomAttributes(typeof(DataMemberAttribute), false).First()).Name == dataMemberName).FirstOrDefault();

This works, but it feels like it could be improved. I particularly don't like that GetCustomAttributes() is called twice.

How can it be re-written better? Ideally, it would be great if I could make it a simple one-liner.

解决方案

// using System.Linq;
// using System.Reflection;
// using System.Runtime.Serialization;
obj.GetType()
   .GetProperties(…)
   .Where(p => Attribute.IsDefined(p, typeof(DataMemberAttribute)))
   .Single(p => ((DataMemberAttribute)Attribute.GetCustomAttribute(
                    p, typeof(DataMemberAttribute))).Name == "Foo");

Notes:

Attribute.IsDefined is used to check for the presence of a custom attribute without retrieving its data. Thus it is more efficient than Attribute.GetCustomAttribute and used to skip properties in a first step.

After the Where operator, we are left with properties that have exactly one DataMemberAttribute: Properties without this attribute have been filtered out, and it cannot be applied more than once. Therefore we can use Attribute.GetCustomAttribute instead of Attribute.GetCustomAttributes.