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 939129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:44:28+00:00 2026-05-15T21:44:28+00:00

I am given: A class T which implements properties that are decorated with Attributes

  • 0

I am given:

  • A class “T” which implements properties that are decorated with Attributes that I am interested in.

  • A PropertyInfo “p” representing a property belonging to (1) an interface “I” that T implements, (2) a base class “B” that T inherits from, or (3) the class T itself.

I need to get the Attributes defined on (or inherited by) the properties in T, even if p‘s DeclaringType is an interface that T implements or a class that T inherits from. So I need a way to get from p to the PropertyInfo that actually exists on T. Thus it seems that there are 3 cases:

  1. p.DeclaringType == typeof(T)

    This is the trivial case. I can iterate through T‘s properties until I find the match. Example:

    return typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                    .FirstOrDefault(property => property == p)
    
  2. p.DeclaringType == typeof(I) where I is an interface that T implements

    This is more complicated. I found that I can use an InterfaceMapping to find the corresponding implementation of the p in T. It seems kind of hackish, but here what works:

    if (p.DeclaringType.IsInterface &&
        typeof(T).GetInterfaces().Contains(p.DeclaringType))
    {
        InterfaceMapping map = typeof(T).GetInterfaceMap(p.DeclaringType);
        MethodInfo getMethod = p.GetGetMethod();
        for (int i = 0; i < map.InterfaceMethods.Length; i++)
        {
            if (map.InterfaceMethods[i] == getMethod)
            {
                MethodInfo target = map.TargetMethods[i];
                return typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                .FirstOrDefault(property => property.GetGetMethod() == target);
            }
        }
    }
    
  3. p.DeclaringType == typeof(B) where B is a class that T inherits from

    I have not yet been able to figure out how to identify the property on T that overrides a virtual property p defined in Ts base class B.

So my questions are

  • In case 3 how do I find the PropertyInfo on T that overrides the property p on B?
  • Is there a better way to do all three? Perhaps a way to unify the three cases.
  • 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-15T21:44:29+00:00Added an answer on May 15, 2026 at 9:44 pm

    I found a way to answer case 3. The key is MethodInfo.GetBaseDefinition

    if (typeof(T).IsSubclassOf(p.DeclaringType))
    {
        MethodInfo baseDefinition = p.GetGetMethod().GetBaseDefinition();
        return typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                        .FirstOrDefault(property => property.GetGetMethod().GetBaseDefinition() == baseDefinition);
    }
    

    However this only works for properties that have been overridden on T.

    Edit:

    By comparing the following, it doesn’t matter if the properties have or haven’t been overriden on T:

    property.GetGetMethod().GetBaseDefinition().MetadataToken == baseDefinition.MetadataToken
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that implements properties in a specific way, to handle some
This is a rather simple question, I have a base class which implements common
Given an aggregation of class instances which refer to each other in a complex,
Given a class, org.eclipse.ui.views.navigator.ResourceNavigator for example, how do I find out which jar file
Is there any possibility that GetPropInfo returns nil even if the given class is
Given a class instance, is it possible to determine if it implements a particular
Program design: Class A, which implements lower level data handling Classes B-E, which provide
Same for methods too: I am given two instances of PropertyInfo or methods which
I have a base class which contains an abstract/MustInherit method. I want this method
I have a class below, i have extracted all the properties to a Interface

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.