Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to tell if MemberInfo represents an override
    text
    copied!<p>Given the following code:</p> <pre><code>public class Base { public virtual void Method() { } } public class Derived : Base { public override void Method() { } } ... var baseMethodInfo = typeof(Base).GetMember("Method")[0]; var derivedMethodInfo = typeof(Derived).GetMember("Method")[0]; </code></pre> <p>Is it possible to determine if the derivedMethodInfo represents a method declaration which overrides another in a base class?</p> <p>In <a href="https://stackoverflow.com/questions/9466582/how-to-get-the-child-declaring-type-from-an-expression?rq=1">another question</a> it was observed that had <code>Method</code> been declared abstract (and not implemented) in the base class, derivedMethodInfo.DeclaringType would have turned up as Base, which makes sense after reading @EricLippert's comments. I noticed that in the present example, since the derived class re-declares the method, that <code>derivedMethodInfo.DeclaringType == derivedMethodInfo.ReflectedType</code>, viz. Derived.</p> <p>There doesn't seem to be any connection between baseMethodInfo and derivedMethodInfo, other than their names are the same and their respective declaring types appear in the same inheritance chain. Is there any better way to make the connection?</p> <p>The reason I ask is that there appears to be no way to distinguish, through reflection, between the earlier example and the following one:</p> <pre><code>public class Base { public virtual void Method() { } } public class Derived : Base { public new void Method() { } } </code></pre> <p>In this case as well, the Derived class both declares and reflects a member called Method.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload