对特定派生类使用动态方法和未定义方法的处理时的方法解析

对特定派生类使用动态方法和未定义方法的处理时的方法解析

本文介绍了对特定派生类使用动态方法和未定义方法的处理时的方法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设以下继承图:

Let's assume the following inheritance graph:

A< -B< C< D< -E< -...(继承树实际上比该示例更复杂,并且包含数百个实际类型). 我不拥有这些类型,也无法控制其实现

A<-B<-C<-D<-E<-... (the inheritance tree is actually more complex than this example, and it contains hundreds of actual types). 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)等.

Handle(A a), Handle(B b), Handle(C c), Handle(D d) and so on.

我当前对Handle(A a)的实现通过使用dynamic关键字调度"到所需的方法:

My current implementation of Handle(A a) "dispatches" to the desired method by using the dynamic keyword:

public static void Handle(A a)
{
    Handle((dynamic)a);
}

public static void Handle(B b)
{
    //B specific processing
}

//And so on

尽管每个对象可以具有不同的运行时类型,但宿主应用程序将A[]中的对象发送给我.就目前而言,我什至对A类型的对象都不感兴趣.

The host application sends me the objects in a A[], though each object can have a different runtime type. As it stands, I'm not even interested in objects of type A.

我需要不同的Handle方法,因为客户要执行的处理根据对象的运行时类型而不同.

I need different Handle methods because the processing the customer wants to perform is different based on the runtime type of the objects.

只要我的代码中有一个Handle方法,并为传入的对象的运行时类型提供相应的签名,我的实现就可以很好地工作,但是就目前而言,当传入一个对象时没有特定的Handle方法,则Handle(A a)方法被递归调用,从而导致堆栈溢出.

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(X x)方法,并且该主机应用程序API的每个后续版本都可以定义新类型.

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方法的类型,而不必执行无休止的一系列if语句,甚至是长的switch语句,以过滤我所针对的对象没有处理程序方法?

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方法?还是有其他方法可以彻底处理遗漏"方法?

Is there any way, at runtime, to determine if a Handle method actually exists for the runtime type of the passed in object? Or are there other ways to cleanly handle the "missing" methods?

欢迎任何见解/建议.

推荐答案

我不记得我在Handle(A a)中得到这种分派的想法了.我记得在某些网站上看到过类似的情况,但是我现在意识到,它一定已应用于与我要实现的目的不同的用例.

I don't remember where I got this idea of dispatching within 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.

我通过简单地将强制转换(dynamic)obj移动到处理"方法之外,然后又返回到调用方来解决了我的问题:

I solved my problem by simply moving the cast (dynamic)obj outside the "Handling" methods, and back onto the caller:

 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
 }

这解决了问题.如果对于派生类型不存在特定的方法,则将调用Handle(A a)方法,不执行任何操作.

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.

这篇关于对特定派生类使用动态方法和未定义方法的处理时的方法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:08