为什么编译器选择了错误的方法重载? [英] Why is the compiler choosing the wrong method overload?

查看:140
本文介绍了为什么编译器选择了错误的方法重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的方法:

public void CacheDelegate(Object obj, MemberInfo memberInfo)
{
   switch (memberInfo.MemberType)
   {
    case MemberTypes.Field:
       var fieldInfo = (FieldInfo) memberInfo;
       CacheDelegate(obj, fieldInfo);
       break;
    case MemberTypes.Property:
       var propertyInfo = (PropertyInfo) memberInfo;
       CacheDelegate(obj, propertyInfo);
       break;
    case MemberTypes.Method:
       var methodInfo = (MethodInfo) memberInfo;
       CacheDelegate(obj, methodInfo);
       break;
    default:
       throw new Exception("Cannot create a delegate for MemberInfo provided.");
    }
}

上面的方法解析memberInfo的类型,并从以下方法调用适用的方法:

The method above resolves the type of the memberInfo and calls the applicable method from the following:

public void CacheDelegate(Object obj, FieldInfo fieldInfo)
{
   // Do stuff...
}

public void CacheDelegate(Object obj, PropertyInfo propertyInfo)
{
   // Do stuff...
}

public sealed override void CacheDelegate(Object obj, MethodInfo methodInfo)
{
   // Do stuff...
}

问题在于,最后一个案例标签case MemberTypes.Method不会以Method Info重载调用CacheDelegate方法,而是以Member Info重载调用CacheDelegate!因此,它基本上只是一次又一次地递归调用自己.我尝试在调用方法时指定参数名称methodInfo:methodInfo,但是Unity引擎告诉我最好的重载方法不包含名为methodInfo的参数.

The problem is that the last case label, case MemberTypes.Method, doesn't call the CacheDelegate method with the Method Info overload, but calls the CacheDelegate with the Member Info overload instead! So it's basically just calling itself over and over again, recursively. I tried specifying the parameter name, methodInfo: methodInfo when calling the method, but then the Unity engine is telling me the best overloaded method does not contain a parameter named methodInfo.

我完全不知道为什么会这样. 任何帮助将不胜感激.

I'm quite at a loss of why this is happening. Any help would be greatly appreciated.

推荐答案

重载解析如下.

从调用的类型开始,找到在该类型上声明的可以使用的方法集.

Starting with the type called on, find the set of methods declared on that type that can be used.

如果该集合为空,则尝试对声明的基本类型或接口进行相同的操作.继续向上移动层次结构,直到找到至少一种匹配的方法,否则将出错.

If that set is empty, then try the same with the base type or interfaces declared. Keep moving up the hierarchy until at least one method that matches is found, or else error.

在找到的集合中,使用最特定的方法.如果是平局则出错.

Of the set that is found, use the most specific method. Error if it's a tie.

因此,在这四个方法中,有三个在此类中声明.这三个中的两个不适用.显然,只有public void CacheDelegate(Object obj, MemberInfo memberInfo)可以作为正确的类来调用,因此它被调用.

So, of the four methods, three were declared in this class. Of those three two are not applicable. That leaves only public void CacheDelegate(Object obj, MemberInfo memberInfo) as clearly the correct class to call, so it is called.

由于基本类型只有一个CacheDelegate重载可供选择,因此您可以使用((BaseType)this).CacheDelegate(obj, methodInfo);强制进行所需的呼叫.

You could use ((BaseType)this).CacheDelegate(obj, methodInfo); to force the call you want, since the base type has only one CacheDelegate overload to choose between.

这篇关于为什么编译器选择了错误的方法重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆