S
Steve
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 AmbiguousMatchException
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.Reflection;
namespace TestHide
{
class foo
{
[STAThread]
static void Main(string[] args)
{
Derived d = new Derived( "test" );
Console.WriteLine( "PropType:\t" +
d.Problem.GetType().ToString() );
try
{
PropertyInfo p = d.GetType().GetProperty
( "Problem" );
}
catch(AmbiguousMatchException e)
{
Console.WriteLine( "Error:\t\t" + e.Message );
PropertyInfo[] p = d.GetType().GetProperties();
for( int n = 0; n < p.Length; n++ )
{
Console.WriteLine( "Name: " + p[n].Name
+ "\tType: " + p[n].PropertyType
+ "\tDecl: " + p[n].DeclaringType );
}
}
catch(Exception e)
{
Console.WriteLine( 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.Problem;
}
set
{
base.Problem = (string)value;
}
}
}
}
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 AmbiguousMatchException
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.Reflection;
namespace TestHide
{
class foo
{
[STAThread]
static void Main(string[] args)
{
Derived d = new Derived( "test" );
Console.WriteLine( "PropType:\t" +
d.Problem.GetType().ToString() );
try
{
PropertyInfo p = d.GetType().GetProperty
( "Problem" );
}
catch(AmbiguousMatchException e)
{
Console.WriteLine( "Error:\t\t" + e.Message );
PropertyInfo[] p = d.GetType().GetProperties();
for( int n = 0; n < p.Length; n++ )
{
Console.WriteLine( "Name: " + p[n].Name
+ "\tType: " + p[n].PropertyType
+ "\tDecl: " + p[n].DeclaringType );
}
}
catch(Exception e)
{
Console.WriteLine( 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.Problem;
}
set
{
base.Problem = (string)value;
}
}
}
}