简体   繁体   English

动态属性 c# 取决于枚举

[英]Dynamic property c# depending on enum

Im trying to create make a property in a class dyanmic depending on the enum property of the class.我试图根据 class 的枚举属性在 class 动态中创建属性。

eg 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例如,我有一个会议的 class 并且它有一个位置枚举,如果选择一个位置,则位置详细信息应该不同,例如,如果选择 Inperson,则位置详细信息应该是地址类型的 class,否则如果缩放位置是选择的细节应该只是一个带有 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;
....

}

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.使 class 本身成为会议类型。

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM