修改接口实现中特定于类型的属性

发布于 2024-08-23 05:36:36 字数 1104 浏览 10 评论 0原文

这是我所拥有的:

public interface IEvent {
  int Id
  string Title

}

public class MeetingEvent : IEvent {
  int Id
  string Title

  //Meeting Properties
  string Room;
  User Organizer; 
}

public class BirthdayEvent : IEvent {
  int Id
  string Title

  //Bday Properties
  int Candles;
  bool Surprise;
}

我正在 ASP.NET MVC 中处理自定义模型绑定器,因为我的主编辑表单继承自 IEvent,同时执行 RenderPartial 以添加其他特定于类型的字段

当我到达模型绑定器时,我可以查看该类型的所有键/值,这很好。现在,我想要这样做:

IEvent _event;

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
    _event = new BirthdayEvent();

    _event.Candles= GetValue<int>(bindingContext, "Candles");
    _event.Surprise= GetValue<bool>(bindingContext, "Surprise");
    break;

  case EventType.Meeting:
    _event = new MeetingEvent();

    // Meeting-specific fields are set here...
    break;

  default:
    throw new InvalidOperationException("Invalid Event Type");
}

本质上,我想要一个 IEvent 变量,并且想要创建一个实现 IEvent 的特定事件类型并设置特定于类型的字段。 Visual Studio告诉我它无法访问BirthdayEvent字段。

虽然我明白这一点,但我似乎不知道我需要做什么。

Here is what I have:

public interface IEvent {
  int Id
  string Title

}

public class MeetingEvent : IEvent {
  int Id
  string Title

  //Meeting Properties
  string Room;
  User Organizer; 
}

public class BirthdayEvent : IEvent {
  int Id
  string Title

  //Bday Properties
  int Candles;
  bool Surprise;
}

I am working on a custom model binder in ASP.NET MVC as my main edit form inherits from IEvent while I perform a RenderPartial to add the other type-specific fields

When I get to the model binder, I can see all the keys/values for the type, which is good. Now, I want to do this:

IEvent _event;

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
    _event = new BirthdayEvent();

    _event.Candles= GetValue<int>(bindingContext, "Candles");
    _event.Surprise= GetValue<bool>(bindingContext, "Surprise");
    break;

  case EventType.Meeting:
    _event = new MeetingEvent();

    // Meeting-specific fields are set here...
    break;

  default:
    throw new InvalidOperationException("Invalid Event Type");
}

In essence, I want an IEvent variable and I want to create a specific event type that implements IEvent and set the type-specific fields. What Visual Studio tells me is that it can not access the BirthdayEvent fields.

While, I understand this, I can't seem to figure out what I need to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-08-30 05:36:36

当您首次创建 BirthdayEventMeetingEvent 实例时,将它们放入该类型的变量中。这样您就可以分配这些成员。然后将它们分配给您的 _event 变量。像这样:

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
  {
    var be = new BirthdayEvent();

    be.Candles= GetValue<int>(bindingContext, "Candles");
    be.Surprise= GetValue<bool>(bindingContext, "Surprise");
    _event = be;
    break;
  }
  case EventType.Meeting:
  {
    var me = new MeetingEvent();

    // Meeting-specific fields are set here...
    _event = me;
    break;
  {
  default:
    throw new InvalidOperationException("Invalid Event Type");
}

When you first create your instances of BirthdayEvent and MeetingEvent, put them in variables of that type. That way you can assign those members. Then assign them to your _event variable. Like this:

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
  {
    var be = new BirthdayEvent();

    be.Candles= GetValue<int>(bindingContext, "Candles");
    be.Surprise= GetValue<bool>(bindingContext, "Surprise");
    _event = be;
    break;
  }
  case EventType.Meeting:
  {
    var me = new MeetingEvent();

    // Meeting-specific fields are set here...
    _event = me;
    break;
  {
  default:
    throw new InvalidOperationException("Invalid Event Type");
}
娇俏 2024-08-30 05:36:36

沿着这些思路:

IEvent _event;

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
    var birthdayEvent = new BirthdayEvent();

    birthdayEvent.Candles= GetValue<int>(bindingContext, "Candles");
    birthdayEvent.Surprise= GetValue<bool>(bindingContext, "Surprise");
    _event = birthdayEvent;
    break;
  /* etc */

  default:
    throw new InvalidOperationException("Invalid Event Type");
}

Something along these lines:

IEvent _event;

switch (eventType) {  // EventType is an enum
  case EventType.Birthday:
    var birthdayEvent = new BirthdayEvent();

    birthdayEvent.Candles= GetValue<int>(bindingContext, "Candles");
    birthdayEvent.Surprise= GetValue<bool>(bindingContext, "Surprise");
    _event = birthdayEvent;
    break;
  /* etc */

  default:
    throw new InvalidOperationException("Invalid Event Type");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文