我能像这样充满活力地工作吗?

发布于 2024-12-08 21:50:38 字数 1058 浏览 0 评论 0原文

我有一个 Patient 类:

class Patient {
  public string First_Name { get; set; }
  public string Last_Name { get; set; }
  public DateTime Date_of_Birth { get; set; }
}

我还有一个界面:

interface IPerson {
  string First_Name { get; }
  string Last_Name { get; }
}

在这个控制台应用程序中,我希望 Display_Person 方法能够工作。它可以编译,但会引发运行时错误,因为 Patient 没有实现 IPerson。

class Program {
  static void Main(string[] args) {
    Patient p = new Patient { 
      First_Name = "Charles", Last_Name = "Lambert",
      Date_of_Birth = new DateTime(1976,5,12),
    };
    Display_Person(p);
  }

  static void Display_Person(dynamic person) {
    IPerson p = person;
    Console.WriteLine("{0}, {1}", p.Last_Name, p.First_Name);
  }
}

在不让 Patient 实现 IPerson 接口的情况下,我可以对哪些代码进行更改以使 Display_Person 方法正常工作?我更喜欢可重复使用的解决方案。

更新:我希望它能够工作,这样我就可以获得智能感知。请忽略这个例子的琐碎之处。它简短而切中要害地解释了我的问题。如果这是 1003 个贷款申请(打印时有书本大小),我不想在我的类中应用 20 多个接口,这样我就可以对相关数据进行分组进行计算。我也不希望每次都输入所有这些属性。过去,由于缺乏智能感知,我不再使用动态语言。 (我不懒惰,我很有效率!)

I have a Patient class:

class Patient {
  public string First_Name { get; set; }
  public string Last_Name { get; set; }
  public DateTime Date_of_Birth { get; set; }
}

I also have an interface:

interface IPerson {
  string First_Name { get; }
  string Last_Name { get; }
}

in this console application, I would like the Display_Person method to work. It compiles but throws a run time error because Patient does not implement IPerson.

class Program {
  static void Main(string[] args) {
    Patient p = new Patient { 
      First_Name = "Charles", Last_Name = "Lambert",
      Date_of_Birth = new DateTime(1976,5,12),
    };
    Display_Person(p);
  }

  static void Display_Person(dynamic person) {
    IPerson p = person;
    Console.WriteLine("{0}, {1}", p.Last_Name, p.First_Name);
  }
}

What code changes, without having Patient implement the IPerson interface, can I make to get the Display_Person method to work? I would prefer a solution that is reusable.

Update: I want this to work so I can get intellisense. Please look past the triviality of this example. It short and to the point at explaining my problem. If this were 1003 loan application (when printed is the size of a book), I would not want to apply 20+ interfaces to my class so I can group related data for calculations. I also would not like having to type out all of those properties every time. The lack of intellisense has steered me away from using dynamic languages in the past. (I'm not lazy i'm efficient!)

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

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

发布评论

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

评论(4

何以畏孤独 2024-12-15 21:50:38

您可以使用即席界面来执行此操作。

You can use Impromptu Interface to do that.

晨曦÷微暖 2024-12-15 21:50:38

如果您确实想要使用动态,那么为什么要进行转换呢?

static void Display_Person(dynamic person) {
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}

但我仍然希望 Patient 实现 IPerson。从设计角度来看,这更有意义,并且类型安全意味着任何错误都会在编译时而不是运行时捕获。

If you really want to use dynamic, then why the conversion?

static void Display_Person(dynamic person) {
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}

But I would still prefer if Patient implemented IPerson. It would make much more sense from a design perspective, and the type safety means any error would get caught at compile time instead of run time.

梦忆晨望 2024-12-15 21:50:38

不!我认为(不实施 IPatient)这是不可能的。您必须实现 IPatient 接口

class Patient : IPatient {}

或删除强制转换。

static void Display_Person(dynamic person) 
{
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}

No! I don't think (without implementing IPatient) that is possible. You have to implement IPatient interface

class Patient : IPatient {}

Or remove casting.

static void Display_Person(dynamic person) 
{
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}
慕烟庭风 2024-12-15 21:50:38

IPerson 是不必要的。只需像这样执行 Display_Person 即可:

static void Display_Person(dynamic person) {
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}

只要 person 实现这些属性,就可以正常工作。

IPerson is unnecessary. Just do the Display_Person like this:

static void Display_Person(dynamic person) {
    Console.WriteLine("{0}, {1}", person.Last_Name, person.First_Name);
}

So long as person implements those properties, this works fine.

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