tags:

views:

67

answers:

1

hi all. i have this two classes:

Item<T> : BusinessBase<T> where T : Item<T>
{
     public static T NewItem()
     {
      //some code here
     }
}
Video : Item <Video>
{

}

now i want to invoke NewItem() method on class Video using reflection. when i try with this:

MethodInfo inf = typeof(Video).GetMethod("NewItem", BindingFlags.Static);

the object inf after executing this line still is null. can i invoke static NewItem() method on class Video?

+3  A: 

You need to specifiy BindingFlags.Public and BindingFlags.FlattenHierarchy in addition to BindingFlags.Static:

MethodInfo inf = typeof(Video).GetMethod("NewItem",
    BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy);

Alternatively, you can get the method from the declaring type without BindingFlags.FlattenHierarchy:

MethodInfo inf = typeof(Item<Video>).GetMethod("NewItem",
    BindingFlags.Static | BindingFlags.Public);

I've tried both ways and they both work.

dtb
thanks man. it work. i'm gonna accept your answer. thanks alot..
backdoor