Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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# 枚举类型并调用扩展方法_C#_Enums_Type Conversion - Fatal编程技术网

C# 枚举类型并调用扩展方法

C# 枚举类型并调用扩展方法,c#,enums,type-conversion,C#,Enums,Type Conversion,您将枚举类型与其基础类型弄错了。请看以下代码: Printing Fruit Value: 8, Description: I am an apple Value: 9, Description: I am an Banana Value: 10, Description: I am an Grape Value: 11, Description: I am an Kiwi Value: 12, Description: I am an Orange Printing Color Value:

您将枚举类型与其基础类型弄错了。请看以下代码:

Printing Fruit
Value: 8, Description: I am an apple
Value: 9, Description: I am an Banana
Value: 10, Description: I am an Grape
Value: 11, Description: I am an Kiwi
Value: 12, Description: I am an Orange
Printing Color
Value: 56, Description: I am the color Red
Value: 57, Description: I am the color Green
Value: 58, Description: I am the color Blue
Value: 59, Description: I am the color Black
Value: 60, Description: I am the color White
Could not load TypeTester.NonExist
TypeTester.Thing is not an enum.
UnderlingType: TypeTester.Fruit
Value: 8, Description: Apple
Value: 9, Description: Banana
Value: 10, Description: Grape
Value: 11, Description: Kiwi
Value: 12, Description: Orange
UnderlingType: TypeTester.Color
Value: 56, Description: Red
Value: 57, Description: Green
Value: 58, Description: Blue
Value: 59, Description: Black
Value: 60, Description: White
这里,
type
是枚举类型-那么为什么要获取底层类型呢?这两种情况下都是
int
。这不是枚举类型,因此
enum.GetValues
显然会失败

你只需要:

Type type = Type.GetType(typeName, true);
if (!type.IsEnum)
    throw new ArgumentException(typeName + " is not an enum.");

Console.WriteLine("UnderlingType: {0}", type.UnderlyingSystemType);
string description = string.Empty;
foreach ( var thing in Enum.GetValues((type.UnderlyingSystemType)))
{
    ...
}

您将枚举类型与其基础类型弄错了。请看以下代码:

Printing Fruit
Value: 8, Description: I am an apple
Value: 9, Description: I am an Banana
Value: 10, Description: I am an Grape
Value: 11, Description: I am an Kiwi
Value: 12, Description: I am an Orange
Printing Color
Value: 56, Description: I am the color Red
Value: 57, Description: I am the color Green
Value: 58, Description: I am the color Blue
Value: 59, Description: I am the color Black
Value: 60, Description: I am the color White
Could not load TypeTester.NonExist
TypeTester.Thing is not an enum.
UnderlingType: TypeTester.Fruit
Value: 8, Description: Apple
Value: 9, Description: Banana
Value: 10, Description: Grape
Value: 11, Description: Kiwi
Value: 12, Description: Orange
UnderlingType: TypeTester.Color
Value: 56, Description: Red
Value: 57, Description: Green
Value: 58, Description: Blue
Value: 59, Description: Black
Value: 60, Description: White
这里,
type
是枚举类型-那么为什么要获取底层类型呢?这两种情况下都是
int
。这不是枚举类型,因此
enum.GetValues
显然会失败

你只需要:

Type type = Type.GetType(typeName, true);
if (!type.IsEnum)
    throw new ArgumentException(typeName + " is not an enum.");

Console.WriteLine("UnderlingType: {0}", type.UnderlyingSystemType);
string description = string.Empty;
foreach ( var thing in Enum.GetValues((type.UnderlyingSystemType)))
{
    ...
}

明白了。在ConvertFromTypeName方法的foreach中,我更改了

foreach (var thing in Enum.GetValues(type))


看起来我所需要做的就是将它强制转换为一个枚举来获得@extension方法。在ConvertFromTypeName方法的foreach中,我更改了

foreach (var thing in Enum.GetValues(type))


看起来我所需要做的只是将它强制转换到一个枚举中,以获取@the extension method

如果我取消对//Console.WriteLine的注释,我仍然会得到一个错误(“Value:{0},Description:{1}”,(int)thing,thing.GetDescription());我得到错误2'object'不包含'GetDescription'的定义,并且最佳扩展方法重载'TypeTester.Helper.GetDescription(System.Enum)'有一些无效参数。如果我取消对//Console.WriteLine的注释,我仍然会得到错误(“值:{0},描述:{1},(int)thing,thing.GetDescription());我发现错误2“object”不包含“GetDescription”的定义,并且最佳扩展方法重载“TypeTester.Helper.GetDescription(System.Enum)”有一些无效参数。请参阅有关在问题中使用签名的常见问题解答。请参阅有关在问题中使用签名的常见问题解答。