Wind-Eagle

No pain,no gain!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

操作Enum的一些实用方法

Posted on 2008-12-04 18:12  Andrew Yin  阅读(291)  评论(0)    收藏  举报

操作Enum的一些实用方法:

 

        public static T GetEnumItem<T>(string name)
        
{
            Type type 
= typeof(T);
            
if (Enum.IsDefined(type, name))
                
return (T)Enum.Parse(type, name, true);
            
if (Enum.IsDefined(type, "OTHERS"))
                
return (T)Enum.Parse(type, "OTHERS"true);
            
throw new ArgumentException();
        }


        
public static string GetEnumName<T>(T value)
        
{
            Type type 
= typeof(T);
            
return Enum.GetName(type, value);
        }