How to change the class of an object dynamically in C#?

后端 未结 10 1350
囚心锁ツ
囚心锁ツ 2021-01-05 17:09

Suppose I have a base class named Visitor, and it has 2 subclass Subscriber and NonSubscriber.

At first a visitor is start off from a NonSubscriber, i.e.

<         


        
10条回答
  •  遥遥无期
    2021-01-05 18:01

    You cant do this type of conversion. What you should do is treat mary as a visitor, and when time arrives, create a new instance of "subscriber":

    Visitor mary = new NonSubscriber();
    // Do some Visitor operations
    ...
    // Now mary is a Subscriber
    mary = new Subscriber();
    

提交回复
热议问题