Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3680896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:37:50+00:00 2026-05-19T03:37:50+00:00

Same for methods too: I am given two instances of PropertyInfo or methods which

  • 0

Same for methods too:

I am given two instances of PropertyInfo or methods which have been extracted from the class they sit on via GetProperty() or GetMember() etc, (or from a MemberExpression maybe).

I want to determine if they are in fact referring to the same Property or the same Method so

(propertyOne == propertyTwo)

or

(methodOne == methodTwo)

Clearly that isn’t going to actually work, you might be looking at the same property, but it might have been extracted from different levels of the class hierarchy (in which case generally, propertyOne != propertyTwo)

Of course, I could look at DeclaringType, and re-request the property, but this starts getting a bit confusing when you start thinking about

  • Properties/Methods declared on interfaces and implemented on classes
  • Properties/Methods declared on a base class (virtually) and overridden on derived classes
  • Properties/Methods declared on a base class, overridden with ‘new’ (in IL world this is nothing special iirc)

At the end of the day, I just want to be able to do an intelligent equality check between two properties or two methods, I’m 80% sure that the above bullet points don’t cover all of the edge cases, and while I could just sit down, write a bunch of tests and start playing about, I’m well aware that my low level knowledge of how these concepts are actually implemented is not excellent, and I’m hoping this is an already answered topic and I just suck at searching.

The best answer would give me a couple of methods that achieve the above, explaining what edge cases have been taken care of and why 🙂


Clarification:

Literally, I want to make sure they are the same property, here are some examples

public interface IFoo
{
     string Bar { get; set; }
}

public class Foo : IFoo
{
     string Bar { get; set; }
}

typeof(IFoo).GetProperty("Bar")

and

typeof(Foo).GetProperty("Bar")

Will return two property infos, which are not equal:

public class BaseClass
{
     public string SomeProperty { get; set ; }
}

public class DerivedClass : BaseClass { }


typeof(BaseClass).GetMethod("SomeProperty")

and

typeof(DerivedClass).GetProperty("SomeProperty")

I can’t actually remember if these two return equal objects now, but in my world they are equal.

Similarly:

public class BaseClass
{
    public virtual SomeMethod() { }
}

public class DerivedClass
{
    public override SomeMethod() { }
}

typeof(BaseClass).GetMethod("SomeMethod")

and

typeof(DerivedClass).GetProperty("SomeMethod")

Again, these won’t match – but I want them to (I know they’re not specifically equal, but in my domain they are because they refer to the same original property)

I could do it structurally, but that would be ‘wrong’.

Further Notes:

How do you even request the property that’s hiding another property? Seems one of my earlier suppositions was invalid, that the default implementation of GetProperty("name") would refer to the current level by default.

BindingFlags.DeclaringType appears just to end up returning null!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:37:51+00:00Added an answer on May 19, 2026 at 3:37 am

    Taking a look at the PropertyInfo objects from your IFoo/Foo example, we can reach these conclusions:

    1. There’s no direct way to see what class/interface the property was declared on initially.
    2. Therefore, to check if the property was in fact declared on an ancestor class we need to iterate over the ancestors and see if the property exists on them as well.
    3. Same goes for interfaces, we need to call Type.GetInterfaces and work from there. Don’t forget that interfaces can implement other interfaces, so this has to be recursive.

    So let’s have a crack at it. First, to cover inherited properties:

    PropertyInfo GetRootProperty(PropertyInfo pi)
    {
        var type = pi.DeclaringType;
    
        while (true) {
            type = type.BaseType;
    
            if (type == null) {
                return pi;
            }
    
            var flags = BindingFlags.NonPublic | BindingFlags.DeclaredOnly | BindingFlags.Instance |
                        BindingFlags.Public | BindingFlags.Static;
            var inheritedProperty = type.GetProperty(pi.Name, flags);
    
            if (inheritedProperty == null) {
                return pi;
            }
    
            pi = inheritedProperty;
        }
    }
    

    Now, to cover properties declared in interfaces (search with DFS):

    PropertyInfo GetImplementedProperty(PropertyInfo pi)
    {
        var type = pi.DeclaringType;
        var interfaces = type.GetInterfaces();
    
        if (interfaces.Length == 0) {
            return pi;
        }
    
        var flags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
        var query = from iface in interfaces
                    let implementedProperty = iface.GetProperty(pi.Name, flags)
                    where implementedProperty != pi
                    select implementedProperty;
    
        return query.DefaultIfEmpty(pi).First();
    }
    

    Tying these together:

    PropertyInfo GetSourceProperty(PropertyInfo pi)
    {
        var inherited = this.GetRootProperty(pi);
        if (inherited != pi) {
            return inherited;
        }
    
        var implemented = this.GetImplementedProperty(pi);
        if (implemented != pi) {
            return implemented;
        }
    
        return pi;
    }
    

    This should work. It doesn’t take into account indexed properties with the same name but different types and/or numbers of indexing parameters, so that’s left as the proverbial excercise for the reader.

    Disclaimer: I didn’t even compile this (no time to run tests right now). It is intended as a starting point for “the” answer, since there is none so far.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.