Determining hidden members via reflection.

What's the sane way to determine, via reflection, whether a type member has hidden a method or not

***CODE***
namespace Goomba
{
public class Base
{
public virtual void Action()
{
System.Console.WriteLine("Happyness is for fools!");
}
}
public class Subclass : Base
{
public override void Action()
{
System.Console.WriteLine("Get happy!");
}
}
public class Shadow : Subclass
{
public new void Action()
{
System.Console.WriteLine("This is the shadow");
}
}
}
***CODE***


Via reflection, grabbing Shadow's members via typeof(Shadow).GetMembers() returns two references to Action -- the hidden method, and the method that is hiding it.

Yet, I can see no way of telling these methods apart in a clean, sane way.

Help


Answer this question

Determining hidden members via reflection.

  • K. Maheswar Reddy

    Sorry but the IsHideBySig property does not work. It appears the only way you can determine if some method is declared as new (shadowing) another method in a base class is using the following logic:

    If the method is listed twice in the return array of the Type.GetMethods and:

    One method's DeclaringType != ReflectedType and the IsVirutal property = true

    One method's DeclaringType = ReflectedType and the IsVirtual property = true

    If the method is overriding a method in the base class then the method is only listed once and:

    DeclaringType = ReflectedType and the IsVirtual property = true

    Definitely not a clean and easy way of figuring this out. However, it appears the only way unless someone else knows something better.

    Hope this helps.

    smc750



  • moimoiiomiom

    Thanks for the help -- it's close but not exactly what I was looking for.

    Maybe my example should have been more clear

    I want to tell if a members method hides an inherited method or not.

    For example, if I modify Shadow to include several other "non hiding" member , how will I be able to detect that Action hides an inherited member called Action, while all the rest are simply normal member declarations



  • Hung Pham

    Type t = typeof(Shadow);

    MethodInfo[] methods = t.GetMethods(BindingFlags.DeclaredOnly |BindingFlags.Instance|BindingFlags.Public);

    You can filter methods to ask reflection to return only the methods declared by the current object and not the ones inherited.In your case Shadow type will return 1 method(the "new" Action method).Comment out the Action method in Shadow and methods.Length will be equal to zero.


  • Bonquest Technologies

    Checking just the base definition for null and its IsVirtual property does not definitively identify methods that are declared with the "new" keyword. This logic I believe will also identify methods that are part of the derived class only. I made a mistake on the previous logic but the following seems to be the only way to identify a method declared with the new keyword. The method must appear twice.

    If the method is listed twice in the return array of the Type.GetMethods and:

    //identify the base method

    method's DeclaringType != ReflectedType and the IsVirutal property = true

    //the same method declared with the new keyword

    method's DeclaringType = ReflectedType and the IsVirtual property = false

    smc750



  • StievieD

    Thanks for the help, the both of you!

    Sadly, I have to admit that the smc750's implementation is identical to the current logic of my code: I was really, desperately hoping that there was something I was missing, but it seems there's no real clean way to determine things aside from this implementation.

    Arrr.

  • Frediano

    Here is a loose implementation which works-

    MethodInfo[] info = t.GetMethods( BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

    foreach (MethodInfo method in info)

    {

    if (method.GetBaseDefinition() != null) // There is a base definition available

    {

    if(!method.GetBaseDefinition().IsVirtual) // It is non-virtual

    {

    list.Add(method);

    }

    }

    }


  • kellyo

    Try checking the IsHideBySig property of the method.

    smc750



  • Determining hidden members via reflection.