我能像这样充满活力地工作吗?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用即席界面来执行此操作。
You can use Impromptu Interface to do that.
如果您确实想要使用动态,那么为什么要进行转换呢?
但我仍然希望 Patient 实现 IPerson。从设计角度来看,这更有意义,并且类型安全意味着任何错误都会在编译时而不是运行时捕获。
If you really want to use dynamic, then why the conversion?
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.
不!我认为(不实施 IPatient)这是不可能的。您必须实现
IPatient
接口或删除强制转换。
No! I don't think (without implementing IPatient) that is possible. You have to implement
IPatient
interfaceOr remove casting.
IPerson 是不必要的。只需像这样执行
Display_Person
即可:只要
person
实现这些属性,就可以正常工作。IPerson is unnecessary. Just do the
Display_Person
like this:So long as
person
implements those properties, this works fine.