考虑.NET函数签名:
Enum.GetName(Type type, object o);
Run Code Online (Sandbox Code Playgroud)
似乎完全不需要询问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(); …Run Code Online (Sandbox Code Playgroud)