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

  • Home
  • SEARCH
  • 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 7591951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:46:53+00:00 2026-05-30T20:46:53+00:00

I have a Parent / Child class hierarchy where the Parent abstractly declares a

  • 0

I have a Parent / Child class hierarchy where the Parent abstractly declares a string property and the Child class implements it:

abstract class Parent
{
   public abstract string Value { get; }
}

class Child : Parent
{
   public override string Value { get { return null; } }
}

When I use an expression that explicitly (or implicitly) uses the Child class, I expect the Expressions’s MemberInfo’s DeclaringType to be ‘Child’, but instead it is Parent:

Child child = new Child();
Expression<Func<string>> expression = (() => child.Value);
MemberInfo memberInfo = expression.GetMemberInfo();
Assert.AreEqual(typeof(Child), memberInfo.DeclaringType); // FAILS!

The assertion fails because the DeclaringType is Parent.

Is there something I can do in declaring my expression or consuming it to reveal the actual use of the Child type?

NOTE: GetMemberInfo() above as an extension method (I even forgot we had written this!):

public static class TypeExtensions
{
    /// <summary>
    /// Gets the member info represented by an expression.
    /// </summary>
    /// <param name="expression">The member expression.</param>
    /// <returns>The member info represeted by the expression.</returns>
    public static MemberInfo GetMemberInfo(this Expression expression)
    {
        var lambda = (LambdaExpression)expression;

        MemberExpression memberExpression;
        if (lambda.Body is UnaryExpression)
        {
            var unaryExpression = (UnaryExpression)lambda.Body;
            memberExpression = (MemberExpression)unaryExpression.Operand;
        }
        else memberExpression = (MemberExpression)lambda.Body;

        return memberExpression.Member;
    }
}
  • 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-30T20:46:55+00:00Added an answer on May 30, 2026 at 8:46 pm

    No – this is an accurate representation of what gets emitted by the C# compiler. The override is effectively ignored when looking for the member – the compiler only cares about the type that originally declared the member. You can see this for yourself by compiling code and then looking at the IL. This method:

    static void Main()
    {
        Child c = new Child();
        string x = c.Value;
    }
    

    is compiled into this IL:

    IL_0000:  nop
    IL_0001:  newobj     instance void Child::.ctor()
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  callvirt   instance string Parent::get_Value()
    IL_000d:  stloc.1
    IL_000e:  ret
    

    One point of trivia: the VB compiler doesn’t work the same way, so this method:

    Public Shared Sub Main(Args As String())
        Dim x As Child = New Child()
        Dim y As String = x.Value
    End Sub
    

    is compiled as:

    IL_0000:  newobj     instance void [lib]Child::.ctor()
    IL_0005:  stloc.0
    IL_0006:  ldloc.0
    IL_0007:  callvirt   instance string [lib]Child::get_Value()
    IL_000c:  stloc.1
    IL_000d:  ret
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have a Parent/Child object/mapping as follows: class Parent { int Id; string name;
Here I have the mapping for the parent and child-class: public class Parent {
Let's have the following class hierarchy: public class ParentClass implements SomeInterface { } public
If you declare an inheritance hierarchy where both the parent and child class have
I have a Parent class. import java.util.HashMap; import java.util.Map; public class Parent { Map<String,String>
I have a parent-child relationship: @Entity @Table(name = user) public final class User {
I have a parent and child class that both need to implement IDisposable .
I have a parent class and child class (inherited from parent). In the child
I have a parent class Parent and a child class Child , defined thus:

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.