Sean
Sean

Reputation: 2577

Get static method of parent object

Having an issue with getting a static method from a parent object. Examine the following code:

var objType = typeof(myClass);   // myClass is a ChildClass object
var methods = objType.GetMethods(BindingFlags.Static | BindingFlags.FlattenHierarchy);

Parent Class:

public class ParentClass {
    public static T GrabStuff<T>(string values) {
        GrabStuff<T>(values, false);
    }

    // ---- Updated
    public static T GrabStuff<T>(string values, bool isSomething) {
        // TODO: Do Stuff
    }
    // ---- Updated
}

Child Class:

public class ChildClass : ParentClass {
}

Methods is returning and array of 0 objects.

Am I doing something wrong here to pull a list of static methods?

Upvotes: 3

Views: 873

Answers (2)

Phil Klein
Phil Klein

Reputation: 7514

You must specify BindingFlags.Public in addition to BindingFlags.Static:

var objType = typeof(ChildClass);
var methods = objType.GetMethods(BindingFlags.Public | BindingFlags.Static);

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

You didn't specify BindingFlags.Public:

var objType = typeof(ChildClass);
var methods = objType.GetMethods(BindingFlags.Static | 
                                 BindingFlags.FlattenHierarchy | 
                                 BindingFlags.Public);

With this change the MethodInfo's for GrabStuff, Equals and ReferenceEquals are returned.

Upvotes: 4

Related Questions