C#: Generic method doesn#39;t call specific method overload(C#:通用方法不调用特定方法重载)
问题描述
我正在尝试在 C# 中创建一个泛型方法,它将根据其主体中的参数数据类型调用不同的方法,然后处理它们的结果.我试图通过创建一个通用包装器方法来实现这一点,然后提供处理方法的几个重载 - 包括一个通用的,如果没有特定的重载可用.
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.
我需要代码与 Mono 2.6 兼容.
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();
}
}
推荐答案
有什么方法可以纠正这种行为吗?
Is there any way correct this behavior?
根据 C# 语言规范,这已经是正确的行为.FuncWrap
中调用的 Func
的重载通常在编译时确定,因此它不能选择不同的 Func
基于执行时类型的重载.
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);
}
现在将根据 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);
}
}
这显然很可怕.
这篇关于C#:通用方法不调用特定方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C#:通用方法不调用特定方法重载
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30