Improvement of Generic Enum Parser and .NET Extensions Methods Library for C# and VB.NET

I updated the previous code about Generic Enum Parser. That code is I include into the .NET Extensions Methods Library for C# and VB.NET project in the Codeplex. The code is below,

public static TEnum ParseStringToEnum<TEnum>(this string dataToMatch, bool ignorecase = default(bool))
       where TEnum : struct
{
       return dataToMatch.IsItemInEnum<TEnum>()() ? default(TEnum) : (TEnum)Enum.Parse(typeof(TEnum), dataToMatch, default(bool));
}
 
public static Func<bool> IsItemInEnum<TEnum>(this string dataToCheck)
        where TEnum : struct
{
        return () => { return string.IsNullOrEmpty(dataToCheck) || !Enum.IsDefined(typeof(TEnum), dataToCheck); };
}
If anyone wants to download it please visit here. The code is part of the StringExtensions class.

No comments:

Post a Comment