Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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.

NonSubscriber mary = new NonSubscriber();

Then later on this "mary" subscribed to some services, and I want to change the type of "mary" to Subscriber.

What is the conventional way to do that?

like image 920
Chris Avatar asked Mar 23 '10 08:03

Chris


2 Answers

can't do that. sorry. C# is not a dynamic language.

like image 150
cruizer Avatar answered Sep 28 '22 03:09

cruizer


You will have to create a new mary = new Subscriber(); and copy all relevant properties.

But a better approach might be to model it differently: Give Visitor a list of subscriptions. An empty list means a NonSubscriber.

like image 25
Henk Holterman Avatar answered Sep 28 '22 02:09

Henk Holterman