Ehs*_*san 1 c# reflection attributes custom-attributes
我有一个3级的财产.
class Issuance
{
[MyAttr]
virtual public long Code1 { get; set; }
[MyAttr]
virtual public long Code2 { get; set; }
virtual public long Code3 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我需要通过自定义属性([MyAttr])选择此类中的一些属性.
我使用GetProperties()但是这会返回所有属性.
var myList = new Issuance().GetType().GetProperties();
//Count of result is 3 (Code1,Code2,Code3) But count of expected is 2(Code1,Code2)
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
只需使用LINQ和一个Where子句MemberInfo.IsDefined:
var myList = typeof(Issuance).GetProperties()
.Where(p => p.IsDefined(typeof(MyAttr), false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1197 次 |
| 最近记录: |