[英]Method resolution when using dynamic and handling of undefined method for specific derived class
Let's assume the following inheritance graph: 让我们假设以下继承图:
A<-B<-C<-D<-E<-... (the inheritance tree is actually more complex than this example, and it contains hundreds of actual types). A <-B <-C <-D <-E <-...(继承树实际上比此示例复杂,并且包含数百个实际类型)。 I do not own those types and have no control over their implementation
我不拥有这些类型,也无法控制其实现
Let's also assume a set of static methods: 我们还假设一组静态方法:
Handle(A a), Handle(B b), Handle(C c), Handle(D d) and so on. 句柄(A a),句柄(B b),句柄(C c),句柄(D d)等。
My current implementation of Handle(A a) "dispatches" to the desired method by using the dynamic
keyword: 我当前的Handle(A a)实现通过使用
dynamic
关键字“调度”到所需的方法:
public static void Handle(A a)
{
Handle((dynamic)a);
}
public static void Handle(B b)
{
//B specific processing
}
//And so on
The host application sends me the objects in a A[]
, though each object can have a different runtime type. 主机应用程序通过
A[]
向我发送对象,尽管每个对象可以具有不同的运行时类型。 As it stands, I'm not even interested in objects of type A
. 就目前而言,我什至对
A
类型A
对象都不感兴趣。
I need different Handle
methods because the processing the customer wants to perform is different based on the runtime type of the objects. 我需要不同的
Handle
方法,因为客户要执行的处理根据对象的运行时类型而不同。
My implementation works very well as long as I have an Handle
method in my code with the corresponding signature for the runtime type of the objects I'm passed in, but as it stands right now, when an object is passed that doesn't have a specific Handle
method, the Handle(A a)
method is called recursively causing a stack overflow. 只要我的代码中有一个
Handle
方法,并为传入的对象的运行时类型提供相应的签名,我的实现就可以很好地工作,但是就目前而言,当传入的对象没有作为特定的Handle
方法, Handle(A a)
方法被递归调用,从而导致堆栈溢出。
I obviously can't define a Handle(X x)
method for each of the hundred or so types that I might get passed from the host application, and each subsequent version of that host application's API can define new types. 很显然,我无法为可能从主机应用程序传递的大约一百种类型定义
Handle(X x)
方法,并且该主机应用程序API的每个后续版本都可以定义新类型。
So my question is how to handle types that do not have a specific Handle
method without having to do an endless series of if
statements, or even a long switch
statement, to filter the objects for which I don't have a handler method? 因此,我的问题是如何处理没有特定
Handle
方法的类型,而不必执行无休止的一系列if
语句甚至长的switch
语句来筛选我没有处理程序方法的对象?
Is there any way, at runtime, to determine if a Handle
method actually exists for the runtime type of the passed in object? 在运行时,是否有任何方法可以确定传入的对象的运行时类型是否确实存在
Handle
方法? Or are there other ways to cleanly handle the "missing" methods? 还是有其他方法可以彻底处理“缺失”方法?
Any insight/recommendations welcome. 欢迎任何见解/建议。
I don't remember where I got this idea of dispatching within Handle(A a)
. 我不记得我在
Handle(A a)
得到这种调度的想法Handle(A a)
。 I recall seeing something like this on some website, but I now realize that it must have applied to a different use case than what I'm trying to achieve. 我记得在某些网站上看到过类似的东西,但是我现在意识到,它一定已应用于与我要实现的目的不同的用例。
I solved my problem by simply moving the cast (dynamic)obj
outside the "Handling" methods, and back onto the caller: 我通过简单地将强制转换
(dynamic)obj
移到“处理”方法之外并返回到调用方来解决了我的问题:
Logger.Handle((dynamic)obj);
And in my implementations, the one for the base of the hierarchy is now just an empty method: 在我的实现中,用于层次结构基础的一个现在只是一个空方法:
public class Logger
{
public static void Handle(A a){}
public static void Handle(B b)
{
//B specific handling
}
//All other Handle methods
}
This solves the problem. 这样就解决了问题。 If a specific method doesn't exist for a derived type, then the
Handle(A a)
method is called, doing nothing. 如果派生类型不存在特定方法,则调用
Handle(A a)
方法,不执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.