本文介绍了C#:通用方法不会调用特定的方法重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在C#中创建一个通用方法,它将根据其身体中的参数数据类型调用不同的方法,然后处理它们的结果。我试图通过创建一个通用的包装方法来实现这一点,然后提供处理方法的一些重载 - 包括一个通用的,如果没有特定的重载可用的情况下。
当我直接调用处理方法时,正确选择适当的版本。然而,当我从包装器方法中调用它时,它总是选择泛型类型,即使我传递给它的特定数据类型存在匹配的重载。
有没有办法调整代码以使其符合我的需要?或者我必须使用不同的方法。
我需要代码与Mono 2.6兼容。
使用System;
类程序
{
static void Func< T>(T val)
{
Console.WriteLine(Generic Func);
static void Func(int val)
{
Console.WriteLine(Int Func);
static void Func(string val)
{
Console.WriteLine(String Func);
static void FuncWrap< T>(T val)
{
Console.Write(Wrap:);
Func(val);
static void Main(string [] args)
{
Func(2);
Func(Potato);
Func(2.0);
FuncWrap(2);
FuncWrap(Potato);
FuncWrap(2.0);
Console.Read();
$ div $解析方案
根据C#语言规范,它已经是正确的行为。在 FuncWrap
中调用的 Func
的重载通常在编译时确定,因此它可以根据执行时间类型选择不同的 Func
重载。
改变行为的一种方法是,然而,使用动态类型:
static void FuncWrap< T>(T val)
{
Console.Write(Wrap:);
dynamic x = val;
Func(x);
}
现在将在执行时根据实际情况执行重载解析 x
的值的类型。这会产生一个性能成本,但应该做你想做的事情。
另外,你可以硬编码重载知识:
static void FuncWrap< T>(T val)
{
Console.Write(Wrap:);
if(typeof(T)== typeof(string))
{
Func((string)(object)val);
if(typeof(T)== typeof(int))
{
Func((int)(object)val);
}
else
{
Func(val);
}
}
这显然非常可怕。
I am trying to create a generic method in C#, which will call different methods based on the argument datatype in its body and process their result afterwards. I am trying to achieve this by creating a generic wrapper method and then provide several overloads of the processing method - including a generic one that'll be used if no specific overload is available.
When I call the processing method directly, appropriate version is correctly selected. However when I call it from the wrapper method it always selects the generic one, even if there's a matching overload for the specific datatype I passed to it.
Is there any way to adjust the code to make it behave the way I need to? Or do I have to use a different approach.
I need the code to be compatible with Mono 2.6.
using System;
class Program
{
static void Func<T>(T val)
{
Console.WriteLine("Generic Func");
}
static void Func(int val)
{
Console.WriteLine("Int Func");
}
static void Func(string val)
{
Console.WriteLine("String Func");
}
static void FuncWrap<T>(T val)
{
Console.Write("Wrap: ");
Func(val);
}
static void Main(string[] args)
{
Func(2);
Func("Potato");
Func(2.0);
FuncWrap(2);
FuncWrap("Potato");
FuncWrap(2.0);
Console.Read();
}
}
解决方案
It's already the correct behaviour according to the C# language specification. The overload of Func
called within FuncWrap
is normally determined at compile time, so it can't pick a different Func
overload based on the execution-time type.
One way of changing the behaviour, however, is to use dynamic typing:
static void FuncWrap<T>(T val)
{
Console.Write("Wrap: ");
dynamic x = val;
Func(x);
}
That will now perform overload resolution at execution time based on the actual type of the value of x
. This incurs a performance cost, but should do what you want it to.
Alternatively, you could hard-code knowledge of the overloads:
static void FuncWrap<T>(T val)
{
Console.Write("Wrap: ");
if (typeof(T) == typeof(string))
{
Func((string)(object)val);
}
else if (typeof(T) == typeof(int))
{
Func((int)(object)val);
}
else
{
Func(val);
}
}
That's clearly pretty horrible though.
这篇关于C#:通用方法不会调用特定的方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!