Accessing a property of derived class from the base class in C#

前端 未结 7 668
夕颜
夕颜 2020-12-03 06:06

In C#, what is the best way to access a property of the derived class when the generic list contains just the base class.

public class ClassA : BaseClass
{
          


        
7条回答
  •  北荒
    北荒 (楼主)
    2020-12-03 06:27

       BaseClass o = MyList[i];
       if (o is ClassB)
       {
          object k = ((ClassB)o).PropertyB;
       }
       if (o is ClassA))
       {
          object j = ((ClassA)o).PropertyA;
       }
    

提交回复
热议问题