C# Reflection: Get info for all members of class a

2019-06-06 20:18发布

问题:

When I run the following code, it only returns a MethodInfo/FieldInfo/etc. that belongs directly to the Type that I'm searching for the info object in. How do I find the info object regardless of the fact that it resides in a base class and could be private?

obj.GetType().GetMethod(methodName, bindingFlags);

回答1:

Well, you answered your own question, but as far as I understood, you main requirement is How do I find the info object regardless of where it is found in the hierarchy?

You don't need recursion here to get all members in the full hierarchy. You can use GetMembers function on a Type and it will return all members including all base classes.

Next code example demonstrates this:

var names = 
    typeof(MyClass).GetMembers()
                   .Select (x => x.Name);

Console.WriteLine (string.Join(Environment.NewLine, names));

for such structure

class MyClass : Base
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

class Base
{
    public string Name { get; set; }
}

returns

get_Name
set_Name
get_Surname
set_Surname
get_Name
set_Name
ToString
Equals
GetHashCode
GetType
.ctor
Name
Surname

note that get_Name accessor for auto-property appears twice because MyClass hides Name property of the base class. Also note ToString, GetType and other methods, defined in object class



回答2:

The following code will look through each base class of an object if the info object is not found in the child class. Note that though it will return the base class info object, it will return the object that it "runs into first", so if you have a variable called _blah in your child class and a variable called _blah in a base class, then the _blah from the child class will be returned.

public static MethodInfo GetMethodInfo(this Type objType, string methodName, BindingFlags flags, bool isFirstTypeChecked = true)
{
    MethodInfo methodInfo = objType.GetMethod(methodName, flags);
    if (methodInfo == null && objType.BaseType != null)
    {
        methodInfo = objType.BaseType.GetMethodInfo(methodName, flags, false);
    }
    if (methodInfo == null && isFirstTypeChecked)
    {
        throw new MissingMethodException(String.Format("Method {0}.{1} could not be found with the following BindingFlags: {2}", objType.ReflectedType.FullName, methodName, flags.ToString()));
    }
    return methodInfo;
}

public static FieldInfo GetFieldInfo(this Type objType, string fieldName, BindingFlags flags, bool isFirstTypeChecked = true)
{
    FieldInfo fieldInfo = objType.GetField(fieldName, flags);
    if (fieldInfo == null && objType.BaseType != null)
    {
        fieldInfo = objType.BaseType.GetFieldInfo(fieldName, flags, false);
    }
    if (fieldInfo == null && isFirstTypeChecked)
    {
        throw new MissingFieldException(String.Format("Field {0}.{1} could not be found with the following BindingFlags: {2}", objType.ReflectedType.FullName, fieldName, flags.ToString()));
    }
    return fieldInfo;
}


标签: c# reflection