开发者

Enum.TryParse not supporting in vs2008 in c#

Enum.TryParse(,,out) not supporting in vs2008 in c#? why? I开发者_JS百科 am trying to use but getting error that TryParse no defined.


Enum.TryParse was introduced in .NET 4. However, you might like to use my Unconstrained Melody library which has something similar, and many other features.


 public static bool TryParse<T>(this Enum theEnum, string valueToParse, out T returnValue)
 {
    returnValue = default(T);
    int intEnumValue;
    if (Int32.TryParse(valueToParse, out intEnumValue))
    {
        if (Enum.IsDefined(typeof(T), intEnumValue))
        {
           returnValue = (T)(object)intEnumValue;
           return true;
        }
    }
    return false;
  }


As per MSDN, Enum.TryParse was not added until .NET 4. VS2008 targets up to .NET 3.5SP1, so that is why you cannot access this method.


This question includes a number of implementation approaches: How to TryParse for Enum value?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜