jeudi 14 février 2019

Reflection: FindMembers returning empty

I'm trying to list all members with a given attribute, I've implemented a method that uses FindMembers but it always return an empty collection. Can anyone tell me what I'm doing wrong?

public List<MemberInfo> GetMember<TClass, TAttribute>()
{
    Type type = typeof(TClass);
    Type attributeType = typeof(TAttribute);
    List<MemberInfo> members = type.FindMembers(MemberTypes.All, BindingFlags.Default, Filter, null).ToList();
    return members;
}

public bool Filter(MemberInfo memberInfo, object filterCriteria)
{
    return memberInfo.IsDefined(typeof(TestAttribute));
}

[Test]
public string MethodName()
{
    return "test";
}

When it I call like this:

List<MemberInfo> members = GetMember<TestClass, TestAttribute>();

It returns empty.





Aucun commentaire:

Enregistrer un commentaire