考虑.NET函数签名:
Enum.GetName(Type type, object o);
使用Type
object o
以下代码说明了这一点:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public enum Color
{
Black, White, Red, Orange, Yellow, Green, Blue, Purple, Pink,
DarkRed, DarkGreen, DarkBlue,
NeonGreen, NeonBlue
}
class Program
{
private static Random rand = new Random();
static void Main(string[] args)
{
Color color = getRandomColor();
PrintType(color);
Console.WriteLine("typeof = " + typeof(Color));
Console.ReadLine();
}
public static void PrintType(object o)
{
Type type = o.GetType();
Console.WriteLine("type = " + type);
}
private static Color getRandomColor()
{
var values = Enum.GetValues(typeof(Color));
Color randomColor = (Color)values.GetValue(rand.Next(values.Length));
return randomColor;
}
}
}
输出
type = ConsoleApplication1.Color
typeof = ConsoleApplication1.Color
这意味着Enum.GetName()
方法签名可能会像这样:
Enum.GetName(object o);
答案 0 :(得分:10)
o
不一定是Color
类型。例如:
Enum.GetName(typeof(Color), 3) // == Orange