接口的扩展方法
我有两个接口 IDto1 和 IDto2。 IDto2 继承 IDto1。这两个接口都用于 DTO,因此我希望在其实现中保留“复杂”代码 - 我通过在每个接口的静态类中放置一个扩展方法 Initialize 来实现这一点。
所以我最终得到以下类型 IDto1、IDto2、IDto1Extensions、IDto2Extensions。
我希望在两个接口上都有 Initialize 扩展方法,但每个接口都有不同的实现。
在客户端代码中,我想使用这样的代码:
(dto as IDto1).Initialize();
...并且我希望根据运行时 dto 变量的解析类型来调用相关的扩展方法。
这在 C# 中可能吗?如果不可能,为什么不呢?
编辑:
抱歉,当然可以:
(dto as IDto1).Initialize();
...将调用 IDto1 类型上的 Initialize 方法。我的意思是 dto 变量将作为参数传递给方法,在这种情况下,我相信考虑到我之前指定的继承层次结构,该方法将被多态地选择。
感谢您的所有回答。
I have two interfaces IDto1 and IDto2. IDto2 inherits IDto1. Both interfaces are for DTOs and so I wish to keep "complex" code out of their implementations - I do this by putting a single extension method Initialize in a static class for each interface.
So I end up with the following types IDto1, IDto2, IDto1Extensions, IDto2Extensions.
I wish to have the Initialize extension method on both interfaces, but for each to have a different implementation.
In client code I want to use code like this:
(dto as IDto1).Initialize();
...and I'd like the relevant extension method to be invoked based on the resolved type of the dto variable at runtime.
Is this possible in C# and if not why not?
Edit:
Sorry, of course this:
(dto as IDto1).Initialize();
...will invoke the Initialize method on the IDto1 type. I meant that the dto variable would be passed in as an argument to a method, under which circumstances I believe the method would be chosen polymorphically given the inheritance hierarchy I specified earlier.
Thanks for all your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您在这里试图实现的是某种多态性。扩展方法是静态方法。在 OOP 中,多态性仅适用于实例方法。
一个选择可能是切换到抽象类。
What you are trying to achieve here is some sort of polymorphysm. Extension methods are static methods. In OOP polymorphysm is applicable only for instance methods.
An option could be to switch to abstract classes.
看了你的发言:
括号中的内容的“解析类型”恰好
IDto1
。它不能是其他任何东西,因此编译器将使用它。事实上,变量永远不会改变类型(我假设您指的是变量值所引用的对象)。在 .NET 4.0 / C# 4.0 中,鸭子类型的一个选项可能是实例方法和
dynamic
......但说实话,我不明白为什么你不能只是将您的
Initialize
方法添加到接口并使用多态性,因为这听起来最接近您想要描述的内容。Looking at your statement:
The "resolved type" of the thing in brackets is precisely
IDto1
. It cannot be anything else, so that is what the compiler will work with. Indeed variables never change type (I assume you mean the object referred to by the variable's value).In .NET 4.0 / C# 4.0, an option here for duck-typing might be an instance method and
dynamic
......but to be honest I can't see why you can't just add your
Initialize
method to the interface and use polymorphism, since that sounds the closest to what you are trying to describe.您试图完成的任务是不可能的,因为扩展方法是在编译时解析的,而在您的情况下, dto 变量的实际类型仅在运行时才知道。
What you are trying to accomplish is not possible because extension methods are resolved at compile time, while in your case the actual type of the
dto
variable is known only at runtime.如果你想让你的代码更加动态,你可以在配置文件中定义你想要的类,并使用类似这样的东西
在这个示例中,我使用第一行来获取类名。
第二行创建对象的实例。
吉列尔梅·费雷拉
http://guilhermeferreira.wordpress.com/
If you want to make your code more dynamic you can define the class that you want in configuration file and use something like this
In this sample I use the first line to get's the class name.
The second line create an instance of the Object.
Guilherme Ferreira
http://guilhermeferreira.wordpress.com/
这听起来像是抽象基类的工作。
This sounds like a job for an abstract base class.