I've got a simple question concerning upcasting in c#. For example I have two interfaces:
interface IUSer
{
string UserName { get; set; }
}
interface IAdmin: IUSer
{
string Password { get; set; }
}
...and a class implementing IAdmin interface:
class Admin : IAdmin
{
public string Password { get; set; }
public string UserName { get; set; }
}
When I create an object of type Admin and try to upcast it to IUser, technically I can then only access UserName user.UserName:
var admin = new Admin() { UserName = "Arthur", Password = "hello" };
var user = (IUSer)admin;
Console.WriteLine( user.UserName );
...but If I cycle through this object's properties using Reflection, like that:
private static void Outputter(IUSer user)
{
foreach (var prop in user.GetType().GetProperties())
{
Console.WriteLine(prop.Name + " " + prop.GetValue(admin));
}
}
...I can also see Password property. The question is - why it is saved when upcasted?