473,666 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Base Class Prop not hidden to Reflection

Hello,

I'm encountering an unexpected behavior when using
the "new" modifier in a derived class to hide an inherited
base class property. I use "new" intentionally so I can
change the Type of the property in the derived class, and
I can use the derived class as expected through standard
instantiation. The unexpected behavior occurs when I try
to set gather the PropertyInfo for the derived class
property via Reflection. I get an AmbiguousMatchE xception
as both the base class and derived class property
implementations are found. Perhaps I don't fully
understand the "new" modifier - I expected the base class
member to be hidden completely when working with the
derived class. I have included a code sample that
illustrates the behavior. If anyone could explain this, I
would be very interested in understanding. Thanks.

using System;
using System.Reflecti on;

namespace TestHide
{
class foo
{

[STAThread]
static void Main(string[] args)
{
Derived d = new Derived( "test" );

Console.WriteLi ne( "PropType:\ t" +
d.Problem.GetTy pe().ToString() );

try
{
PropertyInfo p = d.GetType().Get Property
( "Problem" );
}
catch(Ambiguous MatchException e)
{
Console.WriteLi ne( "Error:\t\t " + e.Message );

PropertyInfo[] p = d.GetType().Get Properties();
for( int n = 0; n < p.Length; n++ )
{
Console.WriteLi ne( "Name: " + p[n].Name
+ "\tType: " + p[n].PropertyType
+ "\tDecl: " + p[n].DeclaringType );
}
}
catch(Exception e)
{
Console.WriteLi ne( e.Message );
}
}

}
public class Base
{
protected int _works = 0;
protected object _problem = null;

public Base(){}

public virtual int Works
{
get
{
return _works;
}
set
{
_works = value;
}
}

public virtual object Problem
{
get
{
return _problem;
}
set
{
_problem = value;
}
}
}

public class Derived : Base
{

public Derived(){}

public Derived(string problem)
{
this.Problem = problem;
}

public override int Works
{
get
{
return base.Works - 1;
}
set
{
base.Works = value + 2;
}
}

public new string Problem
{
get
{
return (string)base.Pr oblem;
}
set
{
base.Problem = (string)value;
}
}
}
}

Jul 21 '05 #1
1 2653
Sorry, forgot the output from the code listing.

PropType: System.String
Error: Ambiguous match found.
Name:Works Type:System.Int 32 Decl:TestHide.D erived
Name:Problem Type:System.Str ing Decl:TestHide.D erived
Name:Problem Type:System.Obj ect Decl:TestHide.B ase
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2789
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied into this property in its Load event. So, I gave the BaseForm and the property 'abstract' modifier, and put the implementation of the property in the inherited class; say MyForm. However, when I did this, I no longer can open MyForm in the design...
10
7360
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
3
5076
by: J E E | last post by:
Hi! Is it possible to access fields in a derived class using reflection? Code below works fine when I access it as a private member in the Page class, but not when accessing base class member through an interface reference. I have tried to change the snd argement to SetAttribute method from 'Name', 'set_Name' to '_name'. That doesn't seem to be the problem. I
15
12779
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
5
3153
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
6
2141
by: Philip Warner | last post by:
I have a set of classes: C (base) C1 (inherits C) C2 (inherits C) ... Cn (inherits C) (the C1..Cn also have subclasses, but thats not really relevant to the question)
1
242
by: Steve | last post by:
Hello, I'm encountering an unexpected behavior when using the "new" modifier in a derived class to hide an inherited base class property. I use "new" intentionally so I can change the Type of the property in the derived class, and I can use the derived class as expected through standard instantiation. The unexpected behavior occurs when I try to set gather the PropertyInfo for the derived class property via Reflection. I get an...
2
21423
by: Mythran | last post by:
System.Drawing.SystemColors is a class with all static properties. Here is what my short-ranged goal is... I want to enumerate all of the SystemColors properties and print the name (using reflection), and I am able to do this: PropertyInfo props = typeof(SystemColors).GetProperties(); foreach (PropertyInfo prop in props) { Console.WriteLine(prop.Name); }
3
1313
by: Alessandro [AkiRoss] Re | last post by:
Hello, I'm trying to to this: all classes of a hierarchy have a property, which is a constant object shared between each class. I'd like to have one function for getting that object, but each class should initialize that object once. For example, given the property of type Property, we have that: class Object { public:
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.