두 유형 매개 변수 인 Generic<A, B>
을 사용하는 제네릭 클래스가 있습니다. 이 클래스에는 매우 긴 별개의 서명이있는 메소드가 있으며 A
및 B
은 별개입니다. 그러나 A == B
인 경우 서명이 정확히 일치하고 과부하 해결을 수행 할 수 없습니다. 어떻게 든이 사건의 전문화 방법을 지정할 수 있습니까? 또는 컴파일러가 임의로 일치하는 오버로드 중 하나를 선택하도록 강제 하시겠습니까? 당신이 독단적으로 일을 결정하는 컴파일러를 원하는 경우에, 당신이 메서드를 호출의 목적은 무엇사례 A == B에 대해 GenericType <A,B>의 구현을 특수화 하시겠습니까?
using System;
namespace Test
{
class Generic<A, B>
{
public string Method(A a, B b)
{
return a.ToString() + b.ToString();
}
public string Method(B b, A a)
{
return b.ToString() + a.ToString();
}
}
class Program
{
static void Main(string[] args)
{
Generic<int, double> t1 = new Generic<int, double>();
Console.WriteLine(t1.Method(1.23, 1));
Generic<int, int> t2 = new Generic<int, int>();
// Following line gives:
// The call is ambiguous between the following methods
// or properties: 'Test.Generic<A,B>.Method(A, B)' and
// 'Test.Generic<A,B>.Method(B, A)'
Console.WriteLine(t2.Method(1, 2));
}
}
}
좋은 해결책. 그냥 두 가지 일반적인 클래스를 만드는 생각하지 않았다 :) – Damovisa