Search code examples
c#reflectionfiltermemberinfo

What's the filterCriteria in the Type.FindMembers C#


I've been searching he internet for hours without finding a satisfying answer for this I know it's what determines what would be included in the MemberInfo object but what exactly are the possible values to this

 public class TestTypeOf
{
    private int tt;
    private string tt2;

    public void test()
    {

        Type type = typeof(TestTypeOf);
        MemberInfo[] info = type.FindMembers(MemberTypes.All, BindingFlags.NonPublic, new MemberFilter(searchFilter), "tt");
        Console.WriteLine(type.BaseType);
        Console.WriteLine(type.Assembly);
        Console.WriteLine(type.Attributes);
        Console.WriteLine(type.FullName);

        foreach (var Info in info)
            Console.WriteLine(Info.ToString());
    }

    private static bool searchFilter(MemberInfo memObj, object obj)
    {
        if (memObj.Name.ToString() == obj.ToString())
            return true;
        else
            return false;
    }
}

that's the code I have been using and it returns and empty array I know that the search criteria has a wrong parameter but is there's something else??


Solution

  • You need BindingFlags.Instance as well, NonPublic is not enough:

    MemberInfo[] info = type.FindMembers(MemberTypes.All, BindingFlags.Instance | BindingFlags.NonPublic, new MemberFilter(searchFilter), "tt");
    

    Your other parameters are fine.