ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Enum.PaseUnstrict

Permit Enum Parse everytime with valid values using a defaultValue param

Source

public static object ParseUnstrict(this Type enumtype, string value, object defaultValue) {
	object res = defaultValue;
	try {
		if(value == "") {
			return res;
		}
		res = System.Enum.Parse(enumtype, value);
	} catch(Exception ex) {
		//do nothing
	}
	return res;
}

Example

EStatus status = (EStatus)Enum.ParseUnstrict(typeof(EStatus), value, EStatus.unknown));

Author: rallets

Submitted on: 22 mei 2009

Language: C#

Type: System.Enum

Views: 5505