My demo code is below:
class Base
{
}
class SubA:Base
{
private int propertyA;
public int PropertyA
{
get{return propertyA}
}
}
class SubB:Base
{
private string propertyB;
public string PropertyB
{
get{return propertyB}
}
}
class Program
{
public void Action(Base obj)
{
//here i wanna use PropertyB if the ture obj is an instance of SubB.
// use PropertyA if the true obj is an instance of SubA
}
}
The true object I transfer to function "Action
" is an instance of SubB or SubA. I wanna access the PropertyB
(if SubB
) or PropertyA
(if SubA
) in the "Action
". Do I violate some basic OO rules? What is the best way to deal with such situation(I don't wanna use the C# key word As
and Is
to test the obj I transfered). I am totally confused now. Any advice or help is greatly appreciated.