非常によく似た2つの一般的な方法があります。1 つは明示的な戻り値の型で呼び出すことができ、もう 1 つは戻り値の型がobj
提供されたものと同じであると推測できます。
最初のメソッドは次のように呼び出されます。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);
}
そして、自動のものはそのように呼ばれます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.
これで、これらの定義がお互いにどのようにあいまいであるかがわかります。奇妙なことに、コンパイル エラーは発生しませんが、2 番目のメソッドが自分自身を呼び出すだけなので、実行時にスタック オーバーフローが発生します。
いずれかのメソッドの名前を変更せずに、2 番目のメソッドが最初のメソッドを呼び出すようにする方法はありますか?