【发布时间】:2018-11-21 12:03:49
【问题描述】:
我有枚举
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
{
...
}
和
public static string GetStringValue(this Enum @enum)
{
...
}
这些方法有不同的签名,但是当我执行下一个操作时ContentMIMEType.Xlsx.GetStringValue()第一个方法被采用。
为什么会发生这种情况,因为对我来说第二种方法的执行更加明显(尝试更改排序顺序,但没有帮助)。
【问题讨论】:
-
这是重载解析的工作方式。例如,请参阅 this 答案 - 它是针对空参数的情况编写的,但解析的工作方式相同。
标签: c# .net enums attributes iconvertible