Home > Mobile >  Dynamic property in c# depending on enum value
Dynamic property in c# depending on enum value

Time:12-17

Im trying to create make a property in a class dyanmic depending on the enum property of the class.

e.g i have a class of meeting and it has an enum of locations, if a location is picked the location details should be different for example if Inperson is picked then the location details should be a class of type Address else if the location of zoom is picked the details should just be a string with the url

public enum Meeting_Location
{
InPerson,
Zoom,
GoogleMeet
}

public class Meeting
{
public string Name;
public Meeting_Location Location;
public ... Location_Details; --> this is dynamic depending on the enum that is selected
}

public class Address
{
public string postcode;
public string country;
public string StreetName;
....

}

CodePudding user response:

In this case, I don't suggest you to use an enum to tell the type of the meeting. Instead, you should use polymorphism. To make the class itself being the type of meeting.

public abstract class Meeting
{
    public string Name { get; set; }
}

public class InPersonMeeting : Meeting
{
    public Address Location { get; set; }
}

public class ZoomMeeting : Meeting
{
    public string Link { get; set; }
}
  • Related