C#扩张方法中的类型转换出错
以下代码中,第一种转换没问题,第二种用扩展方法转换却不成功,请大牛们不吝赐教,谢谢!
public static class AAAA
{
public static double ToDouble(this object v)
{
return (double)v;
}
}
public enum MMMM
{
M1 = 1,
M2 = 2
}
class Program
{
static void Main(string[] args)
{
MMMM aaa = MMMM.M1;
object bbb = aaa;
Console.WriteLine((double)(MMMM)bbb);
Console.WriteLine(((MMMM)bbb).ToDouble());
Console.Read();
}
}
------解决方案--------------------using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public static class AAAA
{
public static double ToDouble(this object v)
{
if (v is Enum)
return (double)(int)v;
return (double)v;
}
}
public enum MMMM
{
M1 = 1,
M2 = 2