I have been writing some code passing through delegates to perform the actual work, but sadly now need to deduce the delegate's parameterised type, so used overloaded functions for the types I'm interested in, but was surprised to see ambiguity as in the example below.
I realise I could cast the delegate parameter to resolve the ambiguity, but why is this ambiguity there, and is there any other way to resolve it without explicitly instantiating/casting a delegate prior the call to CallGotTParam
?
namespace ConversionAmbiguous
{
class IntShortDelegate
{
delegate void GotTParam<T> (T t);
static void GotInt32Param(System.Int32 t)
{
Console.WriteLine("GotInt32Param: {0}", t);
}
static void GotInt16Param(System.Int16 t)
{
Console.WriteLine("GotInt16Param: {0}", t);
}
void CallGotTParam(GotTParam<System.Int32> dcall)
{
dcall(1);
}
void CallGotTParam(GotTParam<System.Int16> dcall)
{
dcall(2);
}
static public void Test()
{
IntShortDelegate test = new IntShortDelegate();
// error CS0121: The call is ambiguous between the following methods or properties:
// 'ConversionAmbiguous.IntShortDelegate.CallGotTParam(Quickie.ConversionAmbiguous.IntShortDelegate.GotTParam<int>)' and
// 'ConversionAmbiguous.IntShortDelegate.CallGotTParam(Quickie.ConversionAmbiguous.IntShortDelegate.GotTParam<short>)'
test.CallGotTParam(IntShortDelegate.GotInt32Param);
GotTParam<System.Int32> d32 = IntShortDelegate.GotInt32Param;
test.CallGotTParam(d32); // This is fine
test.CallGotTParam(IntShortDelegate.GotInt16Param); // This is fine
}
} // class IntShortDelegate
} // Ends namespace ConversionAmbiguous
Compiled against .Net 3.5
Had a brief look at the csharp language specification version 4.0 sections 7.5.2 Type Inference and 7.5.3. Overload resolution, but haven't had time to study them.