简体   繁体   English

泛型方法调用错误的重载

[英]Calling the wrong overload for generic method

I have two generic methods which are very similar. 我有两种非常相似的通用方法。 The idea is that one can be called with an explicit return type, whereas the other infers that the return type is the same as the obj provided. 这个想法是,可以使用显式的返回类型调用该方法,而另一个可以推断该返回类型与提供的obj相同。

The first method would be called like: RestResponse response = myCat.Put<RestResponse>() 第一个方法将被称为: RestResponse response = myCat.Put<RestResponse>()

//Perform a PUT on the current resource, returning the server response deserialized to a new object of the specified type.</summary>
//<typeparam name="T">The expected type of the resource post response. Use 'IRestResponse' to skip deserializing the request.</typeparam>
public static T Put<T>(this APIResource obj, List<Parameter> parameters = null)
{
    if (parameters == null) parameters = new List<Parameter>();
    parameters.Add(new Parameter() { Value = obj, Type = ParameterType.RequestBody });
    return RequestHelper<T>(obj.collection_name + "/" + obj.id, Method.PUT, parameters);
}

And the automatic one is called as such Cat response = myCat.Put(); 而自动Cat response = myCat.Put();称为这样的Cat response = myCat.Put();

//<typeparam name="O">(Automatically Inferred) The type of the current resource, which is also the expected type of the resource request response.</typeparam>
public static O Put<O>(this O obj, List<Parameter> parameters = null) 
     where O : APIResource 
{ return obj.Put<O>(parameters); } //I want to call the first method here.

Now I can see how these definitions are ambiguous with restpect to eachother. 现在,我可以看到这些定义在相互尊重的情况下是如何模棱两可的。 The weird thing is that there's no compile errors, but at run time, I get a stack overflow because the second method just calls itself. 奇怪的是没有编译错误,但是在运行时,由于第二种方法只是调用自身而导致堆栈溢出。

Is there a way I can get the second method to call the first without changing the name of either method? 有没有一种方法可以让我在不更改任何一个方法名称的情况下调用第二个方法?

When deciding the "betterness" of two methods (which is what is done when a method call matches multiple signatures) the method that is defined "closer" to the call point is preferred. 当确定两个方法的“更好”时(这是在一个方法调用匹配多个签名时执行的操作),首选与调用点“更近”的方法。

Two of the more common examples: 两个较常见的示例:

  1. If one method is defined in that same type and another isn't, the one in that type wins. 如果一种方法定义为相同的类型,而另一种方法则没有,则该类型的方法将获胜。

  2. If one method is in the same namespace and another isn't, then the one in the same namespace wins. 如果一个方法在同一个名称空间中,而另一个不在同一个名称空间中,则该方法将获胜。

One way of resolving the ambiguity is to not leverage the fact that it is an extension method; 解决歧义的一种方法是不利用它是扩展方法的事实。 call it as if it weren't (it can still be an extension method though, for use by external callers). 好像没有调用它一样(尽管它仍然可以是扩展方法,供外部调用者使用)。

public static O Put<O>(this O obj, List<Parameter> parameters = null) 
     where O : APIResource 
{ return OtherPutClass.Put<O>(obj, parameters); }

Have you tried explicitly casting to the correct type? 您是否尝试过显式转换为正确的类型?

It would seem that something like: 好像是这样的:

public static O Put<out O>( this O obj , List<Parameter> parameters = null ) where O:APIResource
{
  return ((APIResource)obj).Put<O>(parameters) ;  //I want to call the first method here.
}

or 要么

public static O Put<out O>( this O obj , List<Parameter> parameters = null ) where O:APIResource
{
  return .Put<O>( (APIResource)obj , parameters) ;  //I want to call the first method here.
}

we get you what you want. 我们得到您想要的。

However, the fact that the type system is confused with respect to your intent might be an indication that somebody trying to fix a bug down the line is also going to be confused. 但是,类型系统与您的意图相混淆的事实可能表明有人试图修复错误,这也表示很困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM