Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编译器如何在具有相似签名的2之间选择方法?_C#_.net_Enums_Attributes_Iconvertible - Fatal编程技术网

C# 编译器如何在具有相似签名的2之间选择方法?

C# 编译器如何在具有相似签名的2之间选择方法?,c#,.net,enums,attributes,iconvertible,C#,.net,Enums,Attributes,Iconvertible,我有枚举 public enum ContentMIMEType { [StringValue("application/vnd.ms-excel")] Xls, [StringValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")] Xlsx } 在扩展中,我有两种方法来获取属性值: public static string GetStringValue<TF

我有枚举

public enum ContentMIMEType
{
    [StringValue("application/vnd.ms-excel")]
    Xls,

    [StringValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")]
    Xlsx
}
在扩展中,我有两种方法来获取属性值:

public static string GetStringValue<TFrom>(this TFrom enumValue) 
            where TFrom : struct, IConvertible
{
    ...
}
这些方法有不同的签名,但当我执行下一个操作时,
ContentMIMEType.Xlsx.GetStringValue()
1st方法被采用

为什么会发生这种情况,因为第二种方法的执行对我来说更为明显(已经尝试更改排序顺序,但没有帮助)。

更为明显

仅从站点:

重载是指当两个方法具有相同的 姓名,但签名不同。在编译时,编译器会 它将调用哪一个,基于 参数和方法调用的目标

当编译器无法扣除哪个是正确的时,编译器返回错误

编辑

基于,枚举是结构和实现
IConvertible
,因此满足要求,编译器使用首先匹配。与Enum没有冲突,因为Enum比继承层次结构中的struct更重要。

更重要

仅从站点:

重载是指当两个方法具有相同的 姓名,但签名不同。在编译时,编译器会 它将调用哪一个,基于 参数和方法调用的目标

当编译器无法扣除哪个是正确的时,编译器返回错误

编辑

基于,枚举是结构和实现
IConvertible
,因此满足要求,编译器使用首先匹配。与Enum没有冲突,因为Enum比继承层次结构中的struct更重要。

签名:

public static string GetStringValue<TFrom>(this TFrom enumValue) 
因此选择的是方法。

签名:

public static string GetStringValue<TFrom>(this TFrom enumValue) 

因此选择了这种方法。

这是过载解决方案的工作方式。例如,请参阅answer,它是针对空参数编写的,但解析的工作方式是相同的,重载解析也是这样工作的。请参阅,例如,answer-它是为空参数编写的,但解析方式是相同的。在我的例子中,
struct,IConvertible
Enum
,对吗?在我的例子中,
struct,IConvertible
Enum
更具体,对吗?
public static string GetStringValue<ContentMIMEType>(this ContentMIMEType enumValue) 
public static string GetStringValue(this Enum @enum)