Welcome to MSDN Blogs Sign in | Join | Help

Overriding a property using new and reflection

When you override a property using new with a new type there is discrepancy in behavior from C# and reflection. C# is not aware of the property in the base class and it has been overridden by the new property. But reflection is well aware of both the properties depending on what you are asking for. If you want the derived class property only you need to specify the BindingFlags.DeclaredOnly flag or else it will walk the entire hierarchy.

using System;

using System.Reflection;

class Base {

    public string MyName {

        set {}

  get{ return "Hello";}

 }a

}

class Derived : Base {

 public new int MyName {

  set {}

 }

}

class Test {

 public static void Main() {

  Base b = new Base();

  Derived d = new Derived();

  // Trying to get all properties works

  //

  PropertyInfo[] p = (PropertyInfo [])typeof(Derived).GetProperties();

  foreach(PropertyInfo p1 in p) {

   Console.WriteLine(p1.ToString());

  }

  // Trying to get the property from the derived class alone works

  //

  PropertyInfo p2 = typeof(Derived).GetProperty("MyName", BindingFlags.DeclaredOnly|BindingFlags.Instance | BindingFlags.Public);

  if(p2 != null) {

   Console.WriteLine(p2.ToString());

  }

  // This call is ambiguous and throws an exception (System.Reflection.AmbiguousMatchException)

  //

  typeof(Derived).GetProperty("MyName",BindingFlags.Instance | BindingFlags.Public);

  // This will not compile as the get property is not defined in the derived class

  //

  // Console.WriteLine("Value of MyName in derived class: ", d.MyName);

 }

}

Published Friday, March 17, 2006 2:31 AM by thottams@microsoft.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# New-ish CLR/Reflection Blogger

I mentioned a while back that I change from being a PM for System.Reflection and System.Reflection.Emit..e.t.c...
Thursday, April 27, 2006 11:24 PM by Kathy Kam

# re: Overriding a property using new and reflection

Thank so much for posting this ... it really helped me overcome this exact problem.
Friday, August 11, 2006 11:03 AM by Benjamin Mitchell

# re: Overriding a property using new and reflection

Thanks for the note. It feels good to find that my posting helped someone and encourages me to post more like this.
Friday, August 11, 2006 5:34 PM by Thottam Sriram

# re: Overriding a property using new and reflection

Is there a way to get the inherited properties as well while using BindingFlags.DeclaredOnly? Suppose that the base class had the property A, which the derived class wishes to get (e.g. typeof(Derived).GetProperty("A", ...)). i.e. is there a way to return all inherited properties, while at the same time, if there is are multiple properties with the same name, return only those declared at the calling-type's level? Thanks a lot

Monday, April 23, 2007 1:57 PM by Michael Tan

# Binding to hidden properties using Reflection

Thottam blogged about how C# and Reflection differ in how they bind to hidden properties: http://blogs.msdn.com/thottams/archive/2006/03/17/553376.aspx

Thursday, April 26, 2007 2:08 AM by Weitao Su's WebLog

# re: Overriding a property using new and reflection

I discussed binding to hidden properties using Reflection here: http://blogs.msdn.com/weitao/archive/2007/04/26/binding-to-hidden-properties-using-reflection.aspx

Thursday, April 26, 2007 2:23 AM by wesu

# How to override a property with Reflection Emit | keyongtech

Wednesday, January 21, 2009 10:08 PM by How to override a property with Reflection Emit | keyongtech

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker